comparison CSP2/CSP2_env/env-d9b9114564458d9d-741b3de822f2aaca6c6caa4325c4afce/opt/bbmap-39.01-1/current/prok/PGMTools.java @ 68:5028fdace37b

planemo upload commit 2e9511a184a1ca667c7be0c6321a36dc4e3d116d
author jpayne
date Tue, 18 Mar 2025 16:23:26 -0400
parents
children
comparison
equal deleted inserted replaced
67:0e9998148a16 68:5028fdace37b
1 package prok;
2
3 import java.io.File;
4 import java.io.PrintStream;
5 import java.util.ArrayList;
6
7 import dna.Data;
8 import fileIO.ByteStreamWriter;
9 import fileIO.FileFormat;
10 import fileIO.ReadWrite;
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 import structures.ByteBuilder;
18
19 /**
20 * Static helpers for manipulating pgm files.
21 * main() merges pgm files.
22 * @author Brian Bushnell
23 * @date Sep 24, 2018
24 *
25 */
26 public class PGMTools extends ProkObject {
27
28 /*--------------------------------------------------------------*/
29 /*---------------- Main ----------------*/
30 /*--------------------------------------------------------------*/
31
32 /** Combines multiple pgm files into a single file */
33 public static void main(String[] args){
34
35 //Start a timer immediately upon code entrance.
36 Timer t=new Timer();
37
38 {//Preparse block for help, config files, and outstream
39 PreParser pp=new PreParser(args, new Object() { }.getClass().getEnclosingClass(), false);
40 args=pp.args;
41 outstream=pp.outstream;
42 }
43
44 ProkObject.call18S=true;
45 boolean overwrite=true;
46 boolean allowDupes=false;
47 String out=null;
48 ArrayList<String> in=new ArrayList<String>();
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("in")){
60 assert(b!=null);
61 Tools.addFiles(b, in);
62 }else if(parseStatic(arg, a, b)){
63 //do nothing
64 }else if(a.equals("allowdupes") || a.equals("allowduplicates") || a.equals("dupes")){
65 allowDupes=Parse.parseBoolean(b);
66 }else if(a.equals("verbose")){
67 verbose=Parse.parseBoolean(b);
68 ReadWrite.verbose=verbose;
69 }
70
71 else if(parser.parse(arg, a, b)){
72 //do nothing
73 }else if(b==null && new File(arg.split("@")[0]).exists()){
74 in.add(arg);
75 }else{
76 outstream.println("Unknown parameter "+args[i]);
77 assert(false) : "Unknown parameter "+args[i];
78 // throw new RuntimeException("Unknown parameter "+args[i]);
79 }
80 }
81 overwrite=parser.overwrite;
82 out=parser.out1;
83 }
84
85 checkFileExistence(in, out, overwrite, allowDupes);
86
87 ArrayList<GeneModel> models=loadModels(in);
88 GeneModel gm=mergeModels(models);
89 boolean errorState=writeModel(gm, out, overwrite);
90
91 //Close the print stream if it was redirected
92 Shared.closeStream(outstream);
93 }
94
95 public static boolean parseStatic(String arg, String a, String b){
96 if(a.equals("kinnercds")){
97 int k=Integer.parseInt(b);
98 GeneModel.setInnerK(k);
99 }else if(a.equals("kstartcds")){
100 int k=Integer.parseInt(b);
101 GeneModel.setStartK(k);
102 }else if(a.equals("kstopcds")){
103 int k=Integer.parseInt(b);
104 GeneModel.setStopK(k);
105 }else if(a.equals("kinnerrna")){
106 int k=Integer.parseInt(b);
107 kInnerRNA=k;
108 }else if(a.equals("kstartrna")){
109 int k=Integer.parseInt(b);
110 kStartRNA=k;
111 }else if(a.equals("kstoprna")){
112 int k=Integer.parseInt(b);
113 kStopRNA=k;
114 }else if(a.equals("startleftoffset")){
115 int x=Integer.parseInt(b);
116 GeneModel.setStartLeftOffset(x);
117 }else if(a.equals("startrightoffset")){
118 int x=Integer.parseInt(b);
119 GeneModel.setStartRightOffset(x);
120 }else if(a.equals("stopleftoffset")){
121 int x=Integer.parseInt(b);
122 GeneModel.setStopLeftOffset(x);
123 }else if(a.equals("stoprightoffset")){
124 int x=Integer.parseInt(b);
125 GeneModel.setStopRightOffset(x);
126 }else if(a.equalsIgnoreCase("callcdsonly") || a.equalsIgnoreCase("cdsonly")){
127 callCDS=Parse.parseBoolean(b);
128 calltRNA=call16S=call23S=call5S=call18S=!callCDS;
129 }else if(a.equalsIgnoreCase("call16sonly") || a.equalsIgnoreCase("16sonly")){
130 call16S=Parse.parseBoolean(b);
131 calltRNA=call23S=call5S=call18S=callCDS=!call16S;
132 }else if(a.equalsIgnoreCase("call23sonly") || a.equalsIgnoreCase("23sonly")){
133 call23S=Parse.parseBoolean(b);
134 calltRNA=call16S=call5S=call18S=callCDS=!call23S;
135 }else if(a.equalsIgnoreCase("call5sonly") || a.equalsIgnoreCase("5sonly")){
136 call5S=Parse.parseBoolean(b);
137 calltRNA=call16S=call23S=call18S=callCDS=!call5S;
138 }else if(a.equalsIgnoreCase("calltrnaonly") || a.equalsIgnoreCase("trnaonly")){
139 calltRNA=Parse.parseBoolean(b);
140 call16S=call23S=call5S=call18S=callCDS=!calltRNA;
141 }else if(a.equalsIgnoreCase("call18sonly") || a.equalsIgnoreCase("18sonly")){
142 call18S=Parse.parseBoolean(b);
143 calltRNA=call16S=call23S=call5S=callCDS=!call18S;
144 }
145
146 else if(a.equalsIgnoreCase("callcds") || a.equalsIgnoreCase("cds")){
147 callCDS=Parse.parseBoolean(b);
148 }else if(a.equalsIgnoreCase("calltrna") || a.equalsIgnoreCase("trna")){
149 calltRNA=Parse.parseBoolean(b);
150 }else if(a.equalsIgnoreCase("call16s") || a.equalsIgnoreCase("16s")){
151 call16S=Parse.parseBoolean(b);
152 }else if(a.equalsIgnoreCase("call18s") || a.equalsIgnoreCase("18s")){
153 call18S=Parse.parseBoolean(b);
154 }else if(a.equalsIgnoreCase("call23s") || a.equalsIgnoreCase("23s")){
155 call23S=Parse.parseBoolean(b);
156 }else if(a.equalsIgnoreCase("call5s") || a.equalsIgnoreCase("5s")){
157 call5S=Parse.parseBoolean(b);
158 }else if(a.equalsIgnoreCase("callrna") || a.equalsIgnoreCase("rna")){
159 calltRNA=call16S=call18S=call5S=call23S=Parse.parseBoolean(b);
160 }
161
162 else if(a.equalsIgnoreCase("normalize")){
163 normalize=Parse.parseBoolean(b);
164 }
165
166 else{
167 return false;
168 }
169 return true;
170 }
171
172 public static ArrayList<GeneModel> loadModels(ArrayList<String> fnames){
173 ArrayList<GeneModel> models=new ArrayList<GeneModel>(fnames.size());
174 ArrayList<Double> mults=new ArrayList<Double>(fnames.size());
175 for(String s : fnames){
176 double mult=1;
177 String fname=s;
178 if(s.indexOf('@')>=0){
179 String[] split=s.split("@");
180 fname=split[0];
181 mult=Double.parseDouble(split[1]);
182 }
183 GeneModel pgm=GeneModelParser.loadModel(fname);
184 mults.add(mult);
185 models.add(pgm);
186 }
187 if(normalize){
188 long max=0;
189 for(GeneModel gm : models){
190 max=Tools.max(gm.basesProcessed, max);
191 }
192 for(GeneModel gm : models){
193 if(max!=gm.basesProcessed){
194 double mult=max/(double)(Tools.max(100, gm.basesProcessed));
195 gm.multiplyBy(mult);
196 }
197 }
198 }
199 for(int i=0; i<models.size(); i++){
200 double mult=mults.get(i);
201 GeneModel gm=models.get(i);
202 if(mult!=1){gm.multiplyBy(mult);}
203 }
204 return models;
205 }
206
207 public static GeneModel mergeModels(ArrayList<GeneModel> models){
208 if(models.size()==1){return models.get(0);}
209 GeneModel pgmSum=new GeneModel(true);
210 for(GeneModel pgm : models){
211 pgmSum.add(pgm);
212 }
213 return pgmSum;
214 }
215
216 public static GeneModel loadAndMerge(ArrayList<String> in) {
217 ArrayList<GeneModel> models=loadModels(in);
218 return mergeModels(models);
219 }
220
221 public static boolean writeModel(GeneModel pgm, String out, boolean overwrite){
222 FileFormat ffout=FileFormat.testOutput(out, FileFormat.PGM, null, true, overwrite, false, false);
223 return writeModel(pgm, ffout);
224 }
225
226 public static boolean writeModel(GeneModel pgm, FileFormat ffout){
227 ByteStreamWriter bsw=ByteStreamWriter.makeBSW(ffout);
228
229 ByteBuilder bb=new ByteBuilder();
230 pgm.appendTo(bb);
231
232 boolean errorState=false;
233 if(bsw!=null){
234 bsw.addJob(bb);
235 errorState|=bsw.poisonAndWait();
236 }
237 return errorState;
238 }
239
240 /** Ensure files can be read and written */
241 private static void checkFileExistence(ArrayList<String> in, String out, boolean overwrite, boolean allowDupes){
242 //Ensure output files can be written
243 if(!Tools.testOutputFiles(overwrite, false, false, out)){
244 outstream.println((out==null)+", "+out);
245 throw new RuntimeException("\n\noverwrite="+overwrite+"; Can't write to output file "+out+"\n");
246 }
247 ArrayList<String> in2=new ArrayList<String>();
248 for(String s : in){
249 in2.add(s.split("@")[0]);
250 }
251 in=null;
252
253 for(int i=0; i<in2.size(); i++){
254 String s=in2.get(i);
255 if(s.equalsIgnoreCase("auto") || s.equalsIgnoreCase("default")){
256 in2.set(i, Data.findPath("?model.pgm"));
257 }
258 }
259
260 //Ensure input files can be read
261 ArrayList<String> foo=new ArrayList<String>();
262 foo.addAll(in2);
263 if(!Tools.testInputFiles(allowDupes, true, foo.toArray(new String[0]))){
264 throw new RuntimeException("\nCan't read some input files.\n");
265 }
266
267 //Ensure that no file was specified multiple times
268 if(!allowDupes){
269 foo.add(out);
270 if(!Tools.testForDuplicateFiles(true, foo.toArray(new String[0]))){
271 throw new RuntimeException("\nSome file names were specified multiple times.\n");
272 }
273 }
274 }
275
276 /*--------------------------------------------------------------*/
277
278 private static PrintStream outstream=System.err;
279 public static boolean verbose=false;
280 /** Mix models equally */
281 public static boolean normalize=false;
282
283 }