import java.io.*; import java.util.*; import org.biojava.bio.symbol.*; import org.biojava.bio.seq.*; import org.biojava.bio.seq.io.*; /** * Class that holds methods for printing final result * data to file and to standard output * @author Gard Thomassen * @see ProgramManager */ public class ResultPrinter{ Vector geneCandidates; int i=0; RNACandidate cand; PrintWriter candOut, fastaOut; SequenceIterator fastaReader; String fileName; int en, to, tre , fire, fem, seks, syv, otte; /** * Constructor for the ResultPrinter * holds one vector with RNA candidates * Creates a target file for the output, and * establishes a printWriter * @param v Vector with the RNACandidates */ public ResultPrinter(Vector v, String n){ fileName = n; geneCandidates = v; try{ candOut = new PrintWriter(new BufferedWriter(new FileWriter("ncRNACandidateGenes.txt"))); } catch(IOException e){ System.err.println("Trouble while creating final output file"); } try{ fastaOut = new PrintWriter(new BufferedWriter(new FileWriter("ncRNAfasta.txt"))); } catch(IOException e){ System.err.println("Trouble while creating FASTA output file"); } } /** * Method that iterates through the RNACandidates * and calls methods to print them to file and std.out. */ public void printAllncRNACandidates(){ candOut.write("Number of ncRNA gene candidates " +geneCandidates.size()+"\n"); while(i < geneCandidates.size()){ cand = (RNACandidate)geneCandidates.elementAt(i); //debug if(cand.promoter_name.equals("sigma 70(Dave)")) en++; else if(cand.promoter_name.equals("sigma 32(Dave)")) to++; else if(cand.promoter_name.equals("sigma 54(Dave)")) tre++; else if(cand.promoter_name.equals("sigma 38(Dave)")) fire++; else if(cand.promoter_name.equals("sigma all")) fem++; else if(cand.promoter_name.equals("sigma_70_regulonDB")) seks++; else if(cand.promoter_name.equals("sigma_32_regulonDB")) syv++; else otte++; printCandidateToFile(cand, (i+1)); //printCandidateToScreen(cand); printCandidateToFASTAfile(cand); i++; } fastaOut.close(); candOut.close(); System.out.println("Located : " + geneCandidates.size() + " ncRNA gene candidates"); //debugging // System.out.println("1= "+ en+ "\n 2= "+to+"\n 3= "+tre+"\n 4= "+fire+"\n 5= "+fem+ "\n 6= "+seks + "\n 7="+syv+ "\n 8= "+otte); } /** * Method that prints all RNACandidates data to file */ protected void printCandidateToFile(RNACandidate c, int n){ //printing gene data of this candidate candOut.write("Gene data for this candidate: \n"); //printing name of sequence wher ecandidate is located candOut.write("Seq name : "+c.sequenceName+"\n"); candOut.write("Candidate number = " + n + "\n"); candOut.write("Candidate length = " +(c.relativeStart - c.finish)+ "\n"); //printing promoter data of this Candidate candOut.write("Promoter data for this candidate: \n"); candOut.write("Promoter name :" + c.promoter_name+"\n"); candOut.write("-35 Threshold =" +c.threshold35+"\n"); candOut.write("-35 score = "+c.score35+"\n"); candOut.write("Distance between -35 and -10 :" + c.distance+"\n"); candOut.write("-10 Threshold =" +c.threshold10+"\n"); candOut.write("-10 score = "+c.score10 +"\n"); candOut.write("Relative start of promoter: " + c.start+"\n"); candOut.write("Relative finish of promoter: " + c.finish+"\n"); candOut.write("-35 region : " + c.seq2 +"\n"); candOut.write("-10 region : " + c.seq1 +"\n"); candOut.write("\n"); //printing terminator data of this candidate candOut.write("Terminator data for this candidate: \n"); candOut.write("Hairpin Energy score: " + c.hairpinEnergy +"\n"); candOut.write("U_tail score : "+ c.u_Score+"\n"); candOut.write("Rightleg = " + c.rightleg +"\n"); candOut.write("Leftleg = "+ c.leftleg +"\n"); candOut.write("Loop =" + c.loop + "\n"); candOut.write("U_tail = " + c.u_sequence +"\n"); candOut.write("Relative start of terminator= " + c.relativeStart +"\n"); candOut.write("Relative end of terminator= " + c.relativeEnd +"\n"); candOut.write("Numbers of nucleotides in terminator = " + (c.relativeEnd - c.relativeStart +1) +"\n"); candOut.write("\n \n"); }//end printCandidatesToFile /** * Method that prints all RNACandidates data to std out. */ protected void printCandidateToScreen(RNACandidate c){ //printing gene data of this candidate System.out.println("Gene data for this candidate: "); //printing name of sequence where candidate is located System.out.println("Seq name :"+c.sequenceName); //printing promoter data of this Candidate System.out.println("Promoter data for this candidate: "); System.out.println("Promoter name :" + c.promoter_name); System.out.println("-35 Threshold =" +c.threshold35); System.out.println("-35 score = "+c.score35); System.out.println("Distance between -35 and -10 :" + c.distance); System.out.println("-10 Threshold =" +c.threshold10); System.out.println("-10 score = "+c.score10 ); System.out.println("Relative start of promoter :" + c.start); System.out.println("Relative finish of promoter:" + c.finish); System.out.println("-35 region : " + c.seq2 +"\n"); System.out.println("-10 region : " + c.seq1 +"\n"); System.out.println("\n"); //printing terminator data of this candidate System.out.println("Terminator data for this candidate: "); System.out.println("Hairpin Energy score: " + c.hairpinEnergy ); System.out.println("U_tail score : "+ c.u_Score); System.out.println("Rightleg = " + c.rightleg ); System.out.println("Leftleg = "+ c.leftleg ); System.out.println("Loop =" + c.loop ); System.out.println("U_tail = " + c.u_sequence ); System.out.println("Relative start of terminator= " + c.relativeStart ); System.out.println("Relative end of terminator= " + c.relativeEnd ); System.out.println("Numbers of nucleotides in terminator = " + (c.relativeEnd - c.relativeStart +1) ); }//end printCandidatesToScreen protected void printCandidateToFASTAfile(RNACandidate c){ int promoterEnd = c.start; int terminatorStart = c.relativeEnd; String name = c.sequenceName; Sequence sequence; Sequence tmpSeq = null; SeqTools seqTool = new SeqTools(); String altName; StringTokenizer tok; int numTok = 0; int s = 0, f = 0; try{ FASTA_Reader fileReader = new FASTA_Reader(); fastaReader = fileReader.readFASTA(fileName); while(fastaReader.hasNext()){ sequence = fastaReader.nextSequence(); if(sequence.getName().equals(name)){ promoterEnd = promoterEnd + c.seq2.length() + c.seq1.length() + c.distance; terminatorStart = terminatorStart - (c.relativeEnd - c.relativeStart); try { tok = new StringTokenizer(sequence.getName(), "_"); numTok = tok.countTokens(); altName = tok.nextToken(); int i = 0; while(i <= (numTok - 4)){ altName = altName.concat("_"); altName = altName.concat(tok.nextToken()); i++; } s = Integer.parseInt(tok.nextToken()); f = Integer.parseInt(tok.nextToken()); fastaOut.write(">"+altName+" "+(s+promoterEnd-1)+" "+(s+terminatorStart)+ "\n"); } catch(Exception e){ fastaOut.write(">"+name+"\n"); } printSeq(sequence, promoterEnd, terminatorStart); } else if(sequence.getName().concat(".reversed").equals(name)){ tmpSeq = seqTool.reverseCompliment(sequence); promoterEnd = promoterEnd + c.seq2.length() + c.seq1.length() + c.distance; terminatorStart = terminatorStart - (c.relativeEnd - c.relativeStart); try{ tok = new StringTokenizer(sequence.getName(), "_"); numTok = tok.countTokens(); altName = tok.nextToken(); int i = 0; while(i <= (numTok - 4)){ altName = altName.concat("_"); altName = altName.concat(tok.nextToken()); i++; } altName = altName.concat(".reversed"); s = Integer.parseInt(tok.nextToken()); tok = new StringTokenizer(tok.nextToken(), "."); f = Integer.parseInt(tok.nextToken()); fastaOut.write(">"+altName+" "+(f-terminatorStart-1)+" "+(f-promoterEnd+2)+ "\n"); } catch(Exception e){ fastaOut.write(">"+name+"\n"); } //System.out.println(s +" " + f); //System.out.println(promoterEnd +"\t"+ terminatorStart); printSeq(tmpSeq, promoterEnd, terminatorStart); } } } catch(Exception e){ System.out.println("Something wrong while printing result FASTA file" ); System.out.println("name ="+ name +" pstart = " + promoterEnd + " tEnd = "+ terminatorStart); } } protected void printSeq(Sequence seq, int pStart, int tEnd){ int z = 59; boolean more = true; while(more){ if((pStart + z)< tEnd ) { fastaOut.write(seq.subStr(pStart, (pStart + z))+"\n"); pStart += 60; } else{ more = false; fastaOut.write(seq.subStr(pStart, tEnd)+"\n"); } } } }//end class ResultPrinter