| 1 |
package stream; |
| 2 |
|
| 3 |
import java.util.ArrayList; |
| 4 |
import java.util.LinkedHashMap; |
| 5 |
import java.util.Set; |
| 6 |
|
| 7 |
import fileIO.FileFormat; |
| 8 |
import fileIO.ReadWrite; |
| 9 |
import shared.Shared; |
| 10 |
import shared.Tools; |
| 11 |
import structures.ListNum; |
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
|
| 19 |
public class MultiCros { |
| 20 |
|
| 21 |
public static void main(String[] args){ |
| 22 |
String in=args[0]; |
| 23 |
String pattern=args[1]; |
| 24 |
ArrayList<String> names=new ArrayList<String>(); |
| 25 |
for(int i=2; i<args.length; i++){ |
| 26 |
names.add(args[i]); |
| 27 |
} |
| 28 |
final int buff=Tools.max(16, 2*Shared.threads()); |
| 29 |
MultiCros mcros=new MultiCros(pattern, null, false, false, false, false, false, FileFormat.FASTQ, buff); |
| 30 |
|
| 31 |
ConcurrentReadInputStream cris=ConcurrentReadInputStream.getReadInputStream(-1, true, false, in); |
| 32 |
cris.start(); |
| 33 |
|
| 34 |
ListNum<Read> ln=cris.nextList(); |
| 35 |
ArrayList<Read> reads=(ln!=null ? ln.list : null); |
| 36 |
ArrayListSet als=new ArrayListSet(false); |
| 37 |
|
| 38 |
while(ln!=null && reads!=null && reads.size()>0){ |
| 39 |
|
| 40 |
for(Read r1 : reads){ |
| 41 |
als.add(r1, names); |
| 42 |
} |
| 43 |
cris.returnList(ln); |
| 44 |
if(mcros!=null){mcros.add(als, ln.id);} |
| 45 |
ln=cris.nextList(); |
| 46 |
reads=(ln!=null ? ln.list : null); |
| 47 |
} |
| 48 |
cris.returnList(ln); |
| 49 |
if(mcros!=null){mcros.add(als, ln.id);} |
| 50 |
ReadWrite.closeStreams(cris); |
| 51 |
ReadWrite.closeStreams(mcros); |
| 52 |
} |
| 53 |
|
| 54 |
|
| 55 |
|
| 56 |
|
| 57 |
|
| 58 |
public MultiCros(String pattern1_, String pattern2_, |
| 59 |
boolean ordered_, boolean overwrite_, boolean append_, boolean allowSubprocess_, boolean useSharedHeader_, int defaultFormat_, int maxSize_){ |
| 60 |
assert(pattern1_!=null && pattern1_.indexOf('%')>=0); |
| 61 |
assert(pattern2_==null || pattern1_.indexOf('%')>=0); |
| 62 |
if(pattern2_==null && pattern1_.indexOf('#')>=0){ |
| 63 |
pattern1=pattern1_.replaceFirst("#", "1"); |
| 64 |
pattern2=pattern1_.replaceFirst("#", "2"); |
| 65 |
}else{ |
| 66 |
pattern1=pattern1_; |
| 67 |
pattern2=pattern2_; |
| 68 |
} |
| 69 |
|
| 70 |
ordered=ordered_; |
| 71 |
overwrite= overwrite_; |
| 72 |
append=append_; |
| 73 |
allowSubprocess=allowSubprocess_; |
| 74 |
useSharedHeader=useSharedHeader_; |
| 75 |
|
| 76 |
defaultFormat=defaultFormat_; |
| 77 |
maxSize=maxSize_; |
| 78 |
|
| 79 |
streamList=new ArrayList<ConcurrentReadOutputStream>(); |
| 80 |
streamMap=new LinkedHashMap<String, ConcurrentReadOutputStream>(); |
| 81 |
} |
| 82 |
|
| 83 |
|
| 84 |
|
| 85 |
|
| 86 |
|
| 87 |
|
| 88 |
public void add(ArrayListSet set, long listnum){ |
| 89 |
for(String s : set.getNames()){ |
| 90 |
ArrayList<Read> list=set.getAndClear(s); |
| 91 |
if(list!=null){ |
| 92 |
add(list, listnum, s); |
| 93 |
} |
| 94 |
} |
| 95 |
} |
| 96 |
|
| 97 |
public void add(ArrayList<Read> list, long listnum, String name){ |
| 98 |
ConcurrentReadOutputStream ros=getStream(name); |
| 99 |
ros.add(list, listnum); |
| 100 |
} |
| 101 |
|
| 102 |
public void close(){ |
| 103 |
for(ConcurrentReadOutputStream cros : streamList){cros.close();} |
| 104 |
} |
| 105 |
|
| 106 |
public void join(){ |
| 107 |
for(ConcurrentReadOutputStream cros : streamList){cros.join();} |
| 108 |
} |
| 109 |
|
| 110 |
public void resetNextListID(){ |
| 111 |
for(ConcurrentReadOutputStream cros : streamList){cros.resetNextListID();} |
| 112 |
} |
| 113 |
|
| 114 |
public String fname(){return pattern1;} |
| 115 |
|
| 116 |
|
| 117 |
public boolean errorState(){ |
| 118 |
boolean b=errorState; |
| 119 |
for(ConcurrentReadOutputStream cros : streamList){ |
| 120 |
b=b&&cros.errorState(); |
| 121 |
} |
| 122 |
return b; |
| 123 |
} |
| 124 |
|
| 125 |
public boolean finishedSuccessfully(){ |
| 126 |
boolean b=true; |
| 127 |
for(ConcurrentReadOutputStream cros : streamList){ |
| 128 |
b=b&&cros.finishedSuccessfully(); |
| 129 |
} |
| 130 |
return b; |
| 131 |
} |
| 132 |
|
| 133 |
public Set<String> getKeys(){return streamMap.keySet();} |
| 134 |
|
| 135 |
|
| 136 |
|
| 137 |
|
| 138 |
|
| 139 |
private ConcurrentReadOutputStream makeStream(String name){ |
| 140 |
String s1=pattern1.replaceFirst("%", name); |
| 141 |
String s2=pattern2==null ? null : pattern2.replaceFirst("%", name); |
| 142 |
final FileFormat ff1=FileFormat.testOutput(s1, defaultFormat, null, allowSubprocess, overwrite, append, ordered); |
| 143 |
final FileFormat ff2=FileFormat.testOutput(s2, defaultFormat, null, allowSubprocess, overwrite, append, ordered); |
| 144 |
ConcurrentReadOutputStream ros=ConcurrentReadOutputStream.getStream(ff1, ff2, maxSize, null, useSharedHeader); |
| 145 |
return ros; |
| 146 |
} |
| 147 |
|
| 148 |
|
| 149 |
|
| 150 |
|
| 151 |
|
| 152 |
public ConcurrentReadOutputStream getStream(String name){ |
| 153 |
ConcurrentReadOutputStream ros=streamMap.get(name); |
| 154 |
if(ros==null){ |
| 155 |
synchronized(streamMap){ |
| 156 |
ros=streamMap.get(name); |
| 157 |
if(ros==null){ |
| 158 |
ros=makeStream(name); |
| 159 |
ros.start(); |
| 160 |
streamList.add(ros); |
| 161 |
streamMap.put(name, ros); |
| 162 |
} |
| 163 |
} |
| 164 |
} |
| 165 |
return ros; |
| 166 |
} |
| 167 |
|
| 168 |
|
| 169 |
|
| 170 |
|
| 171 |
|
| 172 |
public final String pattern1, pattern2; |
| 173 |
public final ArrayList<ConcurrentReadOutputStream> streamList; |
| 174 |
public final LinkedHashMap<String, ConcurrentReadOutputStream> streamMap; |
| 175 |
public final boolean ordered; |
| 176 |
|
| 177 |
boolean errorState=false; |
| 178 |
boolean started=false; |
| 179 |
final boolean overwrite; |
| 180 |
final boolean append; |
| 181 |
final boolean allowSubprocess; |
| 182 |
final int defaultFormat; |
| 183 |
final int maxSize; |
| 184 |
final boolean useSharedHeader; |
| 185 |
|
| 186 |
|
| 187 |
|
| 188 |
|
| 189 |
|
| 190 |
public static boolean verbose=false; |
| 191 |
|
| 192 |
} |