Debian Codegraph @

current/driver/MergeBigelow.java

raw symbols SCIP
1 package driver;
2
3 import java.io.File;
4 import java.io.PrintStream;
5 import java.util.HashMap;
6
7 import fileIO.FileFormat;
8 import fileIO.ReadWrite;
9 import fileIO.TextFile;
10 import fileIO.TextStreamWriter;
11 import shared.Parse;
12 import shared.Parser;
13 import shared.PreParser;
14 import shared.Shared;
15 import shared.Timer;
16 import shared.Tools;
17
18 /**
19 * @author Brian Bushnell
20 * @date Oct 17, 2014
21 *
22 */
23 public class MergeBigelow {
24
25 public static void main(String[] args){
26 Timer t=new Timer();
27 MergeBigelow x=new MergeBigelow(args);
28 x.process(t);
29
30 //Close the print stream if it was redirected
31 Shared.closeStream(x.outstream);
32 }
33
34 public MergeBigelow(String[] args){
35
36 {//Preparse block for help, config files, and outstream
37 PreParser pp=new PreParser(args, getClass(), false);
38 args=pp.args;
39 outstream=pp.outstream;
40 }
41
42 Shared.capBuffers(4);
43 ReadWrite.USE_PIGZ=ReadWrite.USE_UNPIGZ=true;
44 ReadWrite.setZipThreads(Shared.threads());
45
46 Parser parser=new Parser();
47 for(int i=0; i<args.length; i++){
48 String arg=args[i];
49 String[] split=arg.split("=");
50 String a=split[0].toLowerCase();
51 String b=split.length>1 ? split[1] : null;
52
53 if(parser.parse(arg, a, b)){
54 //do nothing
55 }else if(a.equals("verbose")){
56 verbose=Parse.parseBoolean(b);
57 ReadWrite.verbose=verbose;
58 }else if(parser.in1==null && i==0 && Tools.looksLikeInputStream(arg)){
59 parser.in1=arg;
60 }else{
61 outstream.println("Unknown parameter "+args[i]);
62 assert(false) : "Unknown parameter "+args[i];
63 // throw new RuntimeException("Unknown parameter "+args[i]);
64 }
65 }
66
67 {//Process parser fields
68 Parser.processQuality();
69
70 maxReads=parser.maxReads;
71
72 overwrite=parser.overwrite;
73 append=parser.append;
74
75 in1=parser.in1;
76 in2=parser.in2;
77
78 out1=parser.out1;
79 }
80
81 if(in1==null || in2==null){throw new RuntimeException("Error - two input files are required.");}
82
83 if(out1!=null && out1.equalsIgnoreCase("null")){out1=null;}
84
85 if(!Tools.testOutputFiles(overwrite, append, false, out1)){
86 outstream.println((out1==null)+", "+out1);
87 throw new RuntimeException("\n\noverwrite="+overwrite+"; Can't write to output files "+out1+"\n");
88 }
89
90 assert(Tools.testInputFiles(false, true, in1, in2));
91 assert(Tools.testForDuplicateFiles(true, in1, in2, out1));
92
93 ffout1=FileFormat.testOutput(out1, FileFormat.TEXT, null, true, overwrite, append, false);
94
95 ffin1=FileFormat.testInput(in1, FileFormat.TEXT, null, true, true);
96 ffin2=FileFormat.testInput(in2, FileFormat.TEXT, null, true, true);
97 }
98
99 void process(Timer t){
100
101 table=hash(ffin2);
102
103 final TextFile tf;
104 {
105 tf=new TextFile(ffin1);
106 if(verbose){outstream.println("Started tf");}
107 }
108
109 final TextStreamWriter tsw;
110 {
111 tsw=new TextStreamWriter(ffout1);
112 tsw.start();
113 if(verbose){outstream.println("Started tsw");}
114 }
115
116 long linesProcessed=0;
117 long charsProcessed=0;
118
119 {
120 String line;
121 while((line=tf.nextLine())!=null){
122 // System.err.println("Processing "+line);
123 linesProcessed++;
124 charsProcessed+=line.length();
125 CharSequence result=processLine(line);
126 if(tsw!=null && result!=null){tsw.println(result);}
127 if(maxReads>0 && linesProcessed>=maxReads){break;}
128 }
129 }
130
131 errorState|=tsw.poisonAndWait();
132 errorState|=tf.close();
133
134 t.stop();
135 outstream.println(Tools.timeLinesBytesProcessed(t, linesProcessed, charsProcessed, 8));
136
137 if(errorState){
138 throw new RuntimeException(getClass().getName()+" terminated in an error state; the output may be corrupt.");
139 }
140 }
141
142
143 private CharSequence processLine(String line){
144 String[] split=line.split(delimiter);
145 String[] split2=table.get(split[0]);
146 if(split2==null){return line;} //Header
147 StringBuilder sb=new StringBuilder();
148 String tab="";
149 // assert(false) : split.length+", "+split2.length;
150 // System.err.println(split[1]);
151 if(split.length>1){
152 if(split[1].contains(" SCGC")){
153 split[1]=split[1].substring(0, split[1].indexOf(" SCGC"));
154 // System.err.println(split[1]);
155 }
156 if(split[1].contains(" "+split[0])){
157 split[1]=split[1].substring(0, split[1].indexOf(" "+split[0]));
158 // System.err.println(split[1]);
159 }
160 split[1]=split[1].toLowerCase();
161 // System.err.println(split[1]);
162 }
163 for(int i=0; i<split.length; i++){
164 sb.append(tab);
165 sb.append(split[i].replace(',','_'));
166 tab="\t";
167 }
168 for(int i=1; i<split2.length; i++){
169 sb.append(tab);
170 sb.append(split2[i].replace(',','_'));
171 tab="\t";
172 }
173 return sb;
174 }
175
176 private HashMap<String, String[]> hash(FileFormat ff){
177 final HashMap<String, String[]> table=new HashMap<String, String[]>();
178 final TextFile tf;
179 {
180 tf=new TextFile(ff);
181 if(verbose){outstream.println("Started tf");}
182 }
183 {
184 String line;
185 while((line=tf.nextLine())!=null){
186 String[] split=line.split(delimiter);
187 table.put(split[0], split);
188 }
189 }
190 return table;
191 }
192
193
194 /*--------------------------------------------------------------*/
195
196 /*--------------------------------------------------------------*/
197
198 private String in1=null;
199 private String in2=null;
200 private String out1=null;
201
202 private String delimiter="\t";
203 private HashMap<String, String[]> table;
204
205 /*--------------------------------------------------------------*/
206
207 private long maxReads=-1;
208
209 /*--------------------------------------------------------------*/
210
211 private final FileFormat ffin1;
212 private final FileFormat ffin2;
213 private final FileFormat ffout1;
214
215 /*--------------------------------------------------------------*/
216
217 private PrintStream outstream=System.err;
218 public static boolean verbose=false;
219 public boolean errorState=false;
220 private boolean overwrite=true;
221 private boolean append=false;
222
223 }