Debian Codegraph @

current/jgi/FindPrimers_old.java

raw symbols SCIP
1 package jgi;
2
3 import java.io.File;
4 import java.util.ArrayList;
5
6 import align2.MSA;
7 import fileIO.ByteStreamWriter;
8 import fileIO.FileFormat;
9 import fileIO.ReadWrite;
10 import shared.Parse;
11 import shared.Parser;
12 import shared.PreParser;
13 import shared.Shared;
14 import shared.Timer;
15 import shared.Tools;
16 import stream.ConcurrentReadInputStream;
17 import stream.FastaReadInputStream;
18 import stream.Read;
19 import stream.SamLine;
20 import stream.SiteScore;
21 import structures.ByteBuilder;
22 import structures.ListNum;
23
24 /**
25 * @author Brian Bushnell
26 * @date Oct 6, 2014
27 *
28 */
29 public class FindPrimers_old {
30
31 public static void main(String[] args){
32 Timer t=new Timer();
33 FindPrimers_old x=new FindPrimers_old(args);
34 x.process(t);
35
36 //Close the print stream if it was redirected
37 Shared.closeStream(x.outstream);
38 }
39
40 public FindPrimers_old(String[] args){
41
42 {//Preparse block for help, config files, and outstream
43 PreParser pp=new PreParser(args, getClass(), false);
44 args=pp.args;
45 outstream=pp.outstream;
46 }
47
48 float cutoff_=0;
49 String literal_=null;
50 String ref_=null;
51 Parser parser=new Parser();
52 for(int i=0; i<args.length; i++){
53 String arg=args[i];
54 String[] split=arg.split("=");
55 String a=split[0].toLowerCase();
56 String b=split.length>1 ? split[1] : null;
57
58 if(parser.parse(arg, a, b)){
59 //do nothing
60 }else if(a.equals("rcomp")){
61 rcomp=Parse.parseBoolean(b);
62 }else if(a.equals("replicate") || a.equals("expand")){
63 replicateAmbiguous=Parse.parseBoolean(b);
64 }else if(a.equals("literal")){
65 literal_=b;
66 }else if(a.equals("cutoff") || a.equals("minid")){
67 cutoff_=Float.parseFloat(b);
68 if(cutoff_>1){cutoff_=cutoff_/100;}
69 assert(cutoff_>=0 && cutoff_<=1) : "Cutoff should range from 0 to 1";
70 }else if(a.equals("primer") || a.equals("query") || a.equals("ref")){
71 if(new File(b).exists()){ref_=b;}
72 else{literal_=b;}
73 }else if(a.equals("msa")){
74 msaType=b;
75 }else if(a.equals("columns")){
76 columns=Integer.parseInt(b);
77 }else if(a.equals("parse_flag_goes_here")){
78 //Set a variable here
79 }else{
80 outstream.println("Unknown parameter "+args[i]);
81 assert(false) : "Unknown parameter "+args[i];
82 // throw new RuntimeException("Unknown parameter "+args[i]);
83 }
84 }
85
86 {//Process parser fields
87 Parser.processQuality();
88
89 maxReads=parser.maxReads;
90 in1=parser.in1;
91 out1=parser.out1;
92 }
93 cutoff=cutoff_;
94
95 if(ref_!=null){
96 ArrayList<Read> list=FastaReadInputStream.toReads(ref_, FileFormat.FASTA, -1);
97 int max=0;
98 queries=new ArrayList<Read>();
99 for(int i=0; i<list.size(); i++){
100 Read r=list.get(i);
101 max=Tools.max(max, r.length());
102 queries.add(r);
103 if(rcomp){
104 r=r.copy();
105 r.reverseComplement();
106 r.id="r_"+r.id;
107 r.setStrand(1);
108 queries.add(r);
109 }
110 }
111 maxqlen=max;
112 }else if(literal_!=null){
113 int max=0;
114 String[] s2=literal_.split(",");
115 queries=new ArrayList<Read>();
116 for(int i=0; i<s2.length; i++){
117 Read r=new Read(s2[i].getBytes(), null, "query", i);
118 max=Tools.max(max, r.length());
119 queries.add(r);
120
121 }
122 maxqlen=max;
123 }else{
124 queries=null;
125 maxqlen=0;
126 }
127
128 if(replicateAmbiguous){
129 queries=Tools.replicateAmbiguous(queries, 1);
130 }
131 if(rcomp){
132 for(int i=0, max=queries.size(); i<max; i++){
133 Read r=queries.get(i).copy();
134 r.reverseComplement();
135 r.id="rquery";
136 r.setStrand(1);
137 queries.add(r);
138 }
139 }
140
141 ffout1=FileFormat.testOutput(out1, FileFormat.FASTQ, null, true, true, false, false);
142 ffin1=FileFormat.testInput(in1, FileFormat.FASTQ, null, true, true);
143 }
144
145 void process(Timer t){
146
147 final ConcurrentReadInputStream cris;
148 {
149 cris=ConcurrentReadInputStream.getReadInputStream(maxReads, true, ffin1, null);
150 if(verbose){outstream.println("Started cris");}
151 cris.start(); //4567
152 }
153 boolean paired=cris.paired();
154
155 final ByteStreamWriter bsw;
156 if(out1!=null){
157
158 assert(!out1.equalsIgnoreCase(in1) && !out1.equalsIgnoreCase(in1)) : "Input file and output file have same name.";
159
160 bsw=new ByteStreamWriter(ffout1);
161 bsw.start();
162 }else{bsw=null;}
163
164
165 MSA msa=MSA.makeMSA(maxqlen+3, columns, msaType);
166
167 long readsProcessed=0;
168 {
169
170 ListNum<Read> ln=cris.nextList();
171 ArrayList<Read> reads=(ln!=null ? ln.list : null);
172
173 if(reads!=null && !reads.isEmpty()){
174 Read r=reads.get(0);
175 assert((ffin1==null || ffin1.samOrBam()) || (r.mate!=null)==cris.paired());
176 }
177
178 while(ln!=null && reads!=null && reads.size()>0){//ln!=null prevents a compiler potential null access warning
179 if(verbose){outstream.println("Fetched "+reads.size()+" reads.");}
180
181 for(int idx=0; idx<reads.size(); idx++){
182 final Read r=reads.get(idx);
183
184 if(r.length()+2>msa.maxColumns){
185 msa=MSA.makeMSA(maxqlen+3, r.length()+2+r.length()/2, "MultiStateAligner11ts");
186 }
187 final int a=0, b=r.length()-1;
188 int[] max;
189
190 SiteScore bestSite=null;
191 Read bestQuery=null;
192 for(int qnum=0; qnum<queries.size(); qnum++){
193 final Read query=queries.get(qnum);
194 max=msa.fillLimited(query.bases, r.bases, a, b, -9999, null);
195 if(max!=null){
196 int[] score=msa.score(query.bases, r.bases, a, b, max[0], max[1], max[2], false);
197 SiteScore ss=new SiteScore(1, query.strand(), score[1], score[2], 1, score[0]);
198 if(bestSite==null || ss.quickScore>bestSite.quickScore){
199 bestQuery=query;
200 ss.setSlowScore(ss.quickScore);
201 ss.score=ss.quickScore;
202 ss.match=msa.traceback(query.bases, r.bases, a, b, score[3], score[4], score[5], false);
203 bestSite=ss;
204 }
205 }
206 }
207
208 if(bsw!=null && bestSite!=null){
209 bsw.println(toBytes(r, bestQuery, bestSite));
210 }
211
212 readsProcessed++;
213 }
214
215 cris.returnList(ln);
216 if(verbose){outstream.println("Returned a list.");}
217 ln=cris.nextList();
218 reads=(ln!=null ? ln.list : null);
219 }
220 if(ln!=null){
221 cris.returnList(ln.id, ln.list==null || ln.list.isEmpty());
222 }
223 }
224
225 if(bsw!=null){bsw.poisonAndWait();}
226 ReadWrite.closeStreams(cris);
227 if(verbose){outstream.println("Finished.");}
228
229 t.stop();
230 outstream.println("Time: \t"+t);
231 outstream.println("Reads Processed: "+readsProcessed+" \t"+Tools.format("%.2fk reads/sec", (readsProcessed/(double)(t.elapsed))*1000000));
232 }
233
234
235 /*--------------------------------------------------------------*/
236
237 private ByteBuilder toBytes(Read r, Read query, SiteScore ss){
238 ByteBuilder bb=new ByteBuilder(80);
239 float f=Read.identity(ss.match);
240 if(f<cutoff){return bb;}
241 bb.append(query.id).append('\t');
242 bb.append(makeFlag(ss)).append('\t');
243 bb.append(r.id.replace('\t', '_')).append('\t');
244 bb.append(ss.start+1).append('\t');
245 bb.append(Tools.max(ss.score/query.length(), 4)).append('\t');
246 String cigar=SamLine.toCigar14(ss.match, ss.start, ss.stop, r.length(), query.bases);
247 if(cigar==null){bb.append('*').append('\t');}else{bb.append(cigar).append('\t');}
248 bb.append('0').append('\t');
249 bb.append('*').append('\t');
250 bb.append('0').append('\t');
251
252 bb.append(query.bases).append('\t');
253 bb.append('*').append('\t');
254
255 bb.append(Tools.format("YI:f:%.2f", (100*f)));
256
257 return bb;
258 }
259
260 public static int makeFlag(SiteScore ss){
261 int flag=0;
262 if(ss.strand()==Shared.MINUS){flag|=0x10;}
263 return flag;
264 }
265
266
267
268 /*--------------------------------------------------------------*/
269
270 /*--------------------------------------------------------------*/
271
272 private String in1=null;
273 private String out1=null;
274
275 private final float cutoff;
276 private boolean rcomp=true;
277 private boolean replicateAmbiguous=true;
278
279 private final FileFormat ffin1;
280 private final FileFormat ffout1;
281 private ArrayList<Read> queries;
282 private final int maxqlen;
283 private int columns=2000;
284 private String msaType="MultiStateAligner11ts";
285
286 /*--------------------------------------------------------------*/
287
288 private long maxReads=-1;
289
290 /*--------------------------------------------------------------*/
291
292 private java.io.PrintStream outstream=System.err;
293 public static boolean verbose=false;
294
295 }