Debian Codegraph @

current/hiseq/PlotReadPosition.java

raw symbols SCIP
1 package hiseq;
2
3 import java.util.ArrayList;
4
5 import barcode.Barcode;
6 import barcode.BarcodeCounter;
7 import barcode.PCRMatrixHDist;
8 import fileIO.FileFormat;
9 import fileIO.ReadWrite;
10 import shared.Parser;
11 import shared.PreParser;
12 import shared.Shared;
13 import shared.Timer;
14 import shared.Tools;
15 import stream.ConcurrentReadInputStream;
16 import stream.ConcurrentReadOutputStream;
17 import stream.Read;
18 import structures.ByteBuilder;
19 import structures.ListNum;
20
21 /**
22 * @author Brian Bushnell
23 * @date Oct 6, 2014
24 *
25 */
26 public class PlotReadPosition {
27
28 public static void main(String[] args){
29 //Start a timer immediately upon code entrance.
30 Timer t=new Timer();
31
32 //Create an instance of this class
33 PlotReadPosition x=new PlotReadPosition(args);
34
35 //Run the object
36 x.process(t);
37
38 //Close the print stream if it was redirected
39 Shared.closeStream(x.outstream);
40 }
41
42 public PlotReadPosition(String[] args){
43
44 {//Preparse block for help, config files, and outstream
45 PreParser pp=new PreParser(args, getClass(), false);
46 args=pp.args;
47 outstream=pp.outstream;
48 }
49
50 Parser parser=new Parser();
51 for(int i=0; i<args.length; i++){
52 String arg=args[i];
53 String[] split=arg.split("=");
54 String a=split[0].toLowerCase();
55 String b=split.length>1 ? split[1] : null;
56 if(b!=null && b.equalsIgnoreCase("null")){b=null;}
57
58 if(a.equals("expected") || a.equals("names") || a.equals("barcodes")){
59 expectedPath=b;
60 }else if(a.equals("parse_flag_goes_here")){
61 //Set a variable here
62 }else if(parser.parse(arg, a, b)){
63 //do nothing
64 }else{
65 // throw new RuntimeException("Unknown parameter "+args[i]);
66 assert(false) : "Unknown parameter "+args[i];
67 outstream.println("Unknown parameter "+args[i]);
68 }
69 }
70
71 {//Process parser fields
72 Parser.processQuality();
73
74 maxReads=parser.maxReads;
75 in1=parser.in1;
76 out1=parser.out1;
77 }
78
79 IlluminaHeaderParser1.PARSE_COMMENT=true;
80 ffout1=FileFormat.testOutput(out1, FileFormat.HEADER, ".header", true, true, false, false);
81 ffin1=FileFormat.testInput(in1, FileFormat.FASTQ, null, true, true);
82 final int delimiter=ffin1.barcodeDelimiter();
83 final int len1=ffin1.barcodeLength(1);
84 final int len2=ffin1.barcodeLength(2);
85 matrix=new PCRMatrixHDist(len1, len2, delimiter, true);
86 ArrayList<String> expected=BarcodeCounter.loadBarcodes(expectedPath, delimiter);
87 matrix.populateExpected(expected);
88 }
89
90 void process(Timer t){
91
92 final ConcurrentReadInputStream cris;
93 {
94 cris=ConcurrentReadInputStream.getReadInputStream(maxReads, true, ffin1, null);
95 cris.start();
96 }
97 boolean paired=cris.paired();
98
99 final ConcurrentReadOutputStream ros;
100 if(out1!=null){
101 final int buff=4;
102
103 assert(!out1.equalsIgnoreCase(in1) && !out1.equalsIgnoreCase(in1)) : "Input file and output file have same name.";
104
105 ros=ConcurrentReadOutputStream.getStream(ffout1, null, buff, "x\ty\thdist\n", false);
106 ros.start();
107 }else{ros=null;}
108
109 long readsProcessed=0, basesProcessed=0;
110 {
111
112 ListNum<Read> ln=cris.nextList();
113 ArrayList<Read> reads=(ln!=null ? ln.list : null);
114
115 if(reads!=null && !reads.isEmpty()){
116 Read r=reads.get(0);
117 assert((ffin1==null || ffin1.samOrBam()) || (r.mate!=null)==cris.paired());
118 }
119
120 ByteBuilder bb=new ByteBuilder(128);
121 while(ln!=null && reads!=null && reads.size()>0){//ln!=null prevents a compiler potential null access warning
122 if(verbose){outstream.println("Fetched "+reads.size()+" reads.");}
123
124 for(int idx=0; idx<reads.size(); idx++){
125 final Read r1=reads.get(idx);
126 readsProcessed+=r1.pairCount();
127 basesProcessed+=r1.pairLength();
128 r1.mate=null;
129
130 // ********* Process reads here *********
131 ihp.parse(r1);
132 // System.err.println(ihp);
133 String barcode=ihp.barcode();
134 Barcode bc=matrix.findClosest(barcode, 99, 0);
135 int dist=barcode.length();
136 if(bc!=null) {
137 dist=bc.hdist(barcode);
138 }
139 bb.clear();
140 bb.append(ihp.xPos()).tab().append(ihp.yPos()).tab().append(dist);
141 r1.id=bb.toString();
142 }
143
144 if(ros!=null){ros.add(reads, ln.id);}
145
146 cris.returnList(ln);
147 if(verbose){outstream.println("Returned a list.");}
148 ln=cris.nextList();
149 reads=(ln!=null ? ln.list : null);
150 }
151 if(ln!=null){
152 cris.returnList(ln.id, ln.list==null || ln.list.isEmpty());
153 }
154 }
155 ReadWrite.closeStreams(cris, ros);
156 if(verbose){outstream.println("Finished.");}
157
158 t.stop();
159 outstream.println("Time: \t"+t);
160 outstream.println("Reads Processed: "+readsProcessed+" \t"+Tools.format("%.2fk reads/sec", (readsProcessed/(double)(t.elapsed))*1000000));
161 }
162
163 /*--------------------------------------------------------------*/
164
165 /*--------------------------------------------------------------*/
166
167 private String in1=null;
168 private String out1=null;
169 private String expectedPath=null;
170
171 private final FileFormat ffin1;
172 private final FileFormat ffout1;
173
174 /*--------------------------------------------------------------*/
175
176 private final PCRMatrixHDist matrix;
177 private final IlluminaHeaderParser2 ihp=new IlluminaHeaderParser2();
178
179 private long maxReads=-1;
180
181 /*--------------------------------------------------------------*/
182
183 private java.io.PrintStream outstream=System.err;
184 public static boolean verbose=false;
185
186 }