| 1 |
package jgi; |
| 2 |
|
| 3 |
import java.io.PrintStream; |
| 4 |
|
| 5 |
import dna.AminoAcid; |
| 6 |
import fileIO.ByteFile1; |
| 7 |
import fileIO.ByteFile2; |
| 8 |
import fileIO.ByteStreamWriter; |
| 9 |
import fileIO.FileFormat; |
| 10 |
import fileIO.ReadWrite; |
| 11 |
import shared.Parse; |
| 12 |
import shared.Parser; |
| 13 |
import shared.PreParser; |
| 14 |
import shared.Shared; |
| 15 |
import shared.Timer; |
| 16 |
import shared.Tools; |
| 17 |
import stream.ConcurrentGenericReadInputStream; |
| 18 |
import stream.FASTQ; |
| 19 |
import stream.FastaReadInputStream; |
| 20 |
import structures.ByteBuilder; |
| 21 |
import tracker.ReadStats; |
| 22 |
|
| 23 |
|
| 24 |
|
| 25 |
|
| 26 |
|
| 27 |
|
| 28 |
public class MakePolymers { |
| 29 |
|
| 30 |
public static void main(String[] args){ |
| 31 |
Timer t=new Timer(); |
| 32 |
MakePolymers x=new MakePolymers(args); |
| 33 |
x.process(t); |
| 34 |
|
| 35 |
|
| 36 |
Shared.closeStream(x.outstream); |
| 37 |
} |
| 38 |
|
| 39 |
public MakePolymers(String[] args){ |
| 40 |
|
| 41 |
{ |
| 42 |
PreParser pp=new PreParser(args, getClass(), false); |
| 43 |
args=pp.args; |
| 44 |
outstream=pp.outstream; |
| 45 |
} |
| 46 |
|
| 47 |
Shared.capBuffers(4); |
| 48 |
ReadWrite.USE_PIGZ=ReadWrite.USE_UNPIGZ=true; |
| 49 |
ReadWrite.setZipThreads(Shared.threads()); |
| 50 |
|
| 51 |
FASTQ.TEST_INTERLEAVED=FASTQ.FORCE_INTERLEAVED=false; |
| 52 |
|
| 53 |
Parser parser=new Parser(); |
| 54 |
for(int i=0; i<args.length; i++){ |
| 55 |
String arg=args[i]; |
| 56 |
String[] split=arg.split("="); |
| 57 |
String a=split[0].toLowerCase(); |
| 58 |
String b=split.length>1 ? split[1] : null; |
| 59 |
|
| 60 |
if(a.equals("verbose")){ |
| 61 |
verbose=Parse.parseBoolean(b); |
| 62 |
ByteFile1.verbose=verbose; |
| 63 |
ByteFile2.verbose=verbose; |
| 64 |
stream.FastaReadInputStream.verbose=verbose; |
| 65 |
ConcurrentGenericReadInputStream.verbose=verbose; |
| 66 |
stream.FastqReadInputStream.verbose=verbose; |
| 67 |
ReadWrite.verbose=verbose; |
| 68 |
}else if(a.equals("k")){ |
| 69 |
mink=maxk=Integer.parseInt(b); |
| 70 |
}else if(a.equals("mink")){ |
| 71 |
mink=Integer.parseInt(b); |
| 72 |
}else if(a.equals("maxk")){ |
| 73 |
maxk=Integer.parseInt(b); |
| 74 |
}else if(a.equals("len") || a.equals("minlen")){ |
| 75 |
minLen=Integer.parseInt(b); |
| 76 |
}else if(parser.parse(arg, a, b)){ |
| 77 |
|
| 78 |
}else{ |
| 79 |
outstream.println("Unknown parameter "+args[i]); |
| 80 |
assert(false) : "Unknown parameter "+args[i]; |
| 81 |
|
| 82 |
} |
| 83 |
} |
| 84 |
|
| 85 |
{ |
| 86 |
Parser.processQuality(); |
| 87 |
|
| 88 |
overwrite=ReadStats.overwrite=parser.overwrite; |
| 89 |
append=ReadStats.append=parser.append; |
| 90 |
|
| 91 |
out1=parser.out1; |
| 92 |
} |
| 93 |
|
| 94 |
assert(FastaReadInputStream.settingsOK()); |
| 95 |
if(out1!=null && out1.equalsIgnoreCase("null")){out1=null;} |
| 96 |
|
| 97 |
if(!Tools.testOutputFiles(overwrite, append, false, out1)){ |
| 98 |
outstream.println((out1==null)+", "+out1); |
| 99 |
throw new RuntimeException("\n\noverwrite="+overwrite+"; Can't write to output files "+out1+"\n"); |
| 100 |
} |
| 101 |
|
| 102 |
ffout1=FileFormat.testOutput(out1, FileFormat.FASTA, null, true, overwrite, append, false); |
| 103 |
} |
| 104 |
|
| 105 |
void process(Timer t){ |
| 106 |
|
| 107 |
final ByteStreamWriter bsw; |
| 108 |
if(ffout1!=null){ |
| 109 |
bsw=new ByteStreamWriter(ffout1); |
| 110 |
bsw.start(); |
| 111 |
}else{bsw=null;} |
| 112 |
|
| 113 |
for(int i=mink; i<=maxk; i++){ |
| 114 |
writeSequence(i, bsw); |
| 115 |
} |
| 116 |
|
| 117 |
if(bsw!=null){errorState|=bsw.poisonAndWait();} |
| 118 |
|
| 119 |
t.stop(); |
| 120 |
outstream.println(Tools.timeReadsBasesProcessed(t, readsProcessed, basesProcessed, 8)); |
| 121 |
|
| 122 |
if(errorState){ |
| 123 |
throw new RuntimeException(getClass().getName()+" terminated in an error state; the output may be corrupt."); |
| 124 |
} |
| 125 |
} |
| 126 |
|
| 127 |
private void writeSequence(int k, ByteStreamWriter bsw){ |
| 128 |
ByteBuilder bb=new ByteBuilder(); |
| 129 |
|
| 130 |
final int minLen2=((minLen+k-1)/k)*k; |
| 131 |
final int minCount; |
| 132 |
if(minLen2-minLen>=k-1){ |
| 133 |
minCount=minLen2/k; |
| 134 |
}else{ |
| 135 |
minCount=minLen2/k+1; |
| 136 |
} |
| 137 |
|
| 138 |
final long max=(1<<(2*k))-1; |
| 139 |
for(long kmer=0; kmer<=max; kmer++){ |
| 140 |
bb.append('>').append(k).append('_').append(kmer).append('\n'); |
| 141 |
for(int i=0; i<minCount; i++){ |
| 142 |
basesProcessed+=k; |
| 143 |
toBytes(kmer, k, bb); |
| 144 |
} |
| 145 |
readsProcessed++; |
| 146 |
bb.nl(); |
| 147 |
if(bb.length>=16384){ |
| 148 |
bsw.print(bb); |
| 149 |
bb.clear(); |
| 150 |
} |
| 151 |
} |
| 152 |
if(bb.length>0){ |
| 153 |
bsw.print(bb); |
| 154 |
bb.clear(); |
| 155 |
} |
| 156 |
} |
| 157 |
|
| 158 |
public static final void toBytes(long kmer, int k, ByteBuilder bb){ |
| 159 |
for(int i=k-1; i>=0; i--){ |
| 160 |
int x=(int)((kmer>>(2*i))&3); |
| 161 |
bb.append(AminoAcid.numberToBase[x]); |
| 162 |
} |
| 163 |
} |
| 164 |
|
| 165 |
|
| 166 |
|
| 167 |
|
| 168 |
|
| 169 |
private long readsProcessed=0; |
| 170 |
private long basesProcessed=0; |
| 171 |
|
| 172 |
private int mink=1, maxk=1; |
| 173 |
|
| 174 |
private int minLen=31; |
| 175 |
|
| 176 |
private String out1=null; |
| 177 |
|
| 178 |
private final FileFormat ffout1; |
| 179 |
|
| 180 |
|
| 181 |
|
| 182 |
|
| 183 |
private PrintStream outstream=System.err; |
| 184 |
public static boolean verbose=false; |
| 185 |
public boolean errorState=false; |
| 186 |
private boolean overwrite=true; |
| 187 |
private boolean append=false; |
| 188 |
|
| 189 |
} |