-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathParseData.pde
267 lines (239 loc) · 10.4 KB
/
ParseData.pde
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
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
/*
* Copyright (c) 2010 The Jackson Laboratory
*
* This software was developed by Matt Hibbs' Lab at The Jackson
* Laboratory (see http://cbfg.jax.org/).
*
* This is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This software is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this software. If not, see <http://www.gnu.org/licenses/>.
*/
/**
* This module is a container for methods that parse and read data.
*/
/**
* This method is (only) called by loadFile in module QTLViewer.
*
* @param path the file being parsed by loadFile
* @return the modified path to be used for loading supplementary files
*/
String getModifiedPath(String path) {
String modifiedPath = "";
if (split(path, ".").length == 2) {
modifiedPath = split(path, ".")[0];
} else if (split(path, ".").length == 1) {
modifiedPath = path;
} else {
for (int i = 0; i < split(path, ".").length - 2; i++) {
modifiedPath += split(path, ".")[i] + ".";
}
if (modifiedPath.length() > 0) {
modifiedPath = modifiedPath.substring(0, modifiedPath.length() - 1);
}
}
return modifiedPath;
}
/**
* Associates threshold data with a phenotype.
*
* @param currentPhenotype the Phenotype object to add data to.
* @param thresholdData the data from readThresholds
* @return true or false, whether or not the operation was successful
*/
boolean addThresholdData(Phenotype currentPhenotype, ArrayList<HashMap<String, float[]>> thresholdData) {
try {
currentPhenotype.thresholds = new float[1][0];
float[] threshArray = thresholdData.get(0).get(currentPhenotype.name);
for (float threshValue : threshArray) {
currentPhenotype.thresholds[0] = append(currentPhenotype.thresholds[0], threshValue);
}
if (thresholdData.size() > 1) {
float[] threshXArray = thresholdData.get(1).get(currentPhenotype.name);
if (thresholdData.get(1).get(currentPhenotype.name) != null) {
currentPhenotype.thresholds = (float[][])append(currentPhenotype.thresholds, new float[0]);
for (float threshXValue : threshXArray) {
currentPhenotype.thresholds[1] = append(currentPhenotype.thresholds[1], threshXValue);
}
currentPhenotype.useXDefaults = false;
}
} else {
currentPhenotype.useXDefaults = true;
}
currentPhenotype.useDefaults = false;
} catch (Exception error) {
error.printStackTrace();
return false;
}
return true;
}
/**
* Associates threshold data with a phenotype.
*
* @param currentPhenotype the Phenotype object to add data to.
* @param csvThresh the CSV data from readCSV
* @param alphaCol the index of the alpha column
* @return true or false, whether or not the operation was successful
*/
boolean addThresholdData(Phenotype currentPhenotype, String[][] csvThresh, int alphaCol) {
try {
String mark = (alphaCol >= 0) ? csvThresh[1][alphaCol] : "";
int col = -1;
for (int j = (alphaCol == -1) ? 1 : 2; j < csvThresh[0].length; j++) {
if (csvThresh[0][j].equals(currentPhenotype.name)) {
col = j;
break;
}
}
currentPhenotype.thresholds = new float[1][0];
if (col == -1) {
throw new Exception(""); // not sure if this is an accepted practice, but it should work
}
for (int j = 1; j < csvThresh.length; j++) {
if (alphaCol > -1 && !csvThresh[j][alphaCol].equals(mark)) {
if (currentPhenotype.thresholds.length == 1) currentPhenotype.thresholds = (float[][])append(currentPhenotype.thresholds, new float[0]);
currentPhenotype.thresholds[1] = (float[])append(currentPhenotype.thresholds[1], float(csvThresh[j][col]));
currentPhenotype.useXDefaults = false;
} else {
currentPhenotype.thresholds[0] = (float[])append(currentPhenotype.thresholds[0], float(csvThresh[j][col]));
}
}
if (currentPhenotype.thresholds[0].length < 2) {
currentPhenotype.thresholds[0] = (float[])append(currentPhenotype.thresholds[0], -height);
}
if (currentPhenotype.thresholds.length > 1 && currentPhenotype.thresholds[1].length < 2) {
currentPhenotype.thresholds[1] = (float[])append(currentPhenotype.thresholds[1], -height);
}
currentPhenotype.useDefaults = false;
} catch (Exception error) {
error.printStackTrace();
return false;
}
return true;
}
/**
* Associates threshold data with a phenotype.
*
* @param currentPhenotype the Phenotype object to add data to.
* @param threshCSVFile the InputStreamReader representing the CSV file
* @return true or false, whether or not the operation was successful
*/
boolean addThreshCSVFile(Phenotype currentPhenotype, InputStreamReader threshCSVFile) {
try {
String[][] csvData = readCSV(threshCSVFile);
currentPhenotype.thresholds = new float[1][2];
currentPhenotype.thresholds[0][0] = float(csvData[1][1]);
currentPhenotype.thresholds[0][1] = float(csvData[2][1]);
if (csvData.length > 4) {
currentPhenotype.thresholds = (float[][])append(currentPhenotype.thresholds, new float[2]);
currentPhenotype.thresholds[1][0] = float(csvData[3][1]);
currentPhenotype.thresholds[1][1] = float(csvData[4][1]);
currentPhenotype.useXDefaults = false;
} else {
currentPhenotype.useXDefaults = true;
}
currentPhenotype.useDefaults = false;
} catch (Exception error) {
error.printStackTrace();
return false;
}
return true;
}
/**
* Associates peak data with a phenotype.
*
* @param currentPhenotype the Phenotype object to add data to.
* @param values the float matrix of peak ranges
* @return true or false, whether or not the operation was successful
*/
void addPeakData(Phenotype currentPhenotype, float[][] values) {
for (int j = 0; j < values.length; j++) {
if (values[j].length == 0) {
continue;
}
currentPhenotype.chr_chrs = append(currentPhenotype.chr_chrs, j + 1);
currentPhenotype.chr_peaks = append(currentPhenotype.chr_peaks, values[j][0]);
Range r = new Range();
r.upper = values[j][2];
r.lower = values[j][1];
currentPhenotype.bayesintrange = (Range[])append(currentPhenotype.bayesintrange, r);
}
}
/**
* Associates peak data with a phenotype.
*
* @param currentPhenotype the Phenotype object to add data to.
* @param csvData the data from readCSV
* @return true or false, whether or not the operation was successful
*/
boolean addPeakData(Phenotype currentPhenotype, String[][] csvData) {
try {
for (int j = 1; j < csvData.length; j++) {
if (csvData[j].length <= 1) {
continue;
}
if (csvData[j][0].startsWith(currentPhenotype.name)) {
currentPhenotype.chr_chrs = append(currentPhenotype.chr_chrs, getChr(csvData[j][1]));
Range r = new Range();
if (float(csvData[j][2]) > unitThreshold) {
currentPhenotype.chr_peaks = append(currentPhenotype.chr_peaks, (float)unitConverter.basePairsToCentimorgans(getChr(csvData[j][1]), Long.parseLong(csvData[j][2])));
r.lower = (float)unitConverter.basePairsToCentimorgans(getChr(csvData[j][1]), Long.parseLong(csvData[j][3]));
r.upper = (float)unitConverter.basePairsToCentimorgans(getChr(csvData[j][1]), Long.parseLong(csvData[j][4]));
} else {
currentPhenotype.chr_peaks = append(currentPhenotype.chr_peaks, float(csvData[j][2]));
r.lower = float(csvData[j][3]);
r.upper = float(csvData[j][4]);
}
currentPhenotype.bayesintrange = (Range[])append(currentPhenotype.bayesintrange, r);
}
}
} catch (Exception error) {
error.printStackTrace();
return false;
}
return true;
}
/**
* Associates peak data with a phenotype.
*
* @param currentPhenotype the Phenotype object to add data to.
* @param peakCSVFile the InputStreamReader to read CSV data from
* @return true or false, whether or not the operation was successful
*/
boolean addPeakCSVFile(Phenotype currentPhenotype, InputStreamReader peakCSVFile) {
try {
String[][] csvData = readCSV(peakCSVFile);
for (int j = 1; j < csvData.length; j++) {
if (csvData[j].length < 7) {
continue;
}
currentPhenotype.chr_chrs = append(currentPhenotype.chr_chrs, getChr(csvData[j][1]));
String range = csvData[j][6];
Range r = new Range();
String rangeUpper = range.split("-")[1].trim();
String rangeLower = range.split("-")[0].trim();
if (float(csvData[j][2]) > unitThreshold) {
currentPhenotype.chr_peaks = append(currentPhenotype.chr_peaks, (float)unitConverter.basePairsToCentimorgans(getChr(csvData[j][1]), Long.parseLong(csvData[j][2])));
r.lower = (float)unitConverter.basePairsToCentimorgans(getChr(csvData[j][1]), Long.parseLong(rangeLower));
r.upper = (float)unitConverter.basePairsToCentimorgans(getChr(csvData[j][1]), Long.parseLong(rangeUpper));
} else {
currentPhenotype.chr_peaks = append(currentPhenotype.chr_peaks, float(csvData[j][2]));
r.upper = float(rangeUpper);
r.lower = float(rangeLower);
currentPhenotype.bayesintrange = (Range[])append(currentPhenotype.bayesintrange, r);
}
}
} catch (Exception error) {
error.printStackTrace();
return false;
}
return true;
}