Debian Codegraph @

current/jasper/KmerPosition.java

raw symbols SCIP
1 package jasper;
2
3 import java.util.ArrayList;
4 import java.util.HashSet;
5
6 import fileIO.ByteStreamWriter;
7 import fileIO.FileFormat;
8 import fileIO.ReadWrite;
9 import shared.Parser;
10 import shared.PreParser;
11 import shared.Shared;
12 import shared.Timer;
13 import shared.Tools;
14 import stream.ConcurrentReadInputStream;
15 import stream.Read;
16 import structures.ListNum;
17 import structures.LongList;
18
19 /**
20 * Read in file of high-throughput reads and report the positions at which kmers from reference start
21 * @author Jasper Toscani Field
22 * @date Jun 4, 2020
23 *
24 */
25 public class KmerPosition {
26
27 public static void main(String[] args){
28 //Start a timer immediately upon code entrance.
29 Timer t=new Timer();
30
31 //Create an instance of this class
32 KmerPosition x=new KmerPosition(args);
33
34 //Run the object
35 x.process(t);
36
37 //Close the print stream if it was redirected
38 Shared.closeStream(x.outstream);
39 }
40
41 public KmerPosition(String[] args){
42
43 {//Preparse block for help, config files, and outstream
44 PreParser pp=new PreParser(args, getClass(), false);
45 args=pp.args;
46 outstream=pp.outstream;
47 }
48
49 Parser parser=new Parser();
50 for(int i=0; i<args.length; i++){
51 String arg=args[i];
52 String[] split=arg.split("=");
53 String a=split[0].toLowerCase();
54 String b=split.length>1 ? split[1] : null;
55 if(b!=null && b.equalsIgnoreCase("null")){b=null;}
56
57 if(a.equals("parse_flag_goes_here")){
58 //Set a variable here
59
60 }else if(a.equals("ref")){
61 ref=b;
62
63 }else if(a.equals("k")){
64 k=Integer.parseInt(b);
65 }else if(parser.parse(arg, a, b)){
66 //do nothing
67 }else{
68 // throw new RuntimeException("Unknown parameter "+args[i]);
69 assert(false) : "Unknown parameter "+args[i];
70 outstream.println("Unknown parameter "+args[i]);
71 }
72 }
73
74 {//Process parser fields
75 Parser.processQuality();
76
77 maxReads=parser.maxReads;
78 in1=parser.in1;
79 in2=parser.in2;
80 out1=parser.out1;
81 }
82
83 ffout1=FileFormat.testOutput(out1, FileFormat.TXT, null, true, true, false, false);
84 ffin1=FileFormat.testInput(in1, FileFormat.FASTQ, null, true, true);
85 ffin2=FileFormat.testInput(in2, FileFormat.FASTQ, null, true, true);
86 ffref=FileFormat.testInput(ref, FileFormat.FASTA, null, true, true);
87 }
88
89 void process(Timer t){
90 HashSet<String> kr=kmerReturn();
91
92 final ConcurrentReadInputStream cris;
93 {
94 cris=ConcurrentReadInputStream.getReadInputStream(maxReads, true, ffin1, ffin2);
95 cris.start();
96 }
97 boolean paired=cris.paired();
98 //System.out.println(paired);
99
100 long readsProcessed=0, basesProcessed=0;
101 {
102
103 ListNum<Read> ln=cris.nextList();
104 ArrayList<Read> reads=(ln!=null ? ln.list : null);
105
106 if(reads!=null && !reads.isEmpty()){
107 Read r=reads.get(0);
108
109 assert((ffin1==null || ffin1.samOrBam()) || (r.mate!=null)==cris.paired());
110 }
111
112 while(ln!=null && reads!=null && reads.size()>0){//ln!=null prevents a compiler potential null access warning
113 if(verbose){outstream.println("Fetched "+reads.size()+" reads.");}
114
115
116 for(int idx=0; idx<reads.size(); idx++){
117 final Read r1=reads.get(idx);
118 final Read r2=r1.mate;
119 readsProcessed+=r1.pairCount();
120 basesProcessed+=r1.pairLength();
121
122 // ********* Process reads here *********
123 //System.out.println(r1);
124 processRead(r1, kr, counts1, totalEncounter1);
125 if(r1.mate!=null) {
126 processRead(r2, kr, counts2, totalEncounter2);
127 }
128 }
129
130 cris.returnList(ln);
131 if(verbose){outstream.println("Returned a list.");}
132 ln=cris.nextList();
133 reads=(ln!=null ? ln.list : null);
134 }
135 if(ln!=null){
136 cris.returnList(ln.id, ln.list==null || ln.list.isEmpty());
137 }
138 }
139 errorState=ReadWrite.closeStreams(cris) | errorState;
140 if(verbose){outstream.println("Finished reading data.");}
141
142 //System.out.println(counts);
143 outputResults(counts1, totalEncounter1, counts2, totalEncounter2);
144
145 t.stop();
146 outstream.println("Time: \t"+t);
147 outstream.println("Reads Processed: "+readsProcessed+" \t"+Tools.format("%.2fk reads/sec", (readsProcessed/(double)(t.elapsed))*1000000));
148 assert(!errorState) : "An error was encountered.";
149 }
150
151 private void outputResults(LongList posCounts1, LongList readCounts1, LongList posCounts2, LongList readCounts2){
152 if(ffout1==null) {return;}
153 ByteStreamWriter bsw=new ByteStreamWriter(ffout1);
154 bsw.start();
155
156 long[] readArray1 = readCounts1.toArray();
157 long[] countArray1 = posCounts1.toArray();
158 long[] readArray2 = readCounts2.toArray();
159 long[] countArray2 = posCounts2.toArray();
160
161 bsw.println("#pos\tread1_count\tread1_perc\tread2_count\tread2_perc");
162
163 int maxLen = Tools.max(readArray1.length, readArray2.length);
164
165 for(int i=0; i<maxLen; i++) {
166
167 bsw.print(i);
168 bsw.print('\t');
169 bsw.print(countArray1.length>i ? countArray1[i] : 0);
170 bsw.print('\t');
171 bsw.print(countArray1.length>i ? (countArray1[i] / (float) readArray1[i]) * 100 : 0, 3);
172 bsw.print('\t');
173 bsw.print(countArray2.length>i ? countArray2[i] : 0);
174 bsw.print('\t');
175 bsw.print(countArray2.length>i ? (countArray2[i] / (float) readArray2[i]) * 100 : 0, 3);
176 bsw.println();
177 }
178 //Write stuff to the bsw
179 //bsw.println("Stuff");
180
181 errorState=bsw.poisonAndWait() | errorState;
182 }
183
184 private HashSet<String> kmerReturn(){
185 HashSet<String> hs=new HashSet<String>();
186 ArrayList<Read> readArray=ConcurrentReadInputStream.getReads(maxReads, false, ffref, null, null, null);
187 for(Read r : readArray) {
188 addToSet(hs, r);
189 }
190
191 //System.out.println(hs);
192
193 return hs;
194 }
195
196 private int addToSet(HashSet<String> hs, Read r) {
197 int countRead=0;
198 for(int i=0, j=k; j<=r.length(); i++, j++) {
199 //String(byte[] bytes, int offset, int length)
200 String s=new String(r.bases, i, k);
201 hs.add(s);
202 countRead++;
203 }
204 return countRead;
205 }
206
207 private LongList processRead(Read r, HashSet<String> hs, LongList count, LongList readCount) {
208 for(int i=0, j=k; j<=r.length(); i++, j++) {
209 //String(byte[] bytes, int offset, int length)
210
211 int x=5;
212 String s=new String(r.bases, i, k);
213 readCount.increment(i);
214 if(hs.contains(s)) {
215 count.increment(i);
216 }
217 //System.out.println(totalEncounter);
218 // System.out.println(counts);
219
220 }
221 return count;
222 }
223
224 /*--------------------------------------------------------------*/
225
226 /*--------------------------------------------------------------*/
227
228 private String in1=null;
229 private String in2=null;
230 private String out1=null;
231 private String ref=null;
232
233 private final FileFormat ffin1;
234 private final FileFormat ffin2;
235 private final FileFormat ffout1;
236 private final FileFormat ffref;
237
238 /*--------------------------------------------------------------*/
239
240 private long maxReads=-1;
241 private boolean errorState=false;
242 private int k=6;
243 private LongList counts1=new LongList();
244 private LongList totalEncounter1=new LongList();
245 private LongList counts2=new LongList();
246 private LongList totalEncounter2=new LongList();
247
248 /*--------------------------------------------------------------*/
249
250 private java.io.PrintStream outstream=System.err;
251 public static boolean verbose=false;
252
253 }