| 1 |
package pp2.datahandler.blast; |
| 2 |
|
| 3 |
import java.io.*; |
| 4 |
|
| 5 |
public class TabularBlastParser { |
| 6 |
|
| 7 |
public static final int eValPos = 10; |
| 8 |
public static final int hspPos = 11; //TODO |
| 9 |
|
| 10 |
|
| 11 |
public static void parseBlast(Process blastProcess, Writer output) throws IOException, InterruptedException { |
| 12 |
|
| 13 |
|
| 14 |
BufferedReader blast = new BufferedReader(new InputStreamReader(blastProcess.getInputStream())); |
| 15 |
|
| 16 |
String line; |
| 17 |
String prevQuery = ""; |
| 18 |
String bestHit = ""; |
| 19 |
double bestE = Double.MAX_VALUE; |
| 20 |
|
| 21 |
while((line = blast.readLine()) != null) { |
| 22 |
|
| 23 |
if(!line.startsWith("#")) { |
| 24 |
|
| 25 |
String[] content = line.split("\t"); |
| 26 |
if(content.length > Math.max(hspPos, eValPos)) { |
| 27 |
|
| 28 |
String query = content[0]; |
| 29 |
String hit = content[1]; |
| 30 |
|
| 31 |
double eValue = Double.parseDouble(content[eValPos]); |
| 32 |
double hsp = Double.parseDouble(content[hspPos]); |
| 33 |
|
| 34 |
if(!query.equals(prevQuery)) { |
| 35 |
|
| 36 |
output.write(bestHit); |
| 37 |
bestHit = query+"\t"+hit+"\t"+eValue+"\t"+hsp+"\n"; |
| 38 |
bestE = eValue; |
| 39 |
|
| 40 |
} else { |
| 41 |
|
| 42 |
if(eValue < bestE) { |
| 43 |
bestE = eValue; |
| 44 |
bestHit = query+"\t"+hit+"\t"+eValue+"\t"+hsp+"\n"; |
| 45 |
} else if(eValue == bestE) { |
| 46 |
String[] old = bestHit.split("\t"); |
| 47 |
bestHit = query+"\t"+old[1]+","+hit+"\t"+eValue+"\t"+hsp+"\n"; |
| 48 |
} |
| 49 |
|
| 50 |
} |
| 51 |
|
| 52 |
prevQuery = query; |
| 53 |
} |
| 54 |
} |
| 55 |
} |
| 56 |
|
| 57 |
int blastExitCode = blastProcess.waitFor(); |
| 58 |
|
| 59 |
if(blastExitCode != 0) System.err.println("BLAST Exit code was " + blastExitCode + "!"); |
| 60 |
|
| 61 |
output.flush(); |
| 62 |
output.close(); |
| 63 |
blast.close(); |
| 64 |
|
| 65 |
} |
| 66 |
|
| 67 |
} |