Debian Codegraph @

current/jgi/MergeSam.java

raw symbols SCIP
1 package jgi;
2
3 import java.io.File;
4 import java.io.PrintStream;
5 import java.util.ArrayList;
6
7 import fileIO.ByteFile;
8 import fileIO.ByteFile1;
9 import fileIO.ByteFile2;
10 import fileIO.ByteStreamWriter;
11 import fileIO.FileFormat;
12 import fileIO.ReadWrite;
13 import shared.Parse;
14 import shared.Parser;
15 import shared.PreParser;
16 import shared.Shared;
17 import shared.Timer;
18 import shared.Tools;
19 import stream.ConcurrentGenericReadInputStream;
20 import stream.FastaReadInputStream;
21
22 /**
23 * @author Brian Bushnell
24 * @date March 8, 2017
25 *
26 */
27 public class MergeSam {
28
29 public static void main(String[] args){
30 Timer t=new Timer();
31 MergeSam x=new MergeSam(args);
32 x.process(t);
33
34 //Close the print stream if it was redirected
35 Shared.closeStream(x.outstream);
36 }
37
38 public MergeSam(String[] args){
39
40 {//Preparse block for help, config files, and outstream
41 PreParser pp=new PreParser(args, getClass(), false);
42 args=pp.args;
43 outstream=pp.outstream;
44 }
45
46 ReadWrite.USE_PIGZ=ReadWrite.USE_UNPIGZ=true;
47 ReadWrite.setZipThreads(Shared.threads());
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
56 if(a.equals("invalid")){
57 outInvalid=b;
58 }else if(a.equals("lines")){
59 maxLines=Long.parseLong(b);
60 if(maxLines<0){maxLines=Long.MAX_VALUE;}
61 }else if(a.equals("verbose")){
62 verbose=Parse.parseBoolean(b);
63 ByteFile1.verbose=verbose;
64 ByteFile2.verbose=verbose;
65 stream.FastaReadInputStream.verbose=verbose;
66 ConcurrentGenericReadInputStream.verbose=verbose;
67 stream.FastqReadInputStream.verbose=verbose;
68 ReadWrite.verbose=verbose;
69 }else if(a.equals("in")){
70 in.add(b);
71 }else if(a.equals("out")){
72 out=b;
73 }else if(b==null && new File(arg).exists()){
74 in.add(arg);
75 }else if(parser.parse(arg, a, b)){//Parse standard flags in the parser
76 //do nothing
77 }else{
78 outstream.println("Unknown parameter "+args[i]);
79 assert(false) : "Unknown parameter "+args[i];
80 // throw new RuntimeException("Unknown parameter "+args[i]);
81 }
82 }
83
84 {//Process parser fields
85 overwrite=parser.overwrite;
86 append=parser.append;
87 }
88
89 assert(FastaReadInputStream.settingsOK());
90
91 if(in==null){throw new RuntimeException("Error - at least one input file is required.");}
92
93 if(out!=null && out.equalsIgnoreCase("null")){out=null;}
94
95 if(!Tools.testOutputFiles(overwrite, append, false, out)){
96 outstream.println((out==null)+", "+out);
97 throw new RuntimeException("\n\noverwrite="+overwrite+"; Can't write to output files "+out+"\n");
98 }
99
100 ffout=FileFormat.testOutput(out, FileFormat.SAM, null, true, overwrite, append, false);
101 ffoutInvalid=FileFormat.testOutput(outInvalid, FileFormat.SAM, null, true, overwrite, append, false);
102 ffin=FileFormat.testInputList(in, FileFormat.SAM, null, true, true);
103 }
104
105 void process(Timer t){
106
107 ByteStreamWriter bsw=null;
108 if(ffout!=null){
109 bsw=new ByteStreamWriter(ffout);
110 bsw.start();
111 }
112
113 ByteStreamWriter bswInvalid=null;
114 if(ffoutInvalid!=null){
115 bswInvalid=new ByteStreamWriter(ffoutInvalid);
116 bswInvalid.start();
117 }
118
119 boolean headerMode=true;
120 for(int fnum=0; fnum<ffin.length; fnum++){
121
122 ByteFile bf=ByteFile.makeByteFile(ffin[fnum]);
123
124 byte[] line=bf.nextLine();
125
126 while(line!=null){
127 if(line.length>0){
128 if(maxLines>0 && linesProcessed>=maxLines){break;}
129 linesProcessed++;
130 bytesProcessed+=line.length;
131
132 boolean valid=true;
133 if(line[0]=='@'){valid=headerMode;}
134 else{headerMode=false;}
135
136 if(valid){
137 linesValid++;
138 if(bsw!=null){
139 bsw.println(line);
140 }
141 }else{
142 if(bswInvalid!=null){
143 bswInvalid.println(line);
144 }
145 }
146 }
147 line=bf.nextLine();
148 }
149
150 errorState|=bf.close();
151 }
152
153 if(bsw!=null){errorState|=bsw.poisonAndWait();}
154 if(bswInvalid!=null){errorState|=bswInvalid.poisonAndWait();}
155
156 t.stop();
157 outstream.println(Tools.timeLinesBytesProcessed(t, linesProcessed, bytesProcessed, 8));
158
159 outstream.println();
160 outstream.println("Valid Lines: \t"+linesValid);
161 outstream.println("Invalid Lines: \t"+(linesProcessed-linesValid));
162
163 if(errorState){
164 throw new RuntimeException(getClass().getName()+" terminated in an error state; the output may be corrupt.");
165 }
166 }
167
168 /*--------------------------------------------------------------*/
169
170 /*--------------------------------------------------------------*/
171
172 private ArrayList<String> in=new ArrayList<String>();
173 private String out="stdout.sam";
174 private String outInvalid=null;
175
176 /*--------------------------------------------------------------*/
177
178 private long linesProcessed=0;
179 private long linesValid=0;
180 private long bytesProcessed=0;
181
182 private long maxLines=Long.MAX_VALUE;
183
184 /*--------------------------------------------------------------*/
185
186 private final FileFormat[] ffin;
187 private final FileFormat ffout;
188 private final FileFormat ffoutInvalid;
189
190
191 /*--------------------------------------------------------------*/
192
193 private PrintStream outstream=System.err;
194 public static boolean verbose=false;
195 public boolean errorState=false;
196 private boolean overwrite=true;
197 private boolean append=false;
198
199 }