Debian Codegraph @

current/template/A_SampleMultipleInput.java

raw symbols SCIP
1 package template;
2
3 import java.io.File;
4 import java.util.ArrayList;
5
6 import fileIO.FileFormat;
7 import fileIO.ReadWrite;
8 import shared.Parser;
9 import shared.PreParser;
10 import shared.Shared;
11 import shared.Timer;
12 import shared.Tools;
13 import stream.ConcurrentReadInputStream;
14 import stream.ConcurrentReadOutputStream;
15 import stream.Read;
16 import structures.ListNum;
17
18 /**
19 * Accepts multiple input files.
20 * Reads them each sequentially, and outputs everything to a single output file.
21 * Generically, it can be used to concatenate files while recompressing them
22 * and avoiding the use of stdio.
23 * @author Brian Bushnell
24 * @date May 10, 2024
25 *
26 */
27 public class A_SampleMultipleInput {
28
29 public static void main(String[] args){
30 //Start a timer immediately upon code entrance.
31 Timer t=new Timer();
32
33 //Create an instance of this class
34 A_SampleMultipleInput x=new A_SampleMultipleInput(args);
35
36 //Run the object
37 x.process(t);
38
39 //Close the print stream if it was redirected
40 Shared.closeStream(x.outstream);
41 }
42
43 public A_SampleMultipleInput(String[] args){
44
45 {//Preparse block for help, config files, and outstream
46 PreParser pp=new PreParser(args, getClass(), false);
47 args=pp.args;
48 outstream=pp.outstream;
49 }
50
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 if(b!=null && b.equalsIgnoreCase("null")){b=null;}
58
59 if(a.equals("parse_flag_goes_here")){
60 //Set a variable here
61 }else if(a.equals("in")){
62 in.clear();
63 String[] b2=(b==null) ? null : (new File(b).exists() ? new String[] {b} : b.split(","));
64 for(String b3 : b2){in.add(b3);}
65 }else if(b==null && new File(arg).exists()){
66 in.add(arg);
67 }else if(parser.parse(arg, a, b)){
68 //do nothing
69 }else{
70 assert(false) : "Unknown parameter "+args[i];
71 outstream.println("Unknown parameter "+args[i]);
72 }
73 }
74
75 {//Process parser fields
76 Parser.processQuality();
77
78 maxReads=parser.maxReads;
79 out1=parser.out1;
80 }
81
82 ffout1=FileFormat.testOutput(out1, FileFormat.FASTQ, null, true, true, false, false);
83 }
84
85 void process(Timer t){
86
87 final ConcurrentReadOutputStream ros;
88 if(out1!=null){
89 final int buff=4;
90
91 for(String s : in) {
92 assert(!out1.equalsIgnoreCase(s)) : "Input file and output file have same name.";
93 }
94
95 ros=ConcurrentReadOutputStream.getStream(ffout1, null, buff, null, false);
96 ros.start();
97 }else{ros=null;}
98
99 for(String s : in) {
100 processInner(s, ros);
101 }
102
103 ReadWrite.closeStream(ros);
104 if(verbose){outstream.println("Finished.");}
105
106 t.stop();
107 outstream.println("Time: \t"+t);
108 outstream.println("Reads Processed: "+readsProcessed+" \t"
109 +Tools.format("%.2fk reads/sec", (readsProcessed/(double)(t.elapsed))*1000000));
110 }
111
112 void processInner(String fname, ConcurrentReadOutputStream ros) {
113 FileFormat ffin=FileFormat.testInput(fname, FileFormat.FASTQ, null, true, true);
114
115 final ConcurrentReadInputStream cris;
116 {
117 cris=ConcurrentReadInputStream.getReadInputStream(maxReads, true, ffin, null);
118 cris.start();
119 }
120 boolean paired=cris.paired();
121
122 {
123 ListNum<Read> ln=cris.nextList();
124 ArrayList<Read> reads=(ln!=null ? ln.list : null);
125
126 if(reads!=null && !reads.isEmpty()){
127 Read r=reads.get(0);
128 assert((ffin==null || ffin.samOrBam()) || (r.mate!=null)==cris.paired());
129 }
130
131 //ln!=null prevents a compiler potential null access warning
132 while(ln!=null && reads!=null && reads.size()>0){
133 if(verbose){outstream.println("Fetched "+reads.size()+" reads.");}
134
135 for(int idx=0; idx<reads.size(); idx++){
136 final Read r1=reads.get(idx);
137 readsProcessed+=r1.pairCount();
138 basesProcessed+=r1.pairLength();
139
140 // ********* Process reads here *********
141 }
142
143 if(ros!=null){ros.add(reads, ln.id);}
144
145 cris.returnList(ln);
146 if(verbose){outstream.println("Returned a list.");}
147 ln=cris.nextList();
148 reads=(ln!=null ? ln.list : null);
149 }
150 if(ln!=null){
151 cris.returnList(ln.id, ln.list==null || ln.list.isEmpty());
152 }
153 }
154 ReadWrite.closeStream(cris);
155 }
156
157 /*--------------------------------------------------------------*/
158
159 /*--------------------------------------------------------------*/
160
161 private ArrayList<String> in=new ArrayList<String>();
162 private String out1=null;
163
164 private final FileFormat ffout1;
165
166 /*--------------------------------------------------------------*/
167
168 private long maxReads=-1;
169 private long readsProcessed=0, basesProcessed=0;
170
171 /*--------------------------------------------------------------*/
172
173 private java.io.PrintStream outstream=System.err;
174 public static boolean verbose=false;
175
176 }