package pp2.datahandler.blast; import java.io.BufferedReader; import java.io.IOException; import java.util.ArrayList; import java.util.regex.Matcher; import java.util.regex.Pattern; import pp2.components.BlastHit; import pp2.tools.Tools; public class BlastParser { private ArrayList> round_results = new ArrayList>(); // each ArrayList element holds a list of the results for each round public BlastParser(String file){ parseFile(file); } public ArrayList> getData(){ return round_results; } public void parseFile(String file) { BufferedReader bl_data = Tools.openFile(file); String line = ""; try { boolean new_entry = false; StringBuffer entry = new StringBuffer(""); Pattern which_round = Pattern.compile("Results\\s+from\\s+round\\s+(\\d+)"); int round = 0; while((line = bl_data.readLine()) != null){ Matcher ma = which_round.matcher(line); if (ma.matches()){ if (entry.length() > 0){ round_results.get(round).add(new BlastHit(entry.toString(), round+1)); entry = new StringBuffer(""); new_entry = false; } round = Integer.valueOf(ma.group(1))-1; ArrayList bl = new ArrayList(); round_results.add(bl); } if(line.startsWith(">")) { if (entry.length()<1){ //reached first hit new_entry = true; entry = new StringBuffer(""); entry.append(line+"\n"); } else{ // reached hit 2..n new_entry = true; round_results.get(round).add(new BlastHit(entry.toString(), round+1)); entry = new StringBuffer(""); entry.append(line+"\n"); } } else if (new_entry){ // all lines between the entries are added entry.append(line+"\n"); } } // while round_results.get(round).add(new BlastHit(entry.toString(), round+1)); // add last hit } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } } public static void main (String[] args){ if(args.length > 0) { String blast_output_file = ""; int round = -1; for(int k=0; k