-
Notifications
You must be signed in to change notification settings - Fork 6
Expand file tree
/
Copy pathAggregateDataCLI.java
More file actions
222 lines (203 loc) · 7.41 KB
/
AggregateDataCLI.java
File metadata and controls
222 lines (203 loc) · 7.41 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
package scriptmanager.cli.Read_Analysis;
import picocli.CommandLine.ArgGroup;
import picocli.CommandLine.Command;
import picocli.CommandLine.Option;
import picocli.CommandLine.Parameters;
import java.io.File;
import java.io.IOException;
import java.util.Scanner;
import java.util.ArrayList;
import java.util.concurrent.Callable;
import scriptmanager.objects.ToolDescriptions;
import scriptmanager.objects.Exceptions.OptionException;
import scriptmanager.scripts.Read_Analysis.AggregateData;
/**
* Command line interface for
* {@link scriptmanager.scripts.Read_Analysis.AggregateData}
*
* @author Olivia Lang
*/
@Command(name = "aggregate-data", mixinStandardHelpOptions = true,
description = ToolDescriptions.aggregate_data_description,
version = "ScriptManager "+ ToolDescriptions.VERSION,
sortOptions = false,
exitCodeOnInvalidInput = 1,
exitCodeOnExecutionException = 1)
public class AggregateDataCLI implements Callable<Integer> {
/**
* Creates a new AggreagateDataCLI object
*/
public AggregateDataCLI() {}
@Parameters( index="0..", description = "The matrix files whose statistics we want.")
private File[] matrixFiles;
@Option(names = {"-f", "--files"}, description = "Input file list of matrix filepaths to aggregate (formatted so each path is on its own line)")
private boolean fileList = false;
@Option(names = {"-o", "--output"}, description = "Specify output file (default = <input1>_SCORES.out, <input2_SCORES.out, ... or ALL_SCORES.out if -m flag is used)")
private File output;
@Option(names = {"-z", "--gzip"}, description = "gzip output (default=false)")
private boolean gzOutput = false;
@ArgGroup(exclusive = true, heading = "Aggregation Method%n")
AggType aggr = new AggType();
static class AggType{
@Option(names = {"--sum"}, description = "use summation method(default)")
private boolean sum = false;
@Option(names = {"--avg"}, description = "use average method")
private boolean avg = false;
@Option(names = {"--med"}, description = "use median method")
private boolean med = false;
@Option(names = {"--mod"}, description = "use mode method")
private boolean mod = false;
@Option(names = {"--min"}, description = "use minimum method")
private boolean min = false;
@Option(names = {"--max"}, description = "use maximum method")
private boolean max = false;
@Option(names = {"--var"}, description = "use positional variance method")
private boolean var = false;
}
@Option(names = {"-m", "--merge"}, description = "merge to one file")
private boolean merge = false;
@Option(names = {"-r", "--start-row"}, description = "")
private int startROW = 2;
@Option(names = {"-l", "--start-col"}, description = "")
private int startCOL = 3;
private int aggType = 0;
private ArrayList<File> matFiles = new ArrayList<File>();
/**
* Runs when this subcommand is called, running script in respective script package with user defined arguments
* @throws IOException Invalid file or parameters
*/
@Override
public Integer call() throws Exception {
System.err.println( ">AggregateDataCLI.call()" );
String validate = validateInput();
if(!validate.equals("")){
System.err.println( validate );
System.err.println("Invalid input. Check usage using '-h' or '--help'");
System.exit(1);
}
AggregateData script_obj = new AggregateData(matFiles, output, merge, startROW, startCOL, aggType, gzOutput);
script_obj.run();
return(0);
}
private String validateInput() throws IOException {
String r = "";
if(matrixFiles==null){
r += "(!)Please indicate at least one file.\n";
return(r);
//Import files as Vector list (scan input file if -f flag used)
}else if(fileList){ //load files from input filelist
if(matrixFiles.length>1){
r += "(!)Please indicate only one file with matrix filepaths when using the -f flag.\n";
return(r);
}else if(!matrixFiles[0].exists()){
r += "(!)File of list of file inputs does not exist: " + matrixFiles[0].getCanonicalPath() + "\n";
return(r);
}else{
Scanner scan = new Scanner(matrixFiles[0]);
while (scan.hasNextLine()) {
matFiles.add(new File(scan.nextLine().trim()));
}
scan.close();
}
}else{ //load input files into bam vector
for(int x=0; x<matrixFiles.length; x++){
matFiles.add(matrixFiles[x]);
}
}
//check each file in Vector
for(int x=0; x<matFiles.size(); x++){
File MAT = matFiles.get(x);
//no ext check
//check input exists
if(!MAT.exists()|| MAT.isDirectory()){
r += "(!)matrix[" + x + "] file does not exist: " + MAT.getName() + "\n";
}
}
if(!r.equals("")){ return(r); }
//check output filename is valid
if(output!=null){
//no check ext
//check directory
if( output.isDirectory() ){
if( !output.exists() ){
r += "(!)Directory must exist. Check your -o directory path.";
}
//check file
} else {
//validate file if merge, copy error message otherwise
if( merge ){
if( output.getParent()==null ){
// System.err.println("output file to current working directory");
} else if(!new File(output.getParent()).exists()){
r += "(!)Check directory of output file exists: " + output.getParent() + "\n";
}
} else if(matFiles.size()!=1){
r += "(!)Must indicate directory (not a file) if you're not using the merge option.";
}
}
} else {
output = new File(System.getProperty("user.dir"));
}
//Set numeric indicator for aggregation method
if(aggr.avg) { aggType = AggregateData.AVERAGE; }
else if(aggr.med) { aggType = AggregateData.MEDIAN; }
else if(aggr.mod) { aggType = AggregateData.MODE; }
else if(aggr.min) { aggType = AggregateData.MINIMUM; }
else if(aggr.max) { aggType = AggregateData.MAXIMUM; }
else if(aggr.var) { aggType = AggregateData.POSITIONAL_VARIANCE; }
//validate row&column start indexes
if(startROW<0){ r += "(!)Row start must not be less than zero\n"; }
if(startCOL<0){ r += "(!)Column start must not be less than zero\n"; }
return(r);
}
/**
* Reconstruct CLI command
*
* @param in ArrayList of TAB files to be processed
* @param output Output directory
* @param m Whether results should be merged into one file
* @param r Starting row (1-indexed)
* @param l Starting column (1-indexed)
* @param index the metric to aggregate by
* @param gzOutput gzip output
* @return command line to execute with formatted inputs
* @throws OptionException
*/
public static String getCLIcommand(ArrayList<File> in, File output, boolean m, int r, int l, int index, boolean gzOutput) throws OptionException {
String command = "java -jar $SCRIPTMANAGER read-analysis aggregate-data";
command += " -o " + output.getAbsolutePath();
command += gzOutput ? " --gzip" : "";
command += m ? " -m" : "";
switch (index) {
case AggregateData.SUM:
command += " --sum";
break;
case AggregateData.AVERAGE:
command += " --avg";
break;
case AggregateData.MEDIAN:
command += " --med";
break;
case AggregateData.MODE:
command += " --mod";
break;
case AggregateData.MINIMUM:
command += " --min";
break;
case AggregateData.MAXIMUM:
command += " --max";
break;
case AggregateData.POSITIONAL_VARIANCE:
command += " --var";
break;
default:
throw new OptionException("invalid scaling type value");
}
command += " -r " + r;
command += " -l " + l;
for (int i = 0; i<in.size(); i++) {
command += " " + in.get(i).getAbsolutePath();
}
return (command);
}
}