BatchBlastExtractor

About BatchBlastExtractor
A simple parser for batch BLAST programs output, with a user friendly graphical interface to extract information from BLASTX output. It generates a tab delimited text file that can be easily imported into any statistical package such as Excel or SPSS for further analysis. For each BLAST hit, the program obtains and saves the essential features from the BLAST output file that would allow further analysis. The program was written in Java and therefore is OS independent. It works on both Windows and Linux OS with java 7+.

Citation
Pirooznia M, Perkins EJ, Deng Y. Batch Blast Extractor: an automated blastx parser application. BMC Genomics. 2008 Sep 16;9 S2:S10. PMID: 18831775

Download
Binary: https://bitbucket.org/mpiro/bbe/downloads

Source: https://bitbucket.org/mpiro/bbe/src



package blastanalyser;

import java.awt.Toolkit; import javax.swing.SwingUtilities; import javax.swing.UIManager; import java.awt.Dimension;

public class BatchBlastExtractor { boolean packFrame = false;

public BatchBlastExtractor { BatchBlastExtractor_Frame frame = new BatchBlastExtractor_Frame; if (packFrame) { frame.pack; }   else { frame.validate; }

Dimension screenSize = Toolkit.getDefaultToolkit.getScreenSize; Dimension frameSize = frame.getSize; if (frameSize.height > screenSize.height) { frameSize.height = screenSize.height; }   if (frameSize.width > screenSize.width) { frameSize.width = screenSize.width; }   frame.setLocation( (screenSize.width - frameSize.width) / 2,                      (screenSize.height - frameSize.height) / 2); frame.setVisible(true); }

public static void main(String[] args) { SwingUtilities.invokeLater(new Runnable {     public void run {        try {          UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName);        }        catch (Exception exception) {          exception.printStackTrace;        }

new BatchBlastExtractor; }   });  } }

analyser.java


package blastanalyser;

import java.io.*; import java.util.regex.*; import java.awt.FileDialog; import javax.swing.JFrame; import org.apache.commons.io.FileUtils;

public class analyser { public analyser { } private String str = ""; private int openFlag = 0; private int saveFlag = 0; String filePath, fileOutputPath; String filename;

public static void main(String args[]) {

analyser analyser = new analyser; String thisLine;

try { BufferedReader br = new BufferedReader(new FileReader( "blastx_Input.txt")); FileWriter fw = new FileWriter("blastx_Output.xls"); BufferedWriter out = new BufferedWriter(fw); writeLine(out, "QueryID\tQueryLen\tAccession Version #\tLength\tScore\tbit\tEvalue\tIdentities\tPositives\tGaps\tOrganism\tDescription"); String str = ""; String str2 = ""; int i=1;

while ( (thisLine = br.readLine) != null) { // while loop begins here

if (thisLine.matches("^Query=.*$")) { String ab = thisLine.substring(7).trim; // all ab = ab + " "; ab = ab.substring(0, ab.indexOf(" ")); // non contig str = ab.toString;//thisLine.toString.substring(7, 30); }       else if (thisLine.matches("^.*letters\\).*$")) {          int pos2 = thisLine.toString.indexOf("lett");          str = str + "\t" + thisLine.toString.substring(10, pos2 - 1) ;          str2 = str;        }        else if (thisLine.matches("^.*Sequences producing significant alignments:.*$")) { // Accession Version #          int k = 0;

// best 100, 20, 5, 1

while ((k <1 && (!(thisLine.matches("^Query=.*$"))))){ if (thisLine.matches("^>.*$")) { // Accession Version #

String tmp = thisLine.substring(thisLine.indexOf("|")+1);

String tmpstr = tmp.substring(0, tmp.indexOf("|")); String desc,org =" ";

if (tmp.matches("^.*\\[.*$")){ desc = tmp.substring(tmp.indexOf("|") + 1,tmp.indexOf("[")); org = tmp.substring(tmp.indexOf("[")+1); }else{ desc = tmp.substring(tmp.indexOf("|") + 1); }

if (org.matches("^.*\\].*$")){ org = org.substring(0, org.indexOf("]")); }

str = str2 + "\t"+tmpstr; k++; do { thisLine = br.readLine; }             while (! (thisLine.matches("^.*Length =.*$"))); str = str + "\t" + thisLine.substring(thisLine.indexOf("Length =") +9); do { thisLine = br.readLine; }               while (! (thisLine.matches("^.*Score =.*$"))); str = str + "\t" + thisLine.substring((thisLine.indexOf("Score =")+7),(thisLine.indexOf("bits"))) +"\t"+ thisLine.substring((thisLine.indexOf("bits")+6),(thisLine.indexOf("Expect")-3)) +"\t"+ thisLine.substring((thisLine.indexOf("Expect =")+8));

thisLine = br.readLine; // next line to Identities...

String positives = ""; String gaps = ""; if (thisLine.matches("^.*Gaps =.*$")){ positives = thisLine.substring((thisLine.indexOf("Positives =")+11),(thisLine.indexOf("Gaps =")-2)); gaps = thisLine.substring((thisLine.indexOf("Gaps =")+6)); }else{ positives = thisLine.substring((thisLine.indexOf("Positives =")+11)); }

str = str + "\t" + thisLine.substring((thisLine.indexOf("Identities =")+12),(thisLine.indexOf("Positives =")-2)) +"\t"+positives+"\t"+gaps+"\t"+org+ "\t"+desc ;

System.out.println(i+"\t"+str); writeLine(out, str); str = "\t";//str.substring(0, 15); i++;

}          thisLine = br.readLine; }

if (thisLine.matches("^Query=.*$")) { //str = thisLine.toString.substring(7, 18);

String ab = thisLine.substring(7).trim; // all ab = ab + " "; ab = ab.substring(0, ab.indexOf(" ")); // non contig

str = ab.toString;//thisLine.toString.substring(7, 30); }       }       else if (thisLine.matches("^.*No hits found.*$")) { str = str +"\t ******No hits\t found.******"; System.out.println(i+"\t"+str); writeLine(out, str); i++;

}     } // end while readLine

out.close; } // end try catch (IOException e) { System.err.println("Error: " + e); } } // end main

public void openFileInput { JFrame frame = new JFrame; FileDialog loadDialog = new FileDialog(frame, "Open File", FileDialog.LOAD); loadDialog.setVisible(true); if (loadDialog.getFile != null) { openFlag = 1; filename = loadDialog.getDirectory + loadDialog.getFile; filePath = filename; fileOutputPath = loadDialog.getDirectory; } }  public void setFileOutput { //openning the file dialog JFrame frame = new JFrame; FileDialog loadDialog = new FileDialog(frame, "Set File", FileDialog.SAVE); loadDialog.setVisible(true); if (loadDialog.getFile != null) { openFlag = 1; fileOutputPath = loadDialog.getDirectory + loadDialog.getFile; } }    public void setStr(String str) { this.str = str; }   public String getStr { return str; }   public String getfilePath { return filePath; }   public String getfileOutputPath { return fileOutputPath; }   public void setOpenFlag(int openFlag) { this.openFlag = openFlag; }   public int getOpenFlag { return openFlag; }   public void setSaveFlag(int saveFlag) { this.saveFlag = saveFlag; }   public int getSaveFlag { return saveFlag; }

public void analyse(String Inputfilename1,String Outputfilename1,int NumberOfResults) { String thisLine; String ab =""; try { File f = new File(Inputfilename1+".tmp"); String content = FileUtils.readFileToString(new File(Inputfilename1)); String linesep = System.lineSeparator; FileUtils.writeStringToFile(f, content.replaceAll(linesep+linesep+"Length=", linesep+"myletters=")); BufferedReader br = new BufferedReader(new FileReader(Inputfilename1+".tmp")); FileWriter fw = new FileWriter(Outputfilename1); BufferedWriter out = new BufferedWriter(fw); writeLine(out, "QueryID\tQueryLen\tAccession Version #\tLength\tScore\tbit\tEvalue\tIdentities\tPositives\tGaps\tFrame\tOrganism\tDescription"); String str = ""; String str2 = ""; int i=1; while ( (thisLine = br.readLine) != null) { // while loop begins here if (thisLine.matches("^Query=.*$")) { thisLine = thisLine.replaceAll(" ", "_"); ab = thisLine.substring(7).trim; // all ab = ab + " "; str = ab.toString; str2 = str; }       else if (  thisLine.matches("^.*myletters=.*$") ) { str = str + "\t" + thisLine.toString.substring(10).trim ; str2 = str; }      else if ( thisLine.matches("^.*letters\\).*$")) {         int pos2 = thisLine.toString.indexOf("lett");         str = str + "\t" + thisLine.toString.substring(10, pos2 - 1) ;          str2 = str;        }        else if (thisLine.matches("^.*Sequences producing significant alignments:.*$")) { // Accession Version #          int k = 0;          // best 100, 20, 5, 1          while (k < NumberOfResults && (!(thisLine.matches("^Query=.*$"))) && (!(thisLine.matches("Matrix: BLOSUM62"))) ){            if (thisLine.matches("^>.*$")) { // Accession Version #              String tmp = thisLine.substring(thisLine.indexOf("|")+1);              String tmpstr = tmp.substring(0, tmp.indexOf("|"));              String desc,org =" ";              if (tmp.matches("^.*\\[.*$")){                desc = tmp.substring(tmp.indexOf("|") + 1,tmp.indexOf("["));                org = tmp.substring(tmp.indexOf("[")+1); }else{ desc = tmp.substring(tmp.indexOf("|") + 1); }             if (org.matches("^.*\\].*$")){ org = org.substring(0, org.indexOf("]")); }            str = str2 + "\t"+tmpstr; k++; do { thisLine = br.readLine; }             while (! (thisLine.matches("^.*Length.*$"))); thisLine = thisLine.replaceAll(" ", ""); str = str + "\t" + thisLine.substring(thisLine.indexOf("Length") +7); do { thisLine = br.readLine; }               while (! (thisLine.matches("^.*Score =.*$"))); thisLine = thisLine.replaceAll(" ", " "); str = str + "\t" + thisLine.substring((thisLine.indexOf("Score =")+7),(thisLine.indexOf("bits"))) +"\t"+ thisLine.substring((thisLine.indexOf("bits")+6),(thisLine.indexOf("Expect")-3)) +"\t"+ thisLine.substring((thisLine.indexOf("Expect =")+8)); thisLine = br.readLine; // next line to Identities... String positives = ""; String gaps = ""; if (thisLine.matches("^.*Gaps =.*$")){ positives = thisLine.substring((thisLine.indexOf("Positives =")+11),(thisLine.indexOf("Gaps =")-2)); gaps = thisLine.substring((thisLine.indexOf("Gaps =")+6)); }else{ positives = thisLine.substring((thisLine.indexOf("Positives =")+11)); }           String frame = ""; frame = br.readLine.toString; frame = frame.substring((frame.indexOf("Frame =")+7)); str = str + "\t" + thisLine.substring((thisLine.indexOf("Identities =")+12),(thisLine.indexOf("Positives =")-2)) +"\t"+positives+"\t"+gaps+"\t"+frame+"\t"+org+ "\t"+desc ; writeLine1(out, str); System.out.println(i+"\t"+ab); str = "\t"; i++; }          thisLine = br.readLine; }      if (thisLine.matches("^Query=.*$")) { thisLine = thisLine.replaceAll(" ", "_"); ab = thisLine.substring(7).trim; // all ab = ab + " "; str = ab.toString; str2 = str; }       } // End of Accession Version else if (thisLine.matches("^.*No hits found.*$")) { str = str +"\t ******No hits\t found.******"; System.out.println(i+"\t"+ab); writeLine1(out, str); i++; }     } // end while readLine System.out.println ("Done"); out.close; br.close; f.delete; } // end try catch (IOException e) { System.err.println("Error: " + e); } }

void writeLine1(BufferedWriter out, String line) throws IOException { out.write(line, 0, line.length); out.newLine; }

static void writeLine(BufferedWriter out, String line) throws IOException { out.write(line, 0, line.length); out.newLine; }

}

frame.java


package blastanalyser;

import java.awt.*; import java.awt.event.*; import javax.swing.JFrame; import javax.swing.JPanel; import javax.swing.JMenuBar; import javax.swing.JMenu; import javax.swing.JMenuItem; import javax.swing.JToolBar; import javax.swing.JButton; import javax.swing.ImageIcon; import javax.swing.JLabel; import java.awt.Dimension; import java.awt.Rectangle; import javax.swing.JFileChooser; import javax.swing.JTextField; import javax.swing.BorderFactory; import java.awt.Color; import javax.swing.border.TitledBorder; import javax.swing.JComboBox; import javax.swing.UIManager; import java.awt.Font;

public class BatchBlastExtractor_Frame extends JFrame { JPanel contentPane; JMenuBar jMenuBar1 = new JMenuBar; JMenu jMenuFile = new JMenu; JMenuItem jMenuFileExit = new JMenuItem; JMenu jMenuHelp = new JMenu; JMenuItem jMenuHelpAbout = new JMenuItem; ImageIcon image1 = new ImageIcon(blastanalyser.BatchBlastExtractor_Frame.class.                                  getResource("openFile.png")); ImageIcon image2 = new ImageIcon(blastanalyser.BatchBlastExtractor_Frame.class.                                  getResource("closeFile.png")); ImageIcon image3 = new ImageIcon(blastanalyser.BatchBlastExtractor_Frame.class.                                  getResource("help.png")); JLabel statusBar = new JLabel; JLabel jLabel1 = new JLabel; JTextField jTxtFieldInput = new JTextField; JButton jBtnInput = new JButton; JTextField jTxtFieldOutput = new JTextField; JLabel jLabel2 = new JLabel; JButton jBtnOutput = new JButton; JButton jBtnRun = new JButton; JButton jBtnExit = new JButton; TitledBorder titledBorder1 = new TitledBorder(""); JLabel jLabel3 = new JLabel; String[] combos = { "Best Matches", "5",        "20",         "100"     };  JComboBox jComboBox1 = new JComboBox(combos); JLabel jLabel4 = new JLabel; JLabel jLabel5 = new JLabel; JLabel jLabel6 = new JLabel; analyser analyser1 = new analyser; public BatchBlastExtractor_Frame { try { setDefaultCloseOperation(EXIT_ON_CLOSE); jbInit; }   catch (Exception exception) { exception.printStackTrace; } }

private void jbInit throws Exception { contentPane = (JPanel) getContentPane; contentPane.setLayout(null); setSize(new Dimension(580, 470)); setTitle("Batch Blast Extractor"); statusBar.setBackground(Color.gray); statusBar.setBorder(BorderFactory.createRaisedBevelBorder); statusBar.setToolTipText(""); statusBar.setText(" Status Bar"); statusBar.setBounds(new Rectangle( -4, 390, 591, 31)); jMenuFile.setText("File"); jMenuFileExit.setText("Exit"); jMenuFileExit.addActionListener(new                                   BatchBlastExtractor_Frame_jMenuFileExit_ActionAdapter(this)); jMenuHelp.setText("Help"); jMenuHelpAbout.setText("About"); jMenuHelpAbout.addActionListener(new                                    BatchBlastExtractor_Frame_jMenuHelpAbout_ActionAdapter(this)); jLabel1.setFont(new java.awt.Font("Dialog", Font.BOLD, 11)); jLabel1.setText("Select Output Directory:"); jLabel1.setBounds(new Rectangle(50, 202, 323, 31)); jTxtFieldInput.setBounds(new Rectangle(49, 68, 336, 24)); jBtnInput.setBounds(new Rectangle(388, 68, 116, 24)); jBtnInput.setText("Browse..."); jBtnInput.addActionListener(new       BatchBlastExtractor_Frame_jBtnInput_actionAdapter(this)); jTxtFieldOutput.setBounds(new Rectangle(50, 236, 336, 24)); jLabel2.setFont(new java.awt.Font("Dialog", Font.BOLD, 11)); jLabel2.setText("Select Input file"); jLabel2.setBounds(new Rectangle(49, 38, 323, 31)); jBtnOutput.setBounds(new Rectangle(389, 236, 116, 24)); jBtnOutput.setText("Browse..."); jBtnOutput.addActionListener(new       BatchBlastExtractor_Frame_jBtnOutput_actionAdapter(this)); jBtnRun.setBounds(new Rectangle(92, 307, 209, 35)); jBtnRun.setText("Run"); jBtnRun.addActionListener(new                             BatchBlastExtractor_Frame_jBtnRun_actionAdapter(this)); jBtnExit.setBounds(new Rectangle(319, 307, 103, 35)); jBtnExit.setText("Exit"); jBtnExit.addActionListener(new                              BatchBlastExtractor_Frame_jBtnExit_actionAdapter(this)); jLabel3.setFont(new java.awt.Font("Dialog", Font.BOLD, 11)); jLabel3.setText("Set the number of matches:"); jLabel3.setBounds(new Rectangle(48, 135, 189, 31)); jComboBox1.setBounds(new Rectangle(214, 141, 151, 24)); jComboBox1.addActionListener(new       BatchBlastExtractor_Frame_jComboBox1_actionAdapter(this)); jLabel4.setBackground(UIManager.getColor("InternalFrame.borderLight")); jLabel4.setForeground(UIManager.getColor("inactiveCaptionText")); jLabel4.setBorder(BorderFactory.createEtchedBorder); jLabel4.setOpaque(true); jLabel4.setBounds(new Rectangle(40, 42, 473, 60)); jLabel5.setBackground(UIManager.getColor("InternalFrame.borderLight")); jLabel5.setForeground(UIManager.getColor("inactiveCaptionText")); jLabel5.setBorder(BorderFactory.createEtchedBorder); jLabel5.setOpaque(true); jLabel5.setBounds(new Rectangle(40, 208, 473, 60)); jLabel6.setBackground(UIManager.getColor("InternalFrame.borderLight")); jLabel6.setBorder(BorderFactory.createEtchedBorder); jLabel6.setOpaque(true); jLabel6.setBounds(new Rectangle(40, 133, 341, 42)); jMenuBar1.add(jMenuFile); jMenuFile.add(jMenuFileExit); jMenuBar1.add(jMenuHelp); jMenuHelp.add(jMenuHelpAbout); setJMenuBar(jMenuBar1); contentPane.add(statusBar, null); contentPane.add(jBtnRun); contentPane.add(jTxtFieldInput); contentPane.add(jLabel2); contentPane.add(jBtnInput); contentPane.add(jLabel3); contentPane.add(jComboBox1); contentPane.add(jTxtFieldOutput); contentPane.add(jLabel1); contentPane.add(jBtnOutput); contentPane.add(jLabel4); contentPane.add(jLabel5); contentPane.add(jLabel6); contentPane.add(jBtnExit); }

void jMenuFileExit_actionPerformed(ActionEvent actionEvent) { System.exit(0); }

void jMenuHelpAbout_actionPerformed(ActionEvent actionEvent) { BatchBlastExtractor_Frame_AboutBox dlg = new BatchBlastExtractor_Frame_AboutBox(this); Dimension dlgSize = dlg.getPreferredSize; Dimension frmSize = getSize; Point loc = getLocation; dlg.setLocation( (frmSize.width - dlgSize.width) / 2 + loc.x,                   (frmSize.height - dlgSize.height) / 2 + loc.y); dlg.setModal(true); dlg.pack; dlg.setVisible(true); }

public void jComboBox1_actionPerformed(ActionEvent e) {

}

public void jBtnExit_actionPerformed(ActionEvent e) { System.exit(0); }

public void jBtnInput_actionPerformed(ActionEvent e) { analyser1.openFileInput; if (analyser1.getOpenFlag == 1) { statusBar.setText(" File Selected"); jTxtFieldInput.setText(analyser1.getfilePath); jTxtFieldOutput.setText(analyser1.getfileOutputPath + "BlastOutput.xls"); }   else statusBar.setText("  Input file has not been selected yet."); }

public void jBtnOutput_actionPerformed(ActionEvent e) { analyser1.setFileOutput; jTxtFieldOutput.setText(analyser1.getfileOutputPath); }

public void jBtnRun_actionPerformed(ActionEvent e) { int comboIndexInt = jComboBox1.getSelectedIndex; int comboIndex = 0; switch(comboIndexInt) { case 0: comboIndex = 1; break; case 1: comboIndex = 5; break; case 2: comboIndex = 20; break; case 3: comboIndex = 25; break; }

analyser1.analyse(jTxtFieldInput.getText,jTxtFieldOutput.getText,comboIndex); } }

class BatchBlastExtractor_Frame_jBtnRun_actionAdapter implements ActionListener { private BatchBlastExtractor_Frame adaptee; BatchBlastExtractor_Frame_jBtnRun_actionAdapter(BatchBlastExtractor_Frame                                                 adaptee) { this.adaptee = adaptee; }

public void actionPerformed(ActionEvent e) { adaptee.jBtnRun_actionPerformed(e); } }

class BatchBlastExtractor_Frame_jBtnOutput_actionAdapter implements ActionListener { private BatchBlastExtractor_Frame adaptee; BatchBlastExtractor_Frame_jBtnOutput_actionAdapter(BatchBlastExtractor_Frame     adaptee) { this.adaptee = adaptee; }

public void actionPerformed(ActionEvent e) { adaptee.jBtnOutput_actionPerformed(e); } }

class BatchBlastExtractor_Frame_jBtnInput_actionAdapter implements ActionListener { private BatchBlastExtractor_Frame adaptee; BatchBlastExtractor_Frame_jBtnInput_actionAdapter(BatchBlastExtractor_Frame     adaptee) { this.adaptee = adaptee; }

public void actionPerformed(ActionEvent e) { adaptee.jBtnInput_actionPerformed(e); } }

class BatchBlastExtractor_Frame_jBtnExit_actionAdapter implements ActionListener { private BatchBlastExtractor_Frame adaptee; BatchBlastExtractor_Frame_jBtnExit_actionAdapter(BatchBlastExtractor_Frame     adaptee) { this.adaptee = adaptee; }

public void actionPerformed(ActionEvent e) { adaptee.jBtnExit_actionPerformed(e); } }

class BatchBlastExtractor_Frame_jComboBox1_actionAdapter implements ActionListener { private BatchBlastExtractor_Frame adaptee; BatchBlastExtractor_Frame_jComboBox1_actionAdapter(BatchBlastExtractor_Frame     adaptee) { this.adaptee = adaptee; }

public void actionPerformed(ActionEvent e) { adaptee.jComboBox1_actionPerformed(e); } }

class BatchBlastExtractor_Frame_jMenuFileExit_ActionAdapter implements ActionListener { BatchBlastExtractor_Frame adaptee;

BatchBlastExtractor_Frame_jMenuFileExit_ActionAdapter(     BatchBlastExtractor_Frame adaptee) { this.adaptee = adaptee; }

public void actionPerformed(ActionEvent actionEvent) { adaptee.jMenuFileExit_actionPerformed(actionEvent); } }

class BatchBlastExtractor_Frame_jMenuHelpAbout_ActionAdapter implements ActionListener { BatchBlastExtractor_Frame adaptee;

BatchBlastExtractor_Frame_jMenuHelpAbout_ActionAdapter(     BatchBlastExtractor_Frame adaptee) { this.adaptee = adaptee; }

public void actionPerformed(ActionEvent actionEvent) { adaptee.jMenuHelpAbout_actionPerformed(actionEvent); } }

-->