| 1 |
package pp2.datahandler.blast; |
| 2 |
|
| 3 |
import java.io.BufferedReader; |
| 4 |
import java.io.IOException; |
| 5 |
import java.util.ArrayList; |
| 6 |
import java.util.regex.Matcher; |
| 7 |
import java.util.regex.Pattern; |
| 8 |
|
| 9 |
import pp2.components.BlastHit; |
| 10 |
import pp2.tools.Tools; |
| 11 |
|
| 12 |
public class BlastParser { |
| 13 |
|
| 14 |
private ArrayList<ArrayList<BlastHit>> round_results = new ArrayList<ArrayList<BlastHit>>(); // each ArrayList element holds a list of the results for each round |
| 15 |
|
| 16 |
public BlastParser(String file){ |
| 17 |
parseFile(file); |
| 18 |
} |
| 19 |
|
| 20 |
public ArrayList<ArrayList<BlastHit>> getData(){ |
| 21 |
return round_results; |
| 22 |
} |
| 23 |
|
| 24 |
public void parseFile(String file) { |
| 25 |
BufferedReader bl_data = Tools.openFile(file); |
| 26 |
String line = ""; |
| 27 |
try { |
| 28 |
boolean new_entry = false; |
| 29 |
StringBuffer entry = new StringBuffer(""); |
| 30 |
Pattern which_round = Pattern.compile("Results\\s+from\\s+round\\s+(\\d+)"); |
| 31 |
int round = 0; |
| 32 |
|
| 33 |
while((line = bl_data.readLine()) != null){ |
| 34 |
Matcher ma = which_round.matcher(line); |
| 35 |
if (ma.matches()){ |
| 36 |
if (entry.length() > 0){ |
| 37 |
round_results.get(round).add(new BlastHit(entry.toString(), round+1)); |
| 38 |
entry = new StringBuffer(""); |
| 39 |
new_entry = false; |
| 40 |
} |
| 41 |
round = Integer.valueOf(ma.group(1))-1; |
| 42 |
ArrayList<BlastHit> bl = new ArrayList<BlastHit>(); |
| 43 |
round_results.add(bl); |
| 44 |
} |
| 45 |
|
| 46 |
if(line.startsWith(">")) { |
| 47 |
if (entry.length()<1){ //reached first hit |
| 48 |
new_entry = true; |
| 49 |
entry = new StringBuffer(""); |
| 50 |
entry.append(line+"\n"); |
| 51 |
} |
| 52 |
else{ // reached hit 2..n |
| 53 |
new_entry = true; |
| 54 |
round_results.get(round).add(new BlastHit(entry.toString(), round+1)); |
| 55 |
entry = new StringBuffer(""); |
| 56 |
entry.append(line+"\n"); |
| 57 |
} |
| 58 |
} |
| 59 |
else if (new_entry){ // all lines between the entries are added |
| 60 |
entry.append(line+"\n"); |
| 61 |
} |
| 62 |
} // while |
| 63 |
round_results.get(round).add(new BlastHit(entry.toString(), round+1)); // add last hit |
| 64 |
} catch (IOException e) { |
| 65 |
// TODO Auto-generated catch block |
| 66 |
e.printStackTrace(); |
| 67 |
} |
| 68 |
} |
| 69 |
|
| 70 |
public static void main (String[] args){ |
| 71 |
if(args.length > 0) { |
| 72 |
String blast_output_file = ""; |
| 73 |
int round = -1; |
| 74 |
for(int k=0; k<args.length; k+=2) { |
| 75 |
if(args[k].equals("-i")) { |
| 76 |
blast_output_file = args[k+1]; |
| 77 |
} |
| 78 |
else if(args[k].equals("-r")) { |
| 79 |
try{ |
| 80 |
round = Integer.valueOf(args[k+1])-1; |
| 81 |
} |
| 82 |
catch(NumberFormatException e){ |
| 83 |
System.out.println("ERROR: Parameter r has to be an integer!"); |
| 84 |
printUsage(); |
| 85 |
System.exit(0); |
| 86 |
} |
| 87 |
} |
| 88 |
else { |
| 89 |
printUsage(); |
| 90 |
System.exit(0); |
| 91 |
} |
| 92 |
} |
| 93 |
BlastParser bp = new BlastParser(blast_output_file); |
| 94 |
if (round < 0){ |
| 95 |
for (int i = 0; i< bp.getData().size(); i++){ |
| 96 |
System.out.println("Round: "+(i+1)); |
| 97 |
for (int j = 0; j < bp.getData().get(i).size();j++){ |
| 98 |
System.out.println(bp.getData().get(i).get(j).toString()); |
| 99 |
} |
| 100 |
} |
| 101 |
} |
| 102 |
else{ |
| 103 |
try{ |
| 104 |
for (int j = 0; j < bp.getData().get(round).size();j++){ |
| 105 |
System.out.println(bp.getData().get(round).get(j).toString()); |
| 106 |
} |
| 107 |
} |
| 108 |
catch(IndexOutOfBoundsException e){ |
| 109 |
System.out.println("ERROR: No results for round "+round+" available!"); |
| 110 |
System.exit(0); |
| 111 |
} |
| 112 |
} |
| 113 |
} |
| 114 |
else { |
| 115 |
printUsage(); |
| 116 |
} |
| 117 |
} |
| 118 |
|
| 119 |
public static void printUsage(){ |
| 120 |
System.out.println("Usage: java -jar blastparser.jar -i [Blast-output-file] (-r [results from which round? (int 1..n)])\n no -r shows all results"); |
| 121 |
} |
| 122 |
|
| 123 |
} |