Debian Codegraph @

current/tax/AnalyzeAccession_ST.java

raw symbols SCIP
1 package tax;
2
3 import java.io.File;
4 import java.io.PrintStream;
5 import java.util.ArrayList;
6 import java.util.Arrays;
7 import java.util.Collections;
8 import java.util.HashMap;
9
10 import fileIO.ByteFile;
11 import fileIO.ByteFile1;
12 import fileIO.ByteFile2;
13 import fileIO.ByteStreamWriter;
14 import fileIO.FileFormat;
15 import fileIO.ReadWrite;
16 import fileIO.TextFile;
17 import shared.Parse;
18 import shared.Parser;
19 import shared.PreParser;
20 import shared.Shared;
21 import shared.Timer;
22 import shared.Tools;
23 import stream.ConcurrentGenericReadInputStream;
24 import stream.FastaReadInputStream;
25 import structures.ByteBuilder;
26 import structures.StringNum;
27
28 /**
29 * Counts patterns in Accessions.
30 * Handles hashing for Accession to TaxID lookups.
31 * @author Brian Bushnell
32 * @date May 9, 2018
33 *
34 */
35 public class AnalyzeAccession_ST {
36
37 public static void main(String[] args){
38 //Start a timer immediately upon code entrance.
39 Timer t=new Timer();
40
41 //Create an instance of this class
42 AnalyzeAccession_ST x=new AnalyzeAccession_ST(args);
43
44 //Run the object
45 x.process(t);
46
47 //Close the print stream if it was redirected
48 Shared.closeStream(x.outstream);
49 }
50
51 public AnalyzeAccession_ST(String[] args){
52
53 {//Preparse block for help, config files, and outstream
54 PreParser pp=new PreParser(args, getClass(), false);
55 args=pp.args;
56 outstream=pp.outstream;
57 }
58
59 ReadWrite.USE_PIGZ=ReadWrite.USE_UNPIGZ=true;
60 ReadWrite.setZipThreads(Shared.threads());
61
62 Parser parser=new Parser();
63 for(int i=0; i<args.length; i++){
64 String arg=args[i];
65 String[] split=arg.split("=");
66 String a=split[0].toLowerCase();
67 String b=split.length>1 ? split[1] : null;
68
69 if(a.equals("lines")){
70 maxLines=Long.parseLong(b);
71 if(maxLines<0){maxLines=Long.MAX_VALUE;}
72 }else if(a.equals("verbose")){
73 verbose=Parse.parseBoolean(b);
74 ByteFile1.verbose=verbose;
75 ByteFile2.verbose=verbose;
76 stream.FastaReadInputStream.verbose=verbose;
77 ConcurrentGenericReadInputStream.verbose=verbose;
78 stream.FastqReadInputStream.verbose=verbose;
79 ReadWrite.verbose=verbose;
80 }else if(a.equals("in")){
81 if(b==null){in.clear();}
82 else{
83 String[] split2=b.split(",");
84 for(String s2 : split2){
85 in.add(s2);
86 }
87 }
88 }else if(b==null && new File(arg).exists()){
89 in.add(arg);
90 }else if(parser.parse(arg, a, b)){
91 //do nothing
92 }else{
93 outstream.println("Unknown parameter "+args[i]);
94 assert(false) : "Unknown parameter "+args[i];
95 // throw new RuntimeException("Unknown parameter "+args[i]);
96 }
97 }
98
99 {//Process parser fields
100 overwrite=parser.overwrite;
101 append=parser.append;
102
103 out=parser.out1;
104 }
105
106 assert(FastaReadInputStream.settingsOK());
107
108 if(in==null){throw new RuntimeException("Error - at least one input file is required.");}
109
110 if(!ByteFile.FORCE_MODE_BF2){
111 ByteFile.FORCE_MODE_BF2=false;
112 ByteFile.FORCE_MODE_BF1=true;
113 }
114
115 if(out!=null && out.equalsIgnoreCase("null")){out=null;}
116
117 if(!Tools.testOutputFiles(overwrite, append, false, out)){
118 outstream.println((out==null)+", "+out);
119 throw new RuntimeException("\n\noverwrite="+overwrite+"; Can't write to output files "+out+"\n");
120 }
121
122 ffout=FileFormat.testOutput(out, FileFormat.TXT, null, true, overwrite, append, false);
123 ffina=new FileFormat[in.size()];
124 for(int i=0; i<in.size(); i++){
125 ffina[i]=FileFormat.testInput(in.get(i), FileFormat.TXT, null, true, false);
126 }
127 }
128
129 void process(Timer t){
130
131 for(FileFormat ffin : ffina){
132 process_inner(ffin);
133 }
134
135 if(ffout!=null){
136 ByteStreamWriter bsw=new ByteStreamWriter(ffout);
137 bsw.println("#Pattern\tCount\tCombos\tBits");
138 ArrayList<StringNum> list=new ArrayList<StringNum>();
139 list.addAll(countMap.values());
140 Collections.sort(list);
141 Collections.reverse(list);
142 for(StringNum sn : list){
143 double combos=1;
144 for(int i=0; i<sn.s.length(); i++){
145 char c=sn.s.charAt(i);
146 if(c=='D'){combos*=10;}
147 else if(c=='L'){combos*=26;}
148 }
149 bsw.print(sn.toString().getBytes());
150 bsw.println("\t"+(long)combos+"\t"+Tools.format("%.2f", Tools.log2(combos)));
151 }
152 bsw.start();
153 errorState|=bsw.poisonAndWait();
154 }
155
156 t.stop();
157
158 outstream.println(Tools.timeLinesBytesProcessed(t, linesProcessed, bytesProcessed, 8));
159
160 outstream.println();
161 outstream.println("Valid Lines: \t"+linesOut);
162 outstream.println("Invalid Lines: \t"+(linesProcessed-linesOut));
163
164 if(errorState){
165 throw new RuntimeException(getClass().getName()+" terminated in an error state; the output may be corrupt.");
166 }
167 }
168
169 void process_inner(FileFormat ffin){
170
171 ByteFile bf=ByteFile.makeByteFile(ffin);
172
173 byte[] line=bf.nextLine();
174 StringBuilder buffer=new StringBuilder(32);
175
176 for(int lineNum=0; line!=null; lineNum++){
177 if(line.length>0){
178 if(maxLines>0 && linesProcessed>=maxLines){break;}
179 linesProcessed++;
180 bytesProcessed+=(line.length+1);
181
182 assert((lineNum==0)==(Tools.startsWith(line, "accession"))) : "Line "+lineNum+": "+new String(line);
183 // final boolean valid=(line[0]!='#');
184
185 if(true){
186 linesOut++;
187 bytesOut+=(line.length+1);
188 increment(line, buffer);
189 }
190 }
191 line=bf.nextLine();
192 }
193
194 errorState|=bf.close();
195 }
196
197 void increment(byte[] line, StringBuilder buffer){
198 buffer.setLength(0);
199 for(int i=0; i<line.length; i++){
200 final byte b=line[i];
201 if(b==' ' || b=='\t' || b=='.'){break;}
202 buffer.append((char)remap[b]);
203 }
204 String key=buffer.toString();
205 StringNum value=countMap.get(key);
206 if(value!=null){value.increment();}
207 else{countMap.put(key, new StringNum(key, 1));}
208 }
209
210 public static long combos(String s){
211 double combos=1;
212 for(int i=0; i<s.length(); i++){
213 char c=s.charAt(i);
214 if(c=='D'){combos*=10;}
215 else if(c=='L'){combos*=26;}
216 }
217 return (combos>=Long.MAX_VALUE ? Long.MAX_VALUE : (long)Math.ceil(combos));
218 }
219
220 public static long combos(byte[] s){
221 double combos=1;
222 for(int i=0; i<s.length; i++){
223 byte c=s[i];
224 if(c=='D'){combos*=10;}
225 else if(c=='L'){combos*=26;}
226 }
227 return (combos>=Long.MAX_VALUE ? -1 : (long)Math.ceil(combos));
228 }
229
230 /*--------------------------------------------------------------*/
231
232 public static HashMap<String, Integer> loadCodeMap(String fname){
233 assert(codeMap==null);
234 TextFile tf=new TextFile(fname);
235 ArrayList<String> list=new ArrayList<String>();
236 for(String line=tf.nextLine(); line!=null; line=tf.nextLine()){
237 if(!line.startsWith("#")){
238 String[] split=line.split("\t");
239 list.add(split[0]);
240 }
241 }
242 HashMap<String, Integer> map=new HashMap<String, Integer>(list.size()*3);
243 codeBits=(int)Math.ceil(Tools.log2(list.size()));
244 final int patternBits=63-codeBits;
245 final long maxCombos=((1L<<(patternBits-1))-1);
246 for(int i=0; i<list.size(); i++){
247 String s=list.get(i);
248 longestPattern=Tools.max(longestPattern, s.length());
249 long combos=combos(s);
250 if(combos<0 || combos>=maxCombos){map.put(s, -1);}
251 else{map.put(s, i);}
252 }
253 codeMap=map;
254 return map;
255 }
256
257 public static long digitize(String s){
258 String pattern=remap(s);
259 Integer code=codeMap.get(pattern);
260 if(code==null){return -2;}
261 if(code.intValue()<0){return -1;}
262
263 long number=0;
264 for(int i=0; i<pattern.length(); i++){
265 char c=s.charAt(i);
266 char p=pattern.charAt(i);
267 if(p=='-'){
268 //do nothing
269 }else if(p=='D'){
270 number=(number*10)+(c-'0');
271 }else if(p=='L'){
272 number=(number*26)+(Tools.toUpperCase(c)-'A');
273 }else{
274 assert(false) : s;
275 }
276 }
277 number=(number<<codeBits)+code;
278 return number;
279 }
280
281 public static long digitize(byte[] s){
282 String pattern=remap(s);
283 Integer code=codeMap.get(pattern);
284 if(code==null){return -2;}
285 if(code.intValue()<0){return -1;}
286
287 long number=0;
288 for(int i=0; i<pattern.length(); i++){
289 byte c=s[i];
290 char p=pattern.charAt(i);
291 if(p=='-'){
292 //do nothing
293 }else if(p=='D'){
294 number=(number*10)+(c-'0');
295 }else if(p=='L'){
296 number=(number*26)+(Tools.toUpperCase(c)-'A');
297 }else{
298 assert(false) : s;
299 }
300 }
301 number=(number<<codeBits)+code;
302 return number;
303 }
304
305 public static String remap(String s){
306 ByteBuilder buffer=new ByteBuilder(s.length());
307 for(int i=0; i<s.length(); i++){
308 final char b=s.charAt(i);
309 if(b==' ' || b=='\t' || b=='.'){break;}
310 buffer.append((char)remap[b]);
311 }
312 return buffer.toString();
313 }
314
315 public static String remap(byte[] s){
316 ByteBuilder buffer=new ByteBuilder(s.length);
317 for(int i=0; i<s.length; i++){
318 final byte b=s[i];
319 if(b==' ' || b=='\t' || b=='.'){break;}
320 buffer.append((char)remap[b]);
321 }
322 return buffer.toString();
323 }
324
325 /*--------------------------------------------------------------*/
326
327 private ArrayList<String> in=new ArrayList<String>();
328 private String out=null;
329
330 /*--------------------------------------------------------------*/
331
332 private HashMap<String, StringNum> countMap=new HashMap<String, StringNum>();
333 public static HashMap<String, Integer> codeMap;
334 private static int codeBits=-1;
335 private static int longestPattern=-1;
336
337 private long linesProcessed=0;
338 private long linesOut=0;
339 private long bytesProcessed=0;
340 private long bytesOut=0;
341
342 private long maxLines=Long.MAX_VALUE;
343
344 /*--------------------------------------------------------------*/
345
346 private final FileFormat[] ffina;
347 private final FileFormat ffout;
348
349 private static final byte[] remap=makeRemap();
350
351 private static byte[] makeRemap(){
352 byte[] array=new byte[128];
353 Arrays.fill(array, (byte)'?');
354 for(int i='A'; i<='Z'; i++){array[i]='L';}
355 for(int i='a'; i<='z'; i++){array[i]='L';}
356 for(int i='0'; i<='9'; i++){array[i]='D';}
357 array['_']=array['-']='-';
358 return array;
359 }
360
361 /*--------------------------------------------------------------*/
362
363 private PrintStream outstream=System.err;
364 public static boolean verbose=false;
365 public boolean errorState=false;
366 private boolean overwrite=true;
367 private boolean append=false;
368
369 }