| 1 |
package stream; |
| 2 |
|
| 3 |
import java.util.ArrayList; |
| 4 |
|
| 5 |
import fileIO.FileFormat; |
| 6 |
import fileIO.ReadWrite; |
| 7 |
import shared.Tools; |
| 8 |
import structures.ListNum; |
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
public class DualCris extends ConcurrentReadInputStream { |
| 16 |
|
| 17 |
public static void main(String[] args){ |
| 18 |
String a=args[0]; |
| 19 |
String b=args.length>1 ? args[1] : null; |
| 20 |
FileFormat ff1=FileFormat.testInput(a, null, false); |
| 21 |
FileFormat ff2=(b==null ? null : FileFormat.testInput(b, null, false)); |
| 22 |
DualCris cris=getReadInputStream(-1, false, ff1, ff2, null, null); |
| 23 |
cris.start(); |
| 24 |
|
| 25 |
ListNum<Read> ln=cris.nextList(); |
| 26 |
ArrayList<Read> reads=ln.list; |
| 27 |
|
| 28 |
boolean foundR1=false, foundR2=false; |
| 29 |
while(ln!=null && reads!=null && reads.size()>0){ |
| 30 |
for(Read r1 : reads){ |
| 31 |
Read r2=r1.mate; |
| 32 |
if(r1.pairnum()==0){foundR1=true;} |
| 33 |
else{foundR2=true;} |
| 34 |
if(r2!=null){ |
| 35 |
if(r2.pairnum()==0){foundR1=true;} |
| 36 |
else{foundR2=true;} |
| 37 |
} |
| 38 |
} |
| 39 |
|
| 40 |
System.err.print(ln.id); |
| 41 |
|
| 42 |
cris.returnList(ln.id, foundR1, foundR2); |
| 43 |
foundR1=foundR2=false; |
| 44 |
ln=cris.nextList(); |
| 45 |
reads=(ln!=null ? ln.list : null); |
| 46 |
System.err.print(","); |
| 47 |
} |
| 48 |
System.err.print("Finished."); |
| 49 |
cris.returnList(ln.id, foundR1, foundR2); |
| 50 |
ReadWrite.closeStreams(cris); |
| 51 |
} |
| 52 |
|
| 53 |
public static DualCris getReadInputStream(long maxReads, boolean keepSamHeader, |
| 54 |
FileFormat ff1, FileFormat ff2, String qf1, String qf2){ |
| 55 |
ConcurrentReadInputStream cris1=(ff1==null ? null : ConcurrentReadInputStream.getReadInputStream(maxReads, keepSamHeader, ff1, null, qf1, null)); |
| 56 |
ConcurrentReadInputStream cris2=(ff2==null ? null : ConcurrentReadInputStream.getReadInputStream(maxReads, keepSamHeader, ff2, null, qf2, null)); |
| 57 |
return new DualCris(cris1, cris2); |
| 58 |
} |
| 59 |
|
| 60 |
public DualCris(ConcurrentReadInputStream cris1_, ConcurrentReadInputStream cris2_){ |
| 61 |
super((cris1_==null ? "null" : cris1_.fname())+(cris2_==null ? "null" : ","+cris2_.fname())); |
| 62 |
cris1=cris1_; |
| 63 |
cris2=cris2_; |
| 64 |
} |
| 65 |
|
| 66 |
private final ConcurrentReadInputStream cris1; |
| 67 |
private final ConcurrentReadInputStream cris2; |
| 68 |
private boolean cris1Active, cris2Active; |
| 69 |
private boolean errorState=false; |
| 70 |
private boolean verbose=false; |
| 71 |
|
| 72 |
@Override |
| 73 |
public ListNum<Read> nextList() { |
| 74 |
|
| 75 |
ListNum<Read> ln1=null, ln2=null; |
| 76 |
if(cris1Active && cris1!=null){ |
| 77 |
ln1=cris1.nextList(); |
| 78 |
if(ln1==null){ |
| 79 |
synchronized(this){ |
| 80 |
cris1Active=false; |
| 81 |
System.err.println("\nSet cris1Active="+cris1Active); |
| 82 |
} |
| 83 |
} |
| 84 |
} |
| 85 |
if(cris2Active && cris2!=null){ |
| 86 |
ln2=cris2.nextList(); |
| 87 |
if(ln2!=null){ |
| 88 |
for(Read r : ln2.list){r.setPairnum(1);} |
| 89 |
}else{ |
| 90 |
synchronized(this){ |
| 91 |
cris2Active=false; |
| 92 |
System.err.println("\nSet cris2Active="+cris2Active); |
| 93 |
} |
| 94 |
} |
| 95 |
} |
| 96 |
|
| 97 |
if(ln1!=null && ln2!=null){ |
| 98 |
final int size1=ln1.size(), size2=ln2.size(); |
| 99 |
final int min=Tools.min(size1, size2); |
| 100 |
for(int i=0; i<min; i++){ |
| 101 |
Read r1=ln1.get(i); |
| 102 |
Read r2=ln2.get(i); |
| 103 |
r1.mate=r2; |
| 104 |
r2.mate=r1; |
| 105 |
} |
| 106 |
if(size2>size1){ |
| 107 |
for(int i=size1; i<size2; i++){ |
| 108 |
ln1.add(ln2.get(i)); |
| 109 |
} |
| 110 |
} |
| 111 |
}else if(ln2!=null){ |
| 112 |
ln1=ln2; |
| 113 |
} |
| 114 |
|
| 115 |
return ln1; |
| 116 |
} |
| 117 |
|
| 118 |
@Override |
| 119 |
public void returnList(long listNum, boolean poison) { |
| 120 |
throw new RuntimeException("Unsupported."); |
| 121 |
} |
| 122 |
|
| 123 |
public void returnList(long listNum, boolean foundR1, boolean foundR2) { |
| 124 |
if(cris1!=null && cris1Active){ |
| 125 |
cris1.returnList(listNum, !foundR1); |
| 126 |
if(!foundR1){cris1Active=false;} |
| 127 |
} |
| 128 |
if(cris2!=null && cris2Active){ |
| 129 |
cris2.returnList(listNum, !foundR2); |
| 130 |
if(!foundR2){cris2Active=false;} |
| 131 |
} |
| 132 |
} |
| 133 |
|
| 134 |
@Override |
| 135 |
public void start() { |
| 136 |
started=true; |
| 137 |
if(cris1!=null){ |
| 138 |
cris1.start(); |
| 139 |
cris1Active=true; |
| 140 |
} |
| 141 |
if(cris2!=null){ |
| 142 |
cris2.start(); |
| 143 |
cris2Active=true; |
| 144 |
} |
| 145 |
} |
| 146 |
|
| 147 |
@Override |
| 148 |
public void run() {assert(false);} |
| 149 |
|
| 150 |
@Override |
| 151 |
public void shutdown() { |
| 152 |
if(cris1!=null){cris1.shutdown();} |
| 153 |
if(cris2!=null){cris2.shutdown();} |
| 154 |
cris1Active=cris2Active=false; |
| 155 |
} |
| 156 |
|
| 157 |
@Override |
| 158 |
public void restart() { |
| 159 |
if(cris1!=null){ |
| 160 |
cris1.restart(); |
| 161 |
cris1Active=true; |
| 162 |
} |
| 163 |
if(cris2!=null){ |
| 164 |
cris2.restart(); |
| 165 |
cris2Active=true; |
| 166 |
} |
| 167 |
} |
| 168 |
|
| 169 |
@Override |
| 170 |
public void close() { |
| 171 |
if(cris1!=null){cris1.close();} |
| 172 |
if(cris2!=null){cris2.close();} |
| 173 |
cris1Active=cris2Active=false; |
| 174 |
} |
| 175 |
|
| 176 |
@Override |
| 177 |
public boolean paired() { |
| 178 |
assert(cris1!=null || cris2!=null); |
| 179 |
if(cris2!=null){return true;} |
| 180 |
if(cris1!=null){return cris1.paired();} |
| 181 |
return false; |
| 182 |
} |
| 183 |
|
| 184 |
@Override |
| 185 |
public Object[] producers() { |
| 186 |
ArrayList<Object> list=new ArrayList<Object>(); |
| 187 |
if(cris1!=null){ |
| 188 |
for(Object o : cris1.producers()){list.add(o);} |
| 189 |
} |
| 190 |
if(cris2!=null){ |
| 191 |
for(Object o : cris2.producers()){list.add(o);} |
| 192 |
} |
| 193 |
return list.toArray(); |
| 194 |
} |
| 195 |
|
| 196 |
@Override |
| 197 |
public boolean errorState() { |
| 198 |
if(cris1!=null){errorState|=cris1.errorState();} |
| 199 |
if(cris2!=null){errorState|=cris2.errorState();} |
| 200 |
return errorState; |
| 201 |
} |
| 202 |
|
| 203 |
@Override |
| 204 |
public void setSampleRate(float rate, long seed) { |
| 205 |
throw new RuntimeException("Invalid."); |
| 206 |
} |
| 207 |
|
| 208 |
@Override |
| 209 |
public long basesIn() { |
| 210 |
return (cris1==null ? 0 : cris1.basesIn())+(cris2==null ? 0 : cris2.basesIn()); |
| 211 |
} |
| 212 |
|
| 213 |
@Override |
| 214 |
public long readsIn() { |
| 215 |
return (cris1==null ? 0 : cris1.readsIn())+(cris2==null ? 0 : cris2.readsIn()); |
| 216 |
} |
| 217 |
|
| 218 |
@Override |
| 219 |
public boolean verbose() { |
| 220 |
return verbose; |
| 221 |
} |
| 222 |
|
| 223 |
} |