| 1 |
import java.io.BufferedReader; |
| 2 |
import java.io.BufferedWriter; |
| 3 |
import java.io.FileNotFoundException; |
| 4 |
import java.io.FileReader; |
| 5 |
import java.io.FileWriter; |
| 6 |
import java.io.IOException; |
| 7 |
import java.util.ArrayList; |
| 8 |
import java.util.Arrays; |
| 9 |
import java.util.Collection; |
| 10 |
import java.util.HashMap; |
| 11 |
import java.util.LinkedList; |
| 12 |
import java.util.List; |
| 13 |
|
| 14 |
/** |
| 15 |
* @author Ariane |
| 16 |
* |
| 17 |
*/ |
| 18 |
public class ParseGONew { |
| 19 |
|
| 20 |
private String file; |
| 21 |
private ArrayList<String> allParents = new ArrayList<String>(); |
| 22 |
private ArrayList<String> goTerms = new ArrayList<String>(); |
| 23 |
private ArrayList<String> rootTerms = new ArrayList<String>(); |
| 24 |
private ArrayList<Integer> maxPath = new ArrayList<Integer>(); |
| 25 |
|
| 26 |
|
| 27 |
/** |
| 28 |
* Constructor |
| 29 |
* @param GOFile |
| 30 |
*/ |
| 31 |
public ParseGONew(String GOFile){ |
| 32 |
file = GOFile; |
| 33 |
} |
| 34 |
|
| 35 |
/** |
| 36 |
* The Parser for the deductiveClosure textFile |
| 37 |
* @throws IOException |
| 38 |
*/ |
| 39 |
public void ParseDeductiveClosure() throws IOException{ |
| 40 |
|
| 41 |
BufferedReader reader = new BufferedReader(new FileReader(file)); |
| 42 |
|
| 43 |
String line; |
| 44 |
ArrayList<LinkedList<String>> directParents = new ArrayList<LinkedList<String>>(); |
| 45 |
|
| 46 |
String allPar = ""; |
| 47 |
while((line = reader.readLine())!= null){ |
| 48 |
|
| 49 |
String[] templine = line.split("\t"); |
| 50 |
|
| 51 |
String go = templine[0]; |
| 52 |
String parent = templine[2]; |
| 53 |
String link = templine[3]; |
| 54 |
String relation = templine[1]; |
| 55 |
|
| 56 |
System.out.println(line); |
| 57 |
|
| 58 |
if(relation.equals("OBO_REL:is_a") || relation.equals("part_of")){ |
| 59 |
|
| 60 |
if (!goTerms.contains(go)){ |
| 61 |
allParents.add(allPar); |
| 62 |
allPar = ""; |
| 63 |
|
| 64 |
goTerms.add(go); |
| 65 |
|
| 66 |
if(parent.equals("GO:0003674")){ |
| 67 |
rootTerms.add("mol_func"); |
| 68 |
} |
| 69 |
if(parent.equals("GO:0008150")){ |
| 70 |
rootTerms.add("bio_proc"); |
| 71 |
} |
| 72 |
if(parent.equals("GO:0005575")){ |
| 73 |
rootTerms.add("cel_comp"); |
| 74 |
} |
| 75 |
|
| 76 |
LinkedList<String> firstParent = new LinkedList<String>(); |
| 77 |
firstParent.add(parent); |
| 78 |
|
| 79 |
if(link.equals("1")){ |
| 80 |
directParents.add(firstParent); |
| 81 |
allPar+= (parent + ","); |
| 82 |
} |
| 83 |
if(!link.equals("1")){ |
| 84 |
allPar+= (parent + ","); |
| 85 |
} |
| 86 |
} |
| 87 |
|
| 88 |
else{ |
| 89 |
int index = goTerms.indexOf(go); |
| 90 |
|
| 91 |
if(parent.equals("GO:0003674")){ |
| 92 |
rootTerms.add("mol_func"); |
| 93 |
} |
| 94 |
if(parent.equals("GO:0008150")){ |
| 95 |
rootTerms.add("bio_proc"); |
| 96 |
} |
| 97 |
if(parent.equals("GO:0005575")){ |
| 98 |
rootTerms.add("cel_comp"); |
| 99 |
} |
| 100 |
|
| 101 |
if(link.equals("1")){ |
| 102 |
LinkedList<String> inDirParent = directParents.get(index); |
| 103 |
inDirParent.add(parent); |
| 104 |
directParents.remove(index); |
| 105 |
directParents.add(index, inDirParent); |
| 106 |
} |
| 107 |
|
| 108 |
allPar+= (parent + ","); |
| 109 |
} |
| 110 |
} |
| 111 |
|
| 112 |
} |
| 113 |
allParents.add(allPar); |
| 114 |
allParents.remove(0); |
| 115 |
getAllParents(directParents); |
| 116 |
|
| 117 |
} |
| 118 |
|
| 119 |
/** |
| 120 |
* calculates the longestPath to the root for each GOTerm |
| 121 |
* and starts the creation of the outfile |
| 122 |
* @param parents |
| 123 |
*/ |
| 124 |
private void getAllParents(ArrayList<LinkedList<String>> parents){ |
| 125 |
|
| 126 |
String output = ""; |
| 127 |
for(int i = 0; i < goTerms.size(); i++){ |
| 128 |
System.out.println(goTerms.get(i)); |
| 129 |
int longestPath = getLongestPathToRoot(parents, i); |
| 130 |
maxPath.add(longestPath); |
| 131 |
} |
| 132 |
|
| 133 |
createOutputString(); |
| 134 |
} |
| 135 |
|
| 136 |
/** |
| 137 |
* creates the outputString for further work |
| 138 |
*/ |
| 139 |
private void createOutputString(){ |
| 140 |
|
| 141 |
try { |
| 142 |
BufferedWriter writer = new BufferedWriter(new FileWriter("/mnt/home/hampt/workspace/metastudent_debian_trunk/metastudentPkg/lib/groupA/GO_Parser/GO_Parents.2014.txt")); |
| 143 |
String out = ""; |
| 144 |
for(int i = 0; i< goTerms.size(); i++){ |
| 145 |
out = (goTerms.get(i) + "\t" + allParents.get(i) + "\t" + maxPath.get(i) + "\t" + rootTerms.get(i) + "\n"); |
| 146 |
writer.write(out); |
| 147 |
} |
| 148 |
writer.close(); |
| 149 |
|
| 150 |
} catch (IOException e) { |
| 151 |
e.printStackTrace(); |
| 152 |
} |
| 153 |
|
| 154 |
// String out = ""; |
| 155 |
// for(int i = 0; i< goTerms.size(); i++){ |
| 156 |
// out += (goTerms.get(i) + "\t" + allParents.get(i) + "\t" + maxPath.get(i) + "\t" + rootTerms.get(i) + "\n"); |
| 157 |
// } |
| 158 |
// System.out.println(out); |
| 159 |
} |
| 160 |
|
| 161 |
/** |
| 162 |
* returns for a specific GOTerm the number of steps to the root |
| 163 |
* @param parents |
| 164 |
* @param posGO |
| 165 |
* @return |
| 166 |
*/ |
| 167 |
private int getLongestPathToRoot(ArrayList<LinkedList<String>> parents, int posGO){ |
| 168 |
int longestPath = 0; |
| 169 |
|
| 170 |
LinkedList<String> diPar = parents.get(posGO); |
| 171 |
HashMap<String, Integer> par = new HashMap<String, Integer>(); |
| 172 |
|
| 173 |
for(int i = 0; i < diPar.size(); i++){ |
| 174 |
par.put(diPar.get(i), 1); |
| 175 |
} |
| 176 |
|
| 177 |
int counter = 2; |
| 178 |
for(int j = 0; j < diPar.size(); j++){ |
| 179 |
|
| 180 |
int index = goTerms.indexOf(diPar.get(j)); |
| 181 |
if(index != -1){ |
| 182 |
LinkedList<String> help = parents.get(index); |
| 183 |
for(int k = 0; k < help.size(); k++){ |
| 184 |
String pa = help.get(k); |
| 185 |
|
| 186 |
if(par.containsKey(pa)){ |
| 187 |
int val = par.get(pa) + 1; |
| 188 |
par.remove(pa); |
| 189 |
par.put(pa, val); |
| 190 |
} |
| 191 |
else{ |
| 192 |
diPar.add(pa); |
| 193 |
par.put(pa, counter); |
| 194 |
} |
| 195 |
} |
| 196 |
counter++; |
| 197 |
} |
| 198 |
} |
| 199 |
|
| 200 |
Collection<Integer> col = par.values(); |
| 201 |
Object[] values = col.toArray(); |
| 202 |
|
| 203 |
longestPath = (Integer) values[0]; |
| 204 |
return longestPath; |
| 205 |
} |
| 206 |
|
| 207 |
} |