Advertisement
Guest User

Untitled

a guest
Jun 28th, 2017
51
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.52 KB | None | 0 0
  1. import java.io.*;
  2. import java.util.*;
  3.  
  4. class Biostar45366
  5. {
  6. private BufferedReader in;
  7. private String buffer;
  8. private Map<String,Term> id2term=new HashMap<String,Term>();
  9. private class Term
  10. {
  11. String id;
  12. String name;
  13. String def;
  14. Set<String> children=new HashSet<String>();
  15. Set<String> is_a=new HashSet<String>();
  16.  
  17. int depth()
  18. {
  19. int min_child=0;
  20. for(String p:is_a)
  21. {
  22. Term parent=id2term.get(p);
  23. if(parent==null)
  24. {
  25. System.err.println("Cannot get "+p);
  26. continue;
  27. }
  28. int n2= parent.depth();
  29. if(min_child==0 || n2<min_child) min_child=n2;
  30. }
  31. return 1+ min_child;
  32. }
  33.  
  34. public String toString()
  35. {
  36. return id+"\t"+name+"\t"+is_a;
  37. }
  38. }
  39.  
  40. private Set<String> getAllDescendantById(String id)
  41. {
  42. Set<String> set=new HashSet<String>();
  43. set.add(id);
  44. Term t=id2term.get(id);
  45. for(String c:t.children)
  46. {
  47. set.addAll(getAllDescendantById(c));
  48. }
  49. return set;
  50. }
  51.  
  52.  
  53. private Term getTermById(String id,boolean create)
  54. {
  55. Term t= this.id2term.get(id);
  56. if(t==null && create)
  57. {
  58. t=new Term();
  59. t.id=id;
  60. t.name=id;
  61. t.def=id;
  62. this.id2term.put(id,t);
  63. }
  64. return t;
  65. }
  66.  
  67. private static String nocomment(String s)
  68. {
  69. int excl=s.indexOf('!');
  70. if(excl!=-1) s=s.substring(0,excl);
  71. return s.trim();
  72. }
  73.  
  74. private String next() throws IOException
  75. {
  76. if(buffer!=null)
  77. {
  78. String s=buffer;
  79. buffer=null;
  80. return s;
  81. }
  82. return in.readLine();
  83. }
  84.  
  85. private void parseTerm() throws IOException
  86. {
  87. Term t=null;
  88. String line;
  89. while((line=next())!=null)
  90. {
  91. if(line.startsWith("["))
  92. {
  93. this.buffer=line;
  94. break;
  95. }
  96. int colon=line.indexOf(':');
  97. if(colon==-1) continue;
  98. if(line.startsWith("id:") && t==null)
  99. {
  100. t=getTermById(line.substring(colon+1).trim(),true);
  101. continue;
  102. }
  103. if(t==null) continue;
  104. if(line.startsWith("name:"))
  105. {
  106. t.name=nocomment(line.substring(colon+1));
  107. continue;
  108. }
  109. else if(line.startsWith("def:"))
  110. {
  111. t.def=nocomment(line.substring(colon+1));
  112. continue;
  113. }
  114. else if(line.startsWith("is_a:"))
  115. {
  116. String rel=nocomment(line.substring(colon+1));
  117. t.is_a.add(rel);
  118. Term parent=getTermById(rel,true);
  119. parent.children.add(t.id);
  120. continue;
  121. }
  122. }
  123. }
  124. private void parse() throws IOException
  125. {
  126. in=new BufferedReader(new InputStreamReader(System.in));
  127. String line;
  128. while((line=next())!=null)
  129. {
  130. if(line.equals("[Term]")) parseTerm();
  131. }
  132. in.close();
  133. }
  134. public static void main(String args[]) throws IOException
  135. {
  136. Biostar45366 app=new Biostar45366();
  137. app.parse();
  138. int level=1;
  139. boolean found=true;
  140. while(found)
  141. {
  142. found=false;
  143. for(Term t: app.id2term.values())
  144. {
  145. if(t.depth()==level)
  146. {
  147. System.out.println(""+level+"\t"+t);
  148. found=true;
  149. }
  150. }
  151. level++;
  152. }
  153. for(String id: app.getAllDescendantById("GO:0001783"))
  154. {
  155. System.out.println(app.id2term.get(id));
  156. }
  157. }
  158. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement