package stream; import java.io.File; import java.util.ArrayList; import align2.ListNum; import align2.Shared; import align2.Tools; import fileIO.FileFormat; import fileIO.ReadWrite; import fileIO.TextFile; /** * This class allows multiple files as input. * These files are synchronized, so a read will be created by merging the sitescores from the same line of each file. * @author Brian Bushnell * @date Jul 16, 2013 * */ public class RTextInputStream extends ReadInputStream { public static void main(String[] args){ RTextInputStream rtis=new RTextInputStream(args, 0); ArrayList list=rtis.nextList(); while(list!=null){ for(Read r : list){ System.out.println(r.toText(true)); } list=rtis.nextList(); } } public RTextInputStream(FileFormat ff1, FileFormat ff2, long crisReadLimit){ this(ff1.name(), (ff2==null ? null : ff2.name()), crisReadLimit); } public RTextInputStream(String fname1, String fname2, long crisReadLimit){ this(new String[] {fname1}, (fname2==null || "null".equalsIgnoreCase(fname2)) ? null : new String[] {fname2}, crisReadLimit); assert(fname2==null || !fname1.equals(fname2)) : "Error - input files have same name."; } public RTextInputStream(String[] fnames_, long crisReadLimit){this(fnames_, null, crisReadLimit);} public RTextInputStream(String[] fnames_, String[] mate_fnames_, long crisReadLimit){ fnames=fnames_; textfiles=new TextFile[fnames.length]; for(int i=0; i list=readList(); // if(list==null || list.size()==0){return null;} // return list.toArray(new Read[list.size()]); // } @Override public synchronized ArrayList nextList(){ // System.out.println((mateStream==null ? "F5: " : "F3: ")+"Grabbing a list: finished="+finished); if(finished){return null;} return readList(); } public final boolean preferArrays(){return false;} private synchronized ArrayList readList(){ assert(buffer==null); // System.out.println((mateStream==null ? "F5: " : "F3: ")+" Entering readList"); if(finished){return null;} ArrayList merged=getListFromFile(textfiles[0]); if(textfiles.length>1){ ArrayList[] temp=new ArrayList[textfiles.length]; temp[0]=merged; for(int i=0; i getListFromFile(TextFile tf){ int len=READS_PER_LIST; if(readLimit-readCount list=new ArrayList(len); for(int i=0; i0 == r.mapScore>0) : r.toText(false); if(interleaved){ s=tf.nextLine(); assert(s!=null) : "Odd number of reads in interleaved file "+tf.name; if(s!=null){ Read r2=Read.fromText(s); assert(r2.numericID==r.numericID) : "Different numeric IDs for paired reads in interleaved file "+tf.name; r2.numericID=r.numericID; r2.mate=r; r.mate=r2; } } list.add(r); } readCount+=list.size(); if(list.size() buffer=null; private int next=0; private long readCount; private final long readLimit; private final boolean interleaved; public static final int READS_PER_LIST=Shared.READ_BUFFER_LENGTH; private final RTextInputStream mateStream; private final ConcurrentReadInputStream cris; public static boolean USE_CRIS=true; //Doubles read speed for zipped paired files @Override /** This is optimistic and may return "true" incorrectly. */ public boolean hasMore() { if(buffer!=null && next=buffer.size()){ buffer=null; next=0; if(!finished){ buffer=nextList(); } } if(buffer==null || next>=buffer.size()){ assert(finished); return null; } Read r=buffer.get(next); buffer.set(next, null); next++; return r; } @Override public synchronized void restart() { finished=false; next=0; buffer=null; for(TextFile tf : textfiles){tf.reset();} if(cris!=null){ cris.restart(); new Thread(cris).start(); }else if(mateStream!=null){mateStream.restart();} } @Override public synchronized boolean close() { boolean error=false; for(TextFile tf : textfiles){error|=tf.close();} if(cris!=null){ error|=ReadWrite.closeStream(cris);; }else if(mateStream!=null){ mateStream.close(); error|=mateStream.errorState(); } return error; } }