comparison CSP2/CSP2_env/env-d9b9114564458d9d-741b3de822f2aaca6c6caa4325c4afce/opt/bbmap-39.01-1/current/tax/IDTree.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 tax;
2
3 import java.io.PrintStream;
4 import java.util.ArrayList;
5
6 import fileIO.FileFormat;
7 import fileIO.ReadWrite;
8 import fileIO.TextFile;
9 import shared.Parse;
10 import shared.Parser;
11 import shared.PreParser;
12 import shared.Shared;
13 import shared.Timer;
14 import shared.Tools;
15 import stream.Read;
16
17 /**
18 * Creates a taxonomic tree from an identity matrix.
19 * @author Brian Bushnell
20 * @date July 1, 2016
21 *
22 */
23 public class IDTree {
24
25 /*--------------------------------------------------------------*/
26 /*---------------- Initialization ----------------*/
27 /*--------------------------------------------------------------*/
28
29 /**
30 * Code entrance from the command line.
31 * @param args Command line arguments
32 */
33 public static void main(String[] args){
34 Timer t=new Timer();
35 IDTree x=new IDTree(args);
36 x.process(t);
37
38 //Close the print stream if it was redirected
39 Shared.closeStream(x.outstream);
40 }
41
42 /**
43 * Constructor.
44 * @param args Command line arguments
45 */
46 public IDTree(String[] args){
47
48 {//Preparse block for help, config files, and outstream
49 PreParser pp=new PreParser(args, getClass(), false);
50 args=pp.args;
51 outstream=pp.outstream;
52 }
53
54 //Set shared static variables
55 Shared.capBuffers(4);
56 ReadWrite.USE_PIGZ=ReadWrite.USE_UNPIGZ=true;
57 ReadWrite.MAX_ZIP_THREADS=Shared.threads();
58
59 //Create a parser object
60 Parser parser=new Parser();
61
62 //Parse each argument
63 for(int i=0; i<args.length; i++){
64 String arg=args[i];
65
66 //Break arguments into their constituent parts, in the form of "a=b"
67 String[] split=arg.split("=");
68 String a=split[0].toLowerCase();
69 String b=split.length>1 ? split[1] : null;
70
71 if(parser.parse(arg, a, b)){//Parse standard flags in the parser
72 //do nothing
73 }else if(a.equals("verbose")){
74 verbose=Parse.parseBoolean(b);
75 }else if(a.equals("parse_flag_goes_here")){
76 //Set a variable here
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
86 overwrite=parser.overwrite;
87 append=parser.append;
88
89 in1=parser.in1;
90
91 out1=parser.out1;
92
93 maxLines=parser.maxReads;
94 }
95
96 //Ensure there is an input file
97 if(in1==null){throw new RuntimeException("Error - at least one input file is required.");}
98
99 //Ensure output files can be written
100 if(!Tools.testOutputFiles(overwrite, append, false, out1)){
101 outstream.println((out1==null)+", "+out1);
102 throw new RuntimeException("\n\noverwrite="+overwrite+"; Can't write to output file "+out1+"\n");
103 }
104
105 //Ensure input files can be read
106 if(!Tools.testInputFiles(false, true, in1)){
107 throw new RuntimeException("\nCan't read some input files.\n");
108 }
109
110 //Ensure that no file was specified multiple times
111 if(!Tools.testForDuplicateFiles(true, in1, out1)){
112 throw new RuntimeException("\nSome file names were specified multiple times.\n");
113 }
114
115 //Create output FileFormat objects
116 ffout1=FileFormat.testOutput(out1, FileFormat.TEXT, null, true, overwrite, append, false);
117
118 //Create input FileFormat objects
119 ffin1=FileFormat.testInput(in1, FileFormat.TEXT, null, true, true);
120 }
121
122 /*--------------------------------------------------------------*/
123 /*---------------- Outer Methods ----------------*/
124 /*--------------------------------------------------------------*/
125
126 /** Create read streams and process all data */
127 void process(Timer t){
128
129 ArrayList<IDNode> list=new ArrayList<IDNode>();
130 TextFile tf=new TextFile(in1);
131 for(String line=tf.nextLine(); line!=null; line=tf.nextLine()){
132 if(maxLines>=0 && list.size()>=maxLines){break;}
133 if(!line.startsWith("#")){
134 String[] split=line.split("\t");
135 double[] array=new double[list.size()];
136 for(int i=0; i<array.length; i++){
137 array[i]=Double.parseDouble(split[i+1]);
138 }
139 IDNode n=new IDNode(array, list.size(), split[0]);
140 list.add(n);
141 }
142 }
143 tf.close();
144
145 IDNode[] nodes=list.toArray(new IDNode[0]);
146 IDNode head=IDNode.makeTree(nodes);
147
148 StringBuilder sb=head.toNewick();
149 sb.append(';');
150
151 if(out1!=null){
152 ReadWrite.writeString(sb, out1);
153 outstream.println("Wrote tree to "+out1);
154 }
155
156 t.stop();
157 outstream.println("Time: \t"+t);
158 }
159
160 /*--------------------------------------------------------------*/
161 /*---------------- Inner Methods ----------------*/
162 /*--------------------------------------------------------------*/
163
164 /**
165 * Process a single read pair.
166 * @param r1 Read 1
167 * @param r2 Read 2 (may be null)
168 * @return True if the reads should be kept, false if they should be discarded.
169 */
170 boolean processReadPair(final Read r1, final Read r2){
171 throw new RuntimeException("TODO");
172 }
173
174 /*--------------------------------------------------------------*/
175 /*---------------- Fields ----------------*/
176 /*--------------------------------------------------------------*/
177
178 /** Primary input file path */
179 private String in1=null;
180
181 /** Primary output file path */
182 private String out1=null;
183
184 /*--------------------------------------------------------------*/
185
186 /** Number of lines processed */
187 protected long linesProcessed=0;
188
189 protected long maxLines=-1;
190
191 /*--------------------------------------------------------------*/
192 /*---------------- Final Fields ----------------*/
193 /*--------------------------------------------------------------*/
194
195 /** Primary input file */
196 private final FileFormat ffin1;
197
198 /** Primary output file */
199 private final FileFormat ffout1;
200
201 /*--------------------------------------------------------------*/
202 /*---------------- Common Fields ----------------*/
203 /*--------------------------------------------------------------*/
204
205 /** Print status messages to this output stream */
206 private PrintStream outstream=System.err;
207 /** Print verbose messages */
208 public static boolean verbose=false;
209 /** True if an error was encountered */
210 public boolean errorState=false;
211 /** Overwrite existing output files */
212 private boolean overwrite=false;
213 /** Append to existing output files */
214 private boolean append=false;
215
216 }