package stream; import java.io.InputStream; import java.util.ArrayList; import java.util.Arrays; import dna.Gene; import fileIO.FileFormat; import fileIO.ReadWrite; import shared.KillSwitch; import shared.Shared; import shared.Timer; import shared.Tools; /** * @author Brian Bushnell * @date Feb 13, 2013 * */ public class FastaReadInputStream extends ReadInputStream { public static void main(String[] args){ int a=20, b=Integer.MAX_VALUE; if(args.length>1){a=Integer.parseInt(args[1]);} if(args.length>2){b=Integer.parseInt(args[2]);} if(args.length>3){MIN_READ_LEN=Integer.parseInt(args[3]);} if(args.length>4){TARGET_READ_LEN=Integer.parseInt(args[4]);} if(TARGET_READ_LEN<1){ TARGET_READ_LEN=Integer.MAX_VALUE; SPLIT_READS=false; }else{ SPLIT_READS=true; } Timer t=new Timer(); FastaReadInputStream fris=new FastaReadInputStream(args[0], false, false, false, Shared.bufferData()); Read r=fris.next(); int i=0; while(r!=null){ if(i=a){break;} } while(r!=null && i++0 ? TARGET_READ_LEN : Integer.MAX_VALUE) : Integer.MAX_VALUE); MAX_DATA=maxdata>0 ? maxdata : Shared.bufferData(); ins=open(); assert(settingsOK()); } @Override public Read next() { if(!hasMore()){ if(verbose){System.err.println("hasMore() returned false; currentList="+ (currentList==null ? null : currentList.size())+", nextReadIndex="+nextReadIndex+", consumed="+consumed);} return null; } Read r=currentList.set(nextReadIndex, null); nextReadIndex++; consumed++; return r; } @Override public ArrayList nextList() { if(nextReadIndex!=0){throw new RuntimeException("'next' should not be used when doing blockwise access.");} if(currentList==null || nextReadIndex>=currentList.size()){ boolean b=fillList(); } ArrayList list=currentList; currentList=null; if(list==null || list.isEmpty()){ list=null; }else{ consumed+=list.size(); } return list; } @Override public boolean hasMore() { if(currentList==null || nextReadIndex>=currentList.size()){ if(open){ fillList(); }else{ // assert(generated>0) : "Was the file empty?"; } } return (currentList!=null && nextReadIndex=currentList.size()); nextReadIndex=0; currentList=new ArrayList(BUF_LEN); if(header==null){ header=nextHeader(); if(header==null){ currentList=null; return false; } } long len=0; for(int i=0; i0; } private final Read generateRead(int pairnum){ if(verbose){System.err.println("Called generateRead(); bstart="+bstart+", bstop="+bstop+", currentSection="+currentSection+", header="+header);} assert(header!=null) : "Null header for fasta read - input file may be corrupt: "+name; if(bstart0); byte[] quals=null; if(FAKE_QUALITY){ quals=new byte[bases.length]; Arrays.fill(quals, (byte)(Shared.FAKE_QUAL)); } // String hd=((currentSection==1 && !hitmax) ? header : header+"_"+currentSection); String hd=((!FORCE_SECTION_NAME && currentSection==1 && bases.length<=maxLen) ? header : header+"_part_"+currentSection); // assert(false) : FORCE_SECTION_NAME+", "+(currentSection==1)+", "+(bases.length<=maxLen)+", "+bases.length+", "+maxLen; assert(currentSection==1 || bases.length>0) : "id="+hd+", section="+currentSection+", len="+bases.length+"\n"+new String(bases); Read r=null; if(FASTQ.PARSE_CUSTOM){//TODO: This appears to be legacy code from old custom header format, which won't work anymore assert(false) : "TODO - this code for parsing custom headers of fasta files is obsolete."; if(header!=null && header.indexOf('_')>0){ String temp=header; if(temp.endsWith(" /1") || temp.endsWith(" /2")){temp=temp.substring(0, temp.length()-3);} String[] answer=temp.split("_"); if(answer.length>=5){ try { int trueChrom=Gene.toChromosome(answer[1]); byte trueStrand=Byte.parseByte(answer[2]); int trueLoc=Integer.parseInt(answer[3]); int trueStop=Integer.parseInt(answer[4]); // r=new Read(bases, trueChrom, trueStrand, trueLoc, trueStop, hd, quals, nextReadID); r=new Read(bases, quals, hd, nextReadID, (flag|trueStrand), trueChrom, trueLoc, trueStop); r.setSynthetic(true); } catch (NumberFormatException e) { FASTQ.PARSE_CUSTOM=false; System.err.println("Turned off PARSE_CUSTOM because could not parse "+new String(header)); } }else{ FASTQ.PARSE_CUSTOM=false; System.err.println("Turned off PARSE_CUSTOM because answer="+Arrays.toString(answer)); } }else{ FASTQ.PARSE_CUSTOM=false; System.err.println("Turned off PARSE_CUSTOM because header="+header+", index="+header.indexOf('_')); } } if(r==null){ r=new Read(bases, quals, hd, nextReadID, flag); } r.setPairnum(pairnum); if(verbose){System.err.println("Made read:\t"+(r.length()>1000 ? r.id : r.toString()));} return r; } private String nextHeader(){ if(verbose){System.err.println("Called nextHeader(); bstart="+bstart+"; bstop="+bstop);} assert(bstart>=bstop || buffer[bstart]=='>' || buffer[bstart]<=slashr) : bstart+", "+bstop+", '"+(char)buffer[bstart]+"'"+"\t"+name; while(bstart=bstop || buffer[x]=='>') : bstart+", "+bstop+", '"+(char)buffer[x]+"'"; while(xslashr){x++;} if(verbose){System.err.println("A: x="+x);} if(xslashr || buffer[x]<=STX || buffer[x]==tab)){ if(buffer[x]==0x1){buffer[x]=carrot;} x++; } } if(verbose){System.err.println("B: x="+x);} if(x>=bstop){ int fb=fillBuffer(); if(verbose){System.err.println("B: fb="+fb);} if(fb<1){ if(verbose){System.err.println("Returning null from nextHeader()");} return null; } x=0; assert(bstart==0 && bstart') : "Improperly formatted fasta file; expecting '>' symbol.\n"+ (buffer[x]=='@' ? "If this is a fastq file, please rename it with a '.fastq' extension.\n" : "")+ bstart+", "+bstop+", "+(int)buffer[x]+", "+(char)buffer[x]; //Note: This assertion will fire if a fasta file starts with a newline. while(xslashr){x++;} if(xslashr || buffer[x]<=STX || buffer[x]==tab)){ if(buffer[x]==0x1){buffer[x]=carrot;} x++; } } } if(verbose){System.err.println("C: x="+x);} assert(x>=bstop || buffer[x]<=slashr); int start=bstart+1, stop=x; if(Shared.TRIM_READ_COMMENTS){ for(int i=start; istart ? new String(buffer, start, stop-start) : ""; // String s=new String(buffer, bstart+1, x-(bstart+1)); if(verbose){System.err.println("Fetched header: '"+s+"'");} bstart=x+1; return s; } private byte[] nextBases(){ if(verbose){System.err.println("Called nextBases()");} // assert(open) : "Attempting to read from a closed file. Current header: "+header; if(bstart>=bstop){ int bytes=fillBuffer(); if(bytes<1 || !open){return null;} } int x=bstart; int bases=0; if(!(x>=bstop || buffer[x]!='>')){ handleNoSequence(x); } // assert(x>=bstop || buffer[x]!='>') : // "A fasta header with no sequence was encountered. To discard such headers, please re-run with the -da flag."; //"\n"+new String(buffer, 0, Tools.min(x+1, buffer.length))+"\n"; while(xslashr){bases++;} x++; } assert(x==bstop || buffer[x]=='>' || bases==maxLen); if(x==bstop && bases1000 ? "*LONG*" : new String(r))+"'");} bstart=x; return r; } private void handleNoSequence(int x){ if(currentSection>0){return;}//This section is spuriously entered for reads that are a multiple of the target read length when splitting. if(WARN_IF_NO_SEQUENCE){ synchronized(getClass()){ if(reportedHeader==header){return;} reportedHeader=header; System.err.println("Warning: A fasta header with no sequence was encountered ("+currentSection+"):\n"+header+"\n"); if(WARN_FIRST_TIME_ONLY){WARN_IF_NO_SEQUENCE=false;} } } assert(!ABORT_IF_NO_SEQUENCE) : "\n"+new String(buffer, 0, Tools.min(x+1, buffer.length))+"\n"; } /** Fills buffer. Ensures that result will extend to the next caret or EOF. Returns number of bytes filled. */ private final int fillBuffer(){ // assert(open); if(!open){return 0;} if(verbose){System.err.println("fillBuffer() : bstart="+bstart+", bstop="+bstop);} if(bstart0){ // assert(bstart>0) : bstart+", "+bstop+", "+new String(buffer); int extra=bstop-bstart; for(int i=0; i=Integer.MAX_VALUE-1){ throw new RuntimeException("Minimum FASTA read length is too long: "+MIN_READ_LEN); } if(MIN_READ_LEN<1){ throw new RuntimeException("Minimum FASTA read length is too short: "+MIN_READ_LEN); } if(SPLIT_READS){ if(TARGET_READ_LEN<1){ throw new RuntimeException("Target FASTA read length is too short: "+TARGET_READ_LEN); } if(MIN_READ_LEN>TARGET_READ_LEN){ throw new RuntimeException("Minimum FASTA read length is longer than maximum read length: "+MIN_READ_LEN+">"+TARGET_READ_LEN); } } if(MIN_READ_LEN>=Integer.MAX_VALUE-1 || MIN_READ_LEN<1){return false;} if(SPLIT_READS && (TARGET_READ_LEN<1 || MIN_READ_LEN>TARGET_READ_LEN)){return false;} return true; } @Override public String fname(){return name;} public final String name; private ArrayList currentList=null; private String header=null; private String reportedHeader=null; private boolean open=false; private byte[] buffer=new byte[16384]; private int bstart=0, bstop=0; public InputStream ins; private long consumed=0; private long nextReadID=0; private int nextReadIndex=0; private int currentSection=0; public final boolean allowSubprocess; public final boolean interleaved; public final boolean amino; public final int flag; private final int BUF_LEN=Shared.bufferLen(); private final long MAX_DATA; private final int maxLen, minLen; public static boolean verbose=false; private static final byte slashr='\r', slashn='\n', carrot='>', space=' ', tab='\t', SOH=0x1, STX=0x2; public static boolean SPLIT_READS=false; public static int TARGET_READ_LEN=500; public static int MIN_READ_LEN=1; public static boolean FAKE_QUALITY=false; public static boolean FORCE_SECTION_NAME=false; public static boolean WARN_IF_NO_SEQUENCE=true; public static boolean WARN_FIRST_TIME_ONLY=true; public static boolean ABORT_IF_NO_SEQUENCE=false; }