| 1 |
package stream; |
| 2 |
|
| 3 |
import java.io.File; |
| 4 |
import java.util.ArrayList; |
| 5 |
import java.util.Arrays; |
| 6 |
|
| 7 |
import fileIO.FileFormat; |
| 8 |
import fileIO.ReadWrite; |
| 9 |
import fileIO.TextFile; |
| 10 |
import shared.Shared; |
| 11 |
import shared.Tools; |
| 12 |
import structures.ListNum; |
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
|
| 19 |
|
| 20 |
|
| 21 |
public class RTextInputStream extends ReadInputStream { |
| 22 |
|
| 23 |
public static void main(String[] args){ |
| 24 |
RTextInputStream rtis=new RTextInputStream(args, 0); |
| 25 |
ArrayList<Read> list=rtis.nextList(); |
| 26 |
while(list!=null){ |
| 27 |
for(Read r : list){ |
| 28 |
System.out.println(r.toText(true)); |
| 29 |
} |
| 30 |
list=rtis.nextList(); |
| 31 |
} |
| 32 |
} |
| 33 |
|
| 34 |
public RTextInputStream(FileFormat ff1, FileFormat ff2, long crisReadLimit){ |
| 35 |
this(ff1.name(), (ff2==null ? null : ff2.name()), crisReadLimit); |
| 36 |
} |
| 37 |
|
| 38 |
public RTextInputStream(String fname1, String fname2, long crisReadLimit){ |
| 39 |
this(new String[] {fname1}, (fname2==null || "null".equalsIgnoreCase(fname2)) ? null : new String[] {fname2}, crisReadLimit); |
| 40 |
assert(fname2==null || !fname1.equals(fname2)) : "Error - input files have same name."; |
| 41 |
} |
| 42 |
public RTextInputStream(String[] fnames_, long crisReadLimit){this(fnames_, null, crisReadLimit);} |
| 43 |
|
| 44 |
public RTextInputStream(String[] fnames_, String[] mate_fnames_, long crisReadLimit){ |
| 45 |
fnames=fnames_; |
| 46 |
textfiles=new TextFile[fnames.length]; |
| 47 |
for(int i=0; i<textfiles.length; i++){ |
| 48 |
textfiles[i]=new TextFile(fnames[i], true); |
| 49 |
} |
| 50 |
|
| 51 |
readLimit=(crisReadLimit<0 ? Long.MAX_VALUE : crisReadLimit); |
| 52 |
if(readLimit==0){ |
| 53 |
System.err.println("Warning - created a read stream for 0 reads."); |
| 54 |
assert(false); |
| 55 |
} |
| 56 |
interleaved=(mate_fnames_!=null ? false : |
| 57 |
(!FASTQ.TEST_INTERLEAVED || textfiles[0].is==System.in) ? FASTQ.FORCE_INTERLEAVED : isInterleaved(fnames[0])); |
| 58 |
|
| 59 |
|
| 60 |
|
| 61 |
mateStream=(mate_fnames_==null ? null : new RTextInputStream(mate_fnames_, null, crisReadLimit)); |
| 62 |
cris=((!USE_CRIS || mateStream==null) ? null : new ConcurrentLegacyReadInputStream(mateStream, crisReadLimit)); |
| 63 |
if(cris!=null){cris.start();} |
| 64 |
} |
| 65 |
|
| 66 |
public static boolean isInterleaved(String fname){ |
| 67 |
File f=new File(fname); |
| 68 |
assert(f.exists() && f.isFile()); |
| 69 |
TextFile tf=new TextFile(fname, false); |
| 70 |
String s=tf.nextLine(); |
| 71 |
tf.close(); |
| 72 |
return "#INTERLEAVED".equals(s); |
| 73 |
} |
| 74 |
|
| 75 |
@Override |
| 76 |
public void start() { |
| 77 |
assert(false); |
| 78 |
if(cris!=null){cris.start();} |
| 79 |
} |
| 80 |
|
| 81 |
|
| 82 |
|
| 83 |
|
| 84 |
|
| 85 |
|
| 86 |
|
| 87 |
|
| 88 |
@Override |
| 89 |
public synchronized ArrayList<Read> nextList(){ |
| 90 |
|
| 91 |
if(finished){return null;} |
| 92 |
return readList(); |
| 93 |
} |
| 94 |
|
| 95 |
private synchronized ArrayList<Read> readList(){ |
| 96 |
assert(buffer==null); |
| 97 |
|
| 98 |
if(finished){return null;} |
| 99 |
|
| 100 |
ArrayList<Read> merged=getListFromFile(textfiles[0]); |
| 101 |
|
| 102 |
if(textfiles.length>1){ |
| 103 |
ArrayList<Read>[] temp=new ArrayList[textfiles.length]; |
| 104 |
temp[0]=merged; |
| 105 |
for(int i=0; i<temp.length; i++){ |
| 106 |
temp[i]=getListFromFile(textfiles[i]); |
| 107 |
} |
| 108 |
|
| 109 |
for(int i=0; i<merged.size(); i++){ |
| 110 |
Read r=merged.get(i); |
| 111 |
for(int j=1; j<temp.length; j++){ |
| 112 |
Read r2=temp[j].get(i); |
| 113 |
assert(r2.numericID==r.numericID); |
| 114 |
assert(r2.id.equals(r.id)); |
| 115 |
if(r.sites==null){r.sites=r2.sites;} |
| 116 |
else if(r2.sites!=null){r.sites.addAll(r2.sites);} |
| 117 |
} |
| 118 |
} |
| 119 |
} |
| 120 |
|
| 121 |
|
| 122 |
|
| 123 |
if(cris!=null){ |
| 124 |
|
| 125 |
ListNum<Read> mates0=cris.nextList(); |
| 126 |
ArrayList<Read> mates=mates0.list; |
| 127 |
assert((mates==null || mates.size()==0) == (merged==null || merged.size()==0)) : (merged==null)+", "+(mates==null); |
| 128 |
if(merged!=null && mates!=null){ |
| 129 |
|
| 130 |
assert(mates.size()==merged.size()) : "\n"+mates.size()+", "+merged.size()+", "+paired()+"\n"+ |
| 131 |
merged.get(0).toText(false)+"\n"+mates.get(0).toText(false)+"\n\n"+ |
| 132 |
merged.get(merged.size()-1).toText(false)+"\n"+mates.get(mates.size()-1).toText(false)+"\n\n"+ |
| 133 |
merged.get(Tools.min(merged.size(), mates.size())-1).toText(false)+"\n"+ |
| 134 |
mates.get(Tools.min(merged.size(), mates.size())-1).toText(false)+"\n\n"; |
| 135 |
|
| 136 |
for(int i=0; i<merged.size(); i++){ |
| 137 |
Read r1=merged.get(i); |
| 138 |
Read r2=mates.get(i); |
| 139 |
r1.mate=r2; |
| 140 |
assert(r1.pairnum()==0); |
| 141 |
|
| 142 |
if(r2!=null){ |
| 143 |
r2.mate=r1; |
| 144 |
r2.setPairnum(1); |
| 145 |
assert(r2.numericID==r1.numericID) : "\n\n"+r1.toText(false)+"\n\n"+r2.toText(false)+"\n"; |
| 146 |
|
| 147 |
} |
| 148 |
|
| 149 |
} |
| 150 |
} |
| 151 |
cris.returnList(mates0.id, mates0.list.isEmpty()); |
| 152 |
}else if(mateStream!=null){ |
| 153 |
|
| 154 |
ArrayList<Read> mates=mateStream.readList(); |
| 155 |
assert((mates==null || mates.size()==0) == (merged==null || merged.size()==0)) : (merged==null)+", "+(mates==null); |
| 156 |
if(merged!=null && mates!=null){ |
| 157 |
assert(mates.size()==merged.size()) : mates.size()+", "+merged.size(); |
| 158 |
|
| 159 |
for(int i=0; i<merged.size(); i++){ |
| 160 |
Read r1=merged.get(i); |
| 161 |
Read r2=mates.get(i); |
| 162 |
r1.mate=r2; |
| 163 |
r2.mate=r1; |
| 164 |
|
| 165 |
assert(r1.pairnum()==0); |
| 166 |
r2.setPairnum(1); |
| 167 |
|
| 168 |
assert(r2.numericID==r1.numericID) : "\n\n"+r1.toText(false)+"\n\n"+r2.toText(false)+"\n"; |
| 169 |
assert(r2.id.equals(r1.id)) : "\n\n"+r1.toText(false)+"\n\n"+r2.toText(false)+"\n"; |
| 170 |
} |
| 171 |
} |
| 172 |
} |
| 173 |
|
| 174 |
if(merged.size()<READS_PER_LIST){ |
| 175 |
if(merged.size()==0){merged=null;} |
| 176 |
shutdown(); |
| 177 |
} |
| 178 |
|
| 179 |
return merged; |
| 180 |
} |
| 181 |
|
| 182 |
private ArrayList<Read> getListFromFile(TextFile tf){ |
| 183 |
|
| 184 |
int len=READS_PER_LIST; |
| 185 |
if(readLimit-readCount<len){len=(int)(readLimit-readCount);} |
| 186 |
|
| 187 |
ArrayList<Read> list=new ArrayList<Read>(len); |
| 188 |
|
| 189 |
for(int i=0; i<len; i++){ |
| 190 |
String s=tf.nextLine(); |
| 191 |
while(s!=null && s.charAt(0)=='#'){s=tf.nextLine();} |
| 192 |
if(s==null){break;} |
| 193 |
Read r=Read.fromText(s); |
| 194 |
|
| 195 |
|
| 196 |
if(interleaved){ |
| 197 |
s=tf.nextLine(); |
| 198 |
assert(s!=null) : "Odd number of reads in interleaved file "+tf.name; |
| 199 |
if(s!=null){ |
| 200 |
Read r2=Read.fromText(s); |
| 201 |
assert(r2.numericID==r.numericID) : "Different numeric IDs for paired reads in interleaved file "+tf.name; |
| 202 |
r2.numericID=r.numericID; |
| 203 |
r2.mate=r; |
| 204 |
r.mate=r2; |
| 205 |
} |
| 206 |
} |
| 207 |
list.add(r); |
| 208 |
} |
| 209 |
readCount+=list.size(); |
| 210 |
|
| 211 |
if(list.size()<len){ |
| 212 |
assert(tf.nextLine()==null); |
| 213 |
tf.close(); |
| 214 |
} |
| 215 |
return list; |
| 216 |
} |
| 217 |
|
| 218 |
@Override |
| 219 |
public boolean paired() { |
| 220 |
return mateStream!=null || interleaved; |
| 221 |
} |
| 222 |
|
| 223 |
public final void shutdown(){ |
| 224 |
finished=true; |
| 225 |
if(mateStream!=null){mateStream.shutdown();} |
| 226 |
if(cris!=null){cris.shutdown();} |
| 227 |
} |
| 228 |
|
| 229 |
@Override |
| 230 |
public String fname(){return Arrays.toString(fnames);} |
| 231 |
|
| 232 |
public boolean finished=false; |
| 233 |
public String[] fnames; |
| 234 |
public TextFile[] textfiles; |
| 235 |
|
| 236 |
private ArrayList<Read> buffer=null; |
| 237 |
private int next=0; |
| 238 |
|
| 239 |
private long readCount; |
| 240 |
private final long readLimit; |
| 241 |
private final boolean interleaved; |
| 242 |
|
| 243 |
public static final int READS_PER_LIST=Shared.bufferLen();; |
| 244 |
|
| 245 |
private final RTextInputStream mateStream; |
| 246 |
private final ConcurrentLegacyReadInputStream cris; |
| 247 |
public static boolean USE_CRIS=true; |
| 248 |
|
| 249 |
@Override |
| 250 |
|
| 251 |
public boolean hasMore() { |
| 252 |
if(buffer!=null && next<buffer.size()){return true;} |
| 253 |
return !finished; |
| 254 |
} |
| 255 |
|
| 256 |
|
| 257 |
@Override |
| 258 |
|
| 259 |
public Read next() { |
| 260 |
if(buffer==null || next>=buffer.size()){ |
| 261 |
buffer=null; |
| 262 |
next=0; |
| 263 |
if(!finished){ |
| 264 |
buffer=nextList(); |
| 265 |
} |
| 266 |
} |
| 267 |
|
| 268 |
if(buffer==null || next>=buffer.size()){ |
| 269 |
assert(finished); |
| 270 |
return null; |
| 271 |
} |
| 272 |
Read r=buffer.get(next); |
| 273 |
buffer.set(next, null); |
| 274 |
next++; |
| 275 |
return r; |
| 276 |
} |
| 277 |
|
| 278 |
|
| 279 |
@Override |
| 280 |
public synchronized void restart() { |
| 281 |
finished=false; |
| 282 |
next=0; |
| 283 |
buffer=null; |
| 284 |
for(TextFile tf : textfiles){tf.reset();} |
| 285 |
if(cris!=null){ |
| 286 |
cris.restart(); |
| 287 |
cris.start(); |
| 288 |
}else if(mateStream!=null){mateStream.restart();} |
| 289 |
} |
| 290 |
|
| 291 |
@Override |
| 292 |
public synchronized boolean close() { |
| 293 |
boolean error=false; |
| 294 |
for(TextFile tf : textfiles){error|=tf.close();} |
| 295 |
if(cris!=null){ |
| 296 |
error|=ReadWrite.closeStream(cris);; |
| 297 |
}else if(mateStream!=null){ |
| 298 |
mateStream.close(); |
| 299 |
error|=mateStream.errorState(); |
| 300 |
} |
| 301 |
return error; |
| 302 |
} |
| 303 |
|
| 304 |
} |