-
Notifications
You must be signed in to change notification settings - Fork 0
/
msreduce.java
701 lines (574 loc) · 23.1 KB
/
msreduce.java
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
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
/*
This program accepts a folder of .dta files, percentage data
to be retained and the output folder as arguments. The output
folder contains .ms2 file which can be directly used with
Crux-Tide search software.
java msreduce ./inputFolder xx ./outputFolder
xx = percentage data to be retained e.g. 10, 20, 30.
Copyright (C) Muaaz Gul Awan and Fahad Saeed
This program 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 2
of the License, or (at your option) any later version.
This program 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 program; if not, write to the Free Software
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
*/
import java.io.BufferedReader;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.io.PrintWriter;
import java.io.UnsupportedEncodingException;
import static java.lang.Math.floor;
import java.util.ArrayList;
import java.util.Random;
import java.text.DecimalFormat;
public class msreduce {
public static String firstLine;
static float sample_amount;
static double dataSizeA = 0,dataSizeN=0,timeStart,timeEnd,totalTime=0,timeStartWithSort,timeEndWithSort,totalTimeWithSort,timeStartWidth,timeEndWidth,totalTimeWidth;
static String targetFolder;
public static void main(String[] args) throws FileNotFoundException, IOException {
targetFolder = args[0];
sample_amount = Float.parseFloat(args[1]);
final File folder = new File(targetFolder);
String temp;
String outPutFolder = args[2];
int fSize=0;
float bias_val = (float) 10;
float avgWidth=0;
avgWidth = getAvgWidth(folder, targetFolder);
PrintWriter writer = new PrintWriter(outPutFolder +sample_amount+ ".ms2", "UTF-8");
int myScans = 0;
for (final File fileEntry : folder.listFiles()) {
if (fileEntry.isDirectory()) {
listFilesForFolder(fileEntry);
} else {
temp = fileEntry.getName();
ArrayList<String> vals = sampleReader(targetFolder + temp);
//System.out.println("file: "+temp);
myScans++;
if(vals.size() >15){
sampleWriter(temp, writer, sample_amount, vals, bias_val, fSize,avgWidth, myScans);
// System.out.println("size:"+vals.size());
}
}
}
System.out.println("Percentage of Data Retained: "+(((dataSizeN/dataSizeA)*100)));
//System.out.println("time taken: "+totalTime);
//System.out.println("peaks retained:"+dataSizeN);
//System.out.println("peaks total:"+dataSizeA);
writer.close();
}
public static ArrayList<String> sampleReader(String File_Name) throws FileNotFoundException, IOException {
BufferedReader reader = new BufferedReader(new FileReader(File_Name));
ArrayList<String> vals = new ArrayList<String>();
String str;
int i = 0;
while ((str = reader.readLine()) != null) {
if (i == 0) {
firstLine = str;
i++;
} else {
vals.add(str);
}
}
reader.close();
return vals;
}
public static void sampleWriter(String source, PrintWriter writer, float sample_val, ArrayList<String> data, float bias_val, int fSize, float avgWidth, int myScan) throws FileNotFoundException, UnsupportedEncodingException {
String[] headsName = source.split("\\.");
String[] headsFile = firstLine.split(" ");
float prec_mass_peptide;
ArrayList<Peak> dataSpectrum = new ArrayList<Peak>();
ArrayList<ArrayList<Peak>> quants;
float spectrumWidth;
dataSizeA += data.size();
dataSpectrum = stringToSpectrum(data);
ArrayList<Peak> recursList = new ArrayList<Peak>();
spectrumWidth = getWidth(dataSpectrum);
float intensitySpread = (spectrumWidth/avgWidth)*100;
float peaksReq = (sample_val/100)*data.size();
timeStart = System.currentTimeMillis();
//below is the classification code
//5 levels
if (intensitySpread < 25){
quants = quantization(dataSpectrum, 5);
dataSpectrum = dynamicTune(quants, recursList, peaksReq, 5, 4);
}
//7 levels
else if ((intensitySpread >=25) && (intensitySpread < 50)){
quants = quantization(dataSpectrum, 7);
dataSpectrum = dynamicTune(quants, recursList, peaksReq, 5, 6);
}
//9 levels
else if ((intensitySpread >=50) && (intensitySpread < 75)){
quants = quantization(dataSpectrum, 9);
dataSpectrum = dynamicTune(quants, recursList, peaksReq, 5, 8);
}
//11 levels
else if ((intensitySpread)>=75){
quants = quantization(dataSpectrum, 11);
dataSpectrum = dynamicTune(quants, recursList, peaksReq, 5, 10);
}
dataSpectrum = sortSpectraInsertion(dataSpectrum);
timeEnd = System.currentTimeMillis();
totalTime += timeEnd - timeStart;
dataSizeN +=dataSpectrum.size();
//converting protonated mass value of precursor ion from .dta file to m/z of the precursor ion
prec_mass_peptide = (float) ((Float.parseFloat(headsFile[0]) + (Float.parseFloat(headsFile[1]) - 1))) / (Float.parseFloat(headsFile[1]));
if(headsName.length > 2)
writer.println("S\t" + headsName[2] + "\t" + headsName[2] + "\t" + String.format("%.6f",prec_mass_peptide));
else
writer.println("S\t" + " " + "\t" + " " + "\t" + String.format("%.6f",prec_mass_peptide));
writer.println("Z\t" + headsFile[1] + "\t" + headsFile[0]);
//writer.flush();
//writing everything to files now
for (int i = 0; i < dataSpectrum.size(); i++) {
writer.println(String.format("%.6f",dataSpectrum.get(i).m_z) + "\t" + String.format("%.6f",dataSpectrum.get(i).intensity));
writer.flush();
}
}
//lists all the files in a given folder
public static void listFilesForFolder(final File folder) throws FileNotFoundException, UnsupportedEncodingException {
PrintWriter writer = new PrintWriter("files.txt", "UTF-8");
for (final File fileEntry : folder.listFiles()) {
if (fileEntry.isDirectory()) {
listFilesForFolder(fileEntry);
} else {
writer.println(fileEntry.getName());
}
}
}
//sorts spectra
public static ArrayList<Integer> sort(ArrayList<Integer> lst) {
int temp;
int largest = 0;
for (int j = 0; j < lst.size(); j++) {
for (int i = 0; i < lst.size() - 1; i++) {
largest = lst.get(i);
temp = lst.get(i + 1);
if (temp < largest) {
lst.set(i, temp);
lst.set(i + 1, largest);
}
}
}
return lst;
}
//sorts spectra wrt m_z values
public static ArrayList<Peak> sortSpectra(ArrayList<Peak> input) {
ArrayList<Peak> result = new ArrayList<Peak>();
Peak temp = new Peak();
Peak max = new Peak();
if (input.size() > 0) {
max.m_z = input.get(0).m_z;
}
for (int j = 0; j < input.size(); j++) {
for (int i = 0; i < input.size() - 1; i++) {
max = input.get(i);
temp = input.get(i + 1);
if (temp.m_z < max.m_z) {
input.set(i, temp);
input.set(i + 1, max);
}
}
}
return input;
}
//sorts spectra wrt m_z values using insertion sort
public static ArrayList<Peak> sortSpectraInsertion(ArrayList<Peak> input) {
int j;
for(int i = 1; i < input.size(); i++){
j=i;
while(j>0 && input.get(j-1).m_z > input.get(j).m_z){
Peak temp = input.get(j-1);
input.set(j-1, input.get(j));
input.set(j, temp);
j--;
}
}
return input;
}
//sorts spectra by intensity
public static ArrayList<Peak> sortSpectraByIntensity(ArrayList<Peak> input) {
Peak temp = new Peak();
Peak max = new Peak();
if (input.size() > 0) {
max.intensity = input.get(0).intensity;
}
for (int j = 0; j < input.size(); j++) {
for (int i = 0; i < input.size() - 1; i++) {
max = input.get(i);
temp = input.get(i + 1);
if (temp.intensity < max.intensity) {
input.set(i, temp);
input.set(i + 1, max);
}
}
}
return input;
}
//sorts spectra by intensity using isnertion sort
public static ArrayList<Peak> sortSpectraByIntensityInsertion(ArrayList<Peak> input) {
int j;
for(int i = 1; i < input.size(); i++){
j=i;
while(j>0 && input.get(j-1).intensity > input.get(j).intensity){
Peak temp = input.get(j-1);
input.set(j-1, input.get(j));
input.set(j, temp);
j--;
}
}
return input;
}
//performs skewed sampling on spectra
public static ArrayList<Peak> skewSampling(ArrayList<Peak> input, float threshold, Peak maxIntensity) {
ArrayList<Peak> output = new ArrayList<Peak>();
float thresholdVal = threshold;
float thresholdIntensity;
thresholdIntensity = thresholdVal * maxIntensity.intensity;
for (int i = 0; i < input.size(); i++) {
if (input.get(i).intensity > thresholdIntensity) {
output.add(input.get(i));
}
}
return output;
}
//finds maximum wrt intensity
public static Peak maxPeak(ArrayList<Peak> input) {
Peak max = new Peak();
max = input.get(0);
for (int i = 0; i < input.size(); i++) {
if (max.intensity < input.get(i).intensity) {
max = input.get(i);
}
}
return max;
}
//finds smallest peak wrt intensity
public static Peak minPeak(ArrayList<Peak> input) {
Peak min = new Peak();
min = input.get(0);
for (int i = 0; i < input.size(); i++) {
if (min.intensity > input.get(i).intensity) {
min = input.get(i);
}
}
return min;
}
//randomly samples a spectrum based on the sampling rate provided
public static ArrayList<String> randomSampler(ArrayList<String> data, float sample_val) {
Random random = new Random();
ArrayList<Integer> indexList = new ArrayList<Integer>();
ArrayList<String> outList = new ArrayList<String>();
float percentage = (float) sample_val / 100;
int ind = 0;
while (indexList.size() < floor(data.size() * percentage)) {
ind = random.nextInt(data.size());
if (!indexList.contains(ind)) {
indexList.add(ind);
}
}
for (int i = 0; i < floor(data.size() * percentage); i++) {
outList.add(data.get(indexList.get(i)));
}
return outList;
}
/*
converts string-spectrum to spectrum-class
*/
public static ArrayList<Peak> stringToSpectrum(ArrayList<String> input) {
ArrayList<Peak> output = new ArrayList<Peak>();
Peak tempSpectrum;
String temp;
String[] temp2;
for (int i = 0; i < input.size(); i++) {
tempSpectrum = new Peak();
temp = input.get(i);
temp2 = temp.split("\\ ");
tempSpectrum.intensity = Float.parseFloat(temp2[1]);
tempSpectrum.m_z = Float.parseFloat(temp2[0]);
output.add(i, tempSpectrum);
}
return output;
}
//creates fsets
public static ArrayList<Fset> createFsets(ArrayList<Peak> data, int fSize) {
ArrayList<Fset> Fsets = new ArrayList<Fset>();
for (int i = 0; i < (data.size() - (fSize - 1)); i++) {
Fset temp = new Fset();
for (int j = i; j < i + fSize; j++) {
temp.sets.add(data.get(j));
}
Fsets.add(temp);
}
return Fsets;
}
//randomly samples Fsets
public static ArrayList<Fset> randomSamplerFset(ArrayList<Fset> data, float sample_val) {
Random random = new Random();
ArrayList<Integer> indexList = new ArrayList<Integer>();
ArrayList<Fset> outList = new ArrayList<Fset>();
float percentage = (float) sample_val / 100;
int ind = 0;
while (indexList.size() < floor(data.size() * percentage)) {
ind = random.nextInt(data.size());
if (!indexList.contains(ind)) {
indexList.add(ind);
}
}
for (int i = 0; i < floor(data.size() * percentage); i++) {
outList.add(data.get(indexList.get(i)));
}
return outList;
}
// merges Fsets
public static ArrayList<Peak> FsetsMergeToSpectrum(ArrayList<Fset> Fsets) {
ArrayList<Peak> spectrum = new ArrayList<Peak>();
for (int i = 0; i < Fsets.size(); i++) {
for (int j = 0; j < Fsets.get(i).sets.size(); j++) {
if (!spectrum.contains(Fsets.get(i).sets.get(j))) {
spectrum.add(Fsets.get(i).sets.get(j));
}
}
}
return spectrum;
}
/*
filters fsets based on a threshold
*/
public static Fset filterFset(Fset setIn, int opt, float threshold) {
Fset setOut = new Fset();
Peak maxPeak,minPeak;
float cutOffVal;
if (opt == 1) {
maxPeak = maxPeak(setIn.sets);
cutOffVal = maxPeak.intensity * (threshold / 100);
for (int i = 0; i < setIn.sets.size(); i++) {
if (setIn.sets.get(i).intensity > cutOffVal) {
setOut.sets.add(setIn.sets.get(i));
}
}
} else if (opt == 0) {
minPeak = minPeak(setIn.sets);
cutOffVal = minPeak.intensity * (threshold / 100);
for (int i = 0; i < setIn.sets.size(); i++) {
if (setIn.sets.get(i).intensity > cutOffVal) {
setOut.sets.add(setIn.sets.get(i));
}
}
} else if (opt == 2) {
float sum = 0;
for (int i = 0; i < setIn.sets.size(); i++) {
sum += setIn.sets.get(i).intensity;
}
cutOffVal = (sum / setIn.sets.size()) * (threshold / 100);
for (int i = 0; i < setIn.sets.size(); i++) {
if (setIn.sets.get(i).intensity > cutOffVal) {
setOut.sets.add(setIn.sets.get(i));
}
}
}
else
System.out.println("ERRORR!!!! Wrong Selection for Filter");
return setOut;
}
/*
Fset related calculations
*/
public static ArrayList<Fset> filterFsetList(ArrayList<Fset> listIn, float threshold, int opt){
ArrayList<Fset> listOut = new ArrayList<Fset>();
for( int i = 0; i < listIn.size(); i++)
listOut.add(filterFset(listIn.get(i),opt , threshold));
return listOut;
}
/*
calculates the average intensity of largest 10 peaks
*/
public static float avgMax10Peaks(ArrayList<Peak> unsortedIn){
float avg=0,sum = 0;
ArrayList<Peak> sortedIn = sortSpectraByIntensityInsertion(unsortedIn);
ArrayList<Peak> max10 = new ArrayList<Peak>();
if(sortedIn.size() > 14){
for( int i = sortedIn.size()-1; i > sortedIn.size()-11 ; i--)
max10.add(sortedIn.get(i));
for (int i = 0; i < max10.size(); i++)
sum += max10.get(i).intensity;
}
avg = sum/10;
return avg;
}
/*
calculates the average intensity of largest 3 peaks
*/
public static float avgMax3Peaks(ArrayList<Peak> unsortedIn){
float avg=0,sum = 0;
ArrayList<Peak> sortedIn = sortSpectraByIntensityInsertion(unsortedIn);
ArrayList<Peak> max3 = new ArrayList<Peak>();
for( int i = sortedIn.size()-1; i > sortedIn.size()-4 ; i--)
max3.add(sortedIn.get(i));
for (int i = 0; i < max3.size(); i++)
sum += max3.get(i).intensity;
avg = sum/3;
return avg;
}
/*
calculates the average intensity of smallest 10 peaks
*/
public static float avgMin10Peaks(ArrayList<Peak> unsortedIn){
float avg=0,sum = 0;
ArrayList<Peak> sortedIn = sortSpectraByIntensityInsertion(unsortedIn);
ArrayList<Peak> min10 = new ArrayList<Peak>();
if(unsortedIn.size() > 14){
for( int i = 0; i <10 ; i++)
min10.add(sortedIn.get(i));
for (int i = 0; i < min10.size(); i++)
sum += min10.get(i).intensity;
}
avg = sum/10;
return avg;
}
//the output list is unsorted.
/**
quantizes given spectra
@param ArrayList<Peak> listIn
input spectra
@param int numSamples
number of quanta
**/
public static ArrayList<ArrayList<Peak>> quantization(ArrayList<Peak> listIn,int numSamples){
ArrayList<ArrayList<Peak>> outList = new ArrayList<ArrayList<Peak>>();
ArrayList<Peak> tempList = new ArrayList<Peak>();
float maxAvg, minAvg,testVar=0;
float jumpInc = (float)1/numSamples,jump=0,jumpLag=0;
float refVal = avgMax10Peaks(listIn);
for (int i = 0; i < numSamples; i++){
if(i == 0){
jumpLag = 0;
jump=jumpInc;
}
for (int j = 0; j < listIn.size(); j++){
if(i == numSamples-1){
if((listIn.get(j).intensity >= (refVal*jumpLag))){
tempList.add(listIn.get(j));
}
else{
}
}
else{
if((listIn.get(j).intensity >= (refVal*jumpLag)) && (listIn.get(j).intensity < (refVal*jump))){
tempList.add(listIn.get(j));
}
}
}
outList.add( new ArrayList<Peak>(tempList));
tempList.clear();
jump=jump+jumpInc;
jumpLag +=jumpInc;
}
return outList;
}
/**
randomly samples give spectra
@param ArrayList<Peak> data
input spectra
@param float sample_val
sampling rate
**/
public static ArrayList<Peak> randomSamplerPeaks(ArrayList<Peak> data, float sample_val) {
Random random = new Random();
ArrayList<Integer> indexList = new ArrayList<Integer>();
ArrayList<Peak> outList = new ArrayList<Peak>();
float percentage = (float) sample_val / 100;
int ind = 0;
while (indexList.size() < floor(data.size() * percentage)) {
ind = random.nextInt(data.size());
if (!indexList.contains(ind)) {
indexList.add(ind);
}
}
for (int i = 0; i < floor(data.size() * percentage); i++) {
outList.add(data.get(indexList.get(i)));
}
return outList;
}
public static float getWidth(ArrayList<Peak> listIn){
float width,maxAvg,minAvg;
maxAvg = avgMax10Peaks(listIn);
minAvg = avgMin10Peaks(listIn);
width = maxAvg-minAvg;
return width;
}
/**
recursively evaluates sampling weights for each quanta.
@param ArrayList<ArrayList<Peak>> quantsIn
quantized spectra
@param String ArrayList<Peak> sampledList
list containing the sampled spectra
@param float numOfPeaksReq
percentage of peaks required to be sampled
@param int tolPeaks
tolerance in number of peaks.
@param int index
starting index
**/
public static ArrayList<Peak> dynamicTune(ArrayList<ArrayList<Peak>> quantsIn, ArrayList<Peak> sampledList,float numOfPeaksReq, int tolPeaks,int index){
if ((quantsIn.get(index).size() >= numOfPeaksReq-tolPeaks) && (quantsIn.get(index).size() <= numOfPeaksReq+tolPeaks)){
for(int i=0; i < quantsIn.get(index).size(); i++)
sampledList.add(quantsIn.get(index).get(i));
return sampledList;
}
else if(quantsIn.get(index).size()> numOfPeaksReq+tolPeaks){
ArrayList<Peak> tempoHold = new ArrayList<Peak>();
tempoHold = randomSamplerPeaks(quantsIn.get(index), (numOfPeaksReq/quantsIn.get(index).size())*100);
for (int i = 0; i < tempoHold.size(); i++)
sampledList.add(tempoHold.get(i));
return sampledList;
}
else{
for(int i=0; i < quantsIn.get(index).size(); i++)
sampledList.add(quantsIn.get(index).get(i));
numOfPeaksReq = numOfPeaksReq - quantsIn.get(index).size();
index = index -1;
return dynamicTune(quantsIn, sampledList, numOfPeaksReq, tolPeaks, index);
}
}
/**
gets average width for spectra
@param File folder
folder object containing the spectra files for which width is to be calculated.
@param String targetFolder
address to the folder which contains spectra files
**/
public static float getAvgWidth(File folder,String targetFolder) throws FileNotFoundException, UnsupportedEncodingException, IOException{
float avgWidth=0,width=0,sum=0,total=0;
String temp;
ArrayList<Peak> testing;
for (final File fileEntry : folder.listFiles()) {
if (fileEntry.isDirectory()) {
listFilesForFolder(fileEntry);
} else {
temp = fileEntry.getName();
ArrayList<String> vals = sampleReader(targetFolder + temp);
//System.out.println("file: "+temp);
testing = stringToSpectrum(vals);
timeStartWidth = System.currentTimeMillis();
width = getWidth(testing);
sum+=width;
total++;
timeEndWidth = System.currentTimeMillis();
totalTimeWidth += timeEndWidth-timeStartWidth ;
}
}
avgWidth = sum/total;
return avgWidth;
}
}