-
Notifications
You must be signed in to change notification settings - Fork 6
Expand file tree
/
Copy pathMZIdentXLFDR.java
More file actions
938 lines (765 loc) · 42.4 KB
/
Copy pathMZIdentXLFDR.java
File metadata and controls
938 lines (765 loc) · 42.4 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
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
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
/*
* Copyright 2015 Lutz Fischer <lfischer at staffmail.ed.ac.uk>.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.rappsilber.fdr;
import org.rappsilber.fdr.result.FDRResult;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.FileWriter;
import java.io.IOException;
import java.io.OutputStreamWriter;
import java.text.ParseException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.logging.Level;
import java.util.logging.Logger;
import org.rappsilber.fdr.entities.PSM;
import org.rappsilber.fdr.entities.PeptidePair;
import org.rappsilber.fdr.utils.StreamReplaceWriter;
import uk.ac.ebi.jmzidml.MzIdentMLElement;
import uk.ac.ebi.jmzidml.model.mzidml.AnalysisCollection;
import uk.ac.ebi.jmzidml.model.mzidml.AnalysisProtocolCollection;
import uk.ac.ebi.jmzidml.model.mzidml.AnalysisSoftware;
import uk.ac.ebi.jmzidml.model.mzidml.AnalysisSoftwareList;
import uk.ac.ebi.jmzidml.model.mzidml.AuditCollection;
import uk.ac.ebi.jmzidml.model.mzidml.Cv;
import uk.ac.ebi.jmzidml.model.mzidml.CvList;
import uk.ac.ebi.jmzidml.model.mzidml.CvParam;
import uk.ac.ebi.jmzidml.model.mzidml.DBSequence;
import uk.ac.ebi.jmzidml.model.mzidml.FragmentationTable;
import uk.ac.ebi.jmzidml.model.mzidml.Inputs;
import uk.ac.ebi.jmzidml.model.mzidml.Modification;
import uk.ac.ebi.jmzidml.model.mzidml.Param;
import uk.ac.ebi.jmzidml.model.mzidml.PeptideEvidence;
import uk.ac.ebi.jmzidml.model.mzidml.PeptideEvidenceRef;
import uk.ac.ebi.jmzidml.model.mzidml.ProteinDetectionHypothesis;
import uk.ac.ebi.jmzidml.model.mzidml.ProteinDetectionList;
import uk.ac.ebi.jmzidml.model.mzidml.Provider;
import uk.ac.ebi.jmzidml.model.mzidml.SequenceCollection;
import uk.ac.ebi.jmzidml.model.mzidml.SpectraData;
import uk.ac.ebi.jmzidml.model.mzidml.SpectrumIdentificationItem;
import uk.ac.ebi.jmzidml.model.mzidml.SpectrumIdentificationList;
import uk.ac.ebi.jmzidml.model.mzidml.SpectrumIdentificationResult;
import uk.ac.ebi.jmzidml.model.mzidml.SubstitutionModification;
import uk.ac.ebi.jmzidml.model.mzidml.UserParam;
import uk.ac.ebi.jmzidml.model.utils.MzIdentMLVersion;
import uk.ac.ebi.jmzidml.xml.io.MzIdentMLMarshaller;
import uk.ac.ebi.jmzidml.xml.io.MzIdentMLUnmarshaller;
public class MZIdentXLFDR extends OfflineFDR {
private MzIdentMLUnmarshaller unmarshaller;
//private URL xmlFileURL = JmzIdentMLParser.class.getClassLoader().getResource("CPTAC_Progenesis_Identifications.mzid");
//private URL xmlFileURL = JmzIdentMLParser.class.getClassLoader().getResource("55merge_mascot_full.mzid");
private String xmlFile;
private List<String> peptideList = new ArrayList();
private List<String> dbSequenceList = new ArrayList();
private List<String> matrix = new ArrayList();
private List<String> matrix_sii = new ArrayList();
private List<String> matrix_pe = new ArrayList();
private HashMap<String, PeptideEvidence> peptideEvidenceIdHashMap = new HashMap<String, PeptideEvidence>();
private HashMap<String, SpectraData> spectraDataIdHashMap = new HashMap<String, SpectraData>();
private HashMap<String, SpectrumIdentificationItem> siiIdHashMap = new HashMap<String, SpectrumIdentificationItem>();
private HashMap<SpectrumIdentificationItem, Double> psmScores = new HashMap<SpectrumIdentificationItem, Double>();
private HashMap<String, SpectrumIdentificationResult> siiIdToSirHashMap = new HashMap<String, SpectrumIdentificationResult>();
private HashMap<Integer, String> columnToScoreMap = new HashMap<Integer, String>();
private HashMap<Integer, String> columnToProtScoreMap = new HashMap<Integer, String>();
private HashMap<String, DBSequence> dbSequenceIdHashMap = new HashMap<String, DBSequence>();
private HashMap<String, ProteinDetectionHypothesis> pdhIdHashMap = new HashMap<String, ProteinDetectionHypothesis>();
private HashMap<String, List<ProteinDetectionHypothesis>> peptide_pdh_HashMap = new HashMap<String, List<ProteinDetectionHypothesis>>();
private HashMap<String, ArrayList<SpectrumIdentificationItem>> crosslinkedPSM = new HashMap<String, ArrayList<SpectrumIdentificationItem>>();
private HashMap<String, ArrayList<SpectrumIdentificationItem>> PSMidToCrosslinkedPSM = new HashMap<String, ArrayList<SpectrumIdentificationItem>>();
private HashMap<String, uk.ac.ebi.jmzidml.model.mzidml.Peptide> peptideIdHashMap = new HashMap<String, uk.ac.ebi.jmzidml.model.mzidml.Peptide>();
private ArrayList<SpectrumIdentificationItem> linearPSM = new ArrayList<SpectrumIdentificationItem>();
// private List<ProteinDetectionHypothesis> proteinDetectionHypothesisList = new ArrayList<ProteinDetectionHypothesis>();
// private List<ProteinAmbiguityGroup> proteinAmbiguityGroupList = new ArrayList<ProteinAmbiguityGroup>();
private ArrayList<SpectrumIdentificationResult> sirList = new ArrayList();
ProteinDetectionList proteinDetectionList = new ProteinDetectionList();
private String sep = ",";
private String pagHeader = "PAG ID" + sep + "PAG score" + sep + "protein accession" + sep + "Pass Threshold (Protein)" + sep + "description" + sep + "group membership" + sep;
private String spectrumHeader = "Raw data location" + sep + "Spectrum ID" + sep + "Spectrum Title" + sep + "Retention Time (s)" + sep;
private String psmHeader = "PSM_ID" + sep + "rank" + sep + "Pass Threshold" + sep + "Calc m/z" + sep + "Exp m/z" + sep + "Charge" + sep + "Sequence" + sep + "Modifications";
private String pScoreHeader = ""; //Protein score header will be set only after reading the file
private String scoreHeader = ""; //This will be set only after reading the file
private String endPsmHeader = sep + "proteinacc_start_stop_pre_post_;" + sep + "Is decoy";
private String representativeProteinAcc = "MS:1001591"; //Used to identify the representative of each group - only used for the one line export of PAGS
// protected String crosslinkedModAcc = "MS:8888888";
/**
* cvTerm used to identify modifications, that span several peptides
* This is the cvTerm for the Modification, that holds the mass of the cross-linker
*/
private String crosslinkedDonorModAcc = "MS:1002509";
/**
* cvTerm used to identify modifications, that span several peptides
* This is the cvTerm for the Modification, that holds a zero mass to denote the second (third, forth ...) site a cross-linker is attached to
*/
private String crosslinkedReceptorModAcc = "MS:1002510";
// protected String crosslinkedSIIAcc = "MS:9999999";
/**
* cvTerm used to identify members of cross-linked PSMs
*/
private String crosslinkedSIIAcc = "MS:1002511";
// /**
// * cvTerm used to identify members of cross-linked PSMs
// * This one denotes the "beta" peptide
// */
// private String crosslinkedReceptorSIIAcc = "MS:9999992";
/** Identifies a mzIdentML-file, that represents a cross-link search */
private String crosslinkedSearchAcc = "MS:9999XXX";
/** XiFDR mzIdenML - id*/
private String analysesSoftwareFDR =
" <AnalysisSoftware version=\"%XIFDRVERSION%\" name=\"XiFDR\" id=\"fdr_software\">\n" +
" </AnalysisSoftware>\n";
/**Used to identify modifications, that span several peptides*/
protected String PSMScore = "score";
private boolean scoreFound = false;
private Boolean isVerbose = true;
private boolean deletePassThreshold = true;
//
// public static void main(String[] args) {
// MZIdentXLFDR mzidToCsv = new MZIdentXLFDR();
//
//
// //TODO - Undecided which if any command line arguments to include - minimally need to know whether to export Peptides or PAGs
//
// if (args != null && args.length == 3) {
// mzidToCsv.unmarshaller = new MzIdentMLUnmarshaller(new File(args[0]));
// mzidToCsv.init(args[1],args[2]);
//
// } else {
//
// mzidToCsv.unmarshaller = new MzIdentMLUnmarshaller(new File(mzidToCsv.xmlFile));
// mzidToCsv.init("out.csv","exportPSMs");
// //mzidToCsv.init("out.csv","exportProteinGroups");
//
// //System.out.println("Error - correct usage MzIdentMLToCSV inputFile.mzid outputFile.csv [exportProteinGroups|exportPSMs|exportProteinsOnly]");
// //System.exit(1);
// }
//
//
// }
//
public void readMzIdentML(File f, boolean passThreshHoldOnly) throws FileNotFoundException, IOException, ParseException {
MzIdentMLUnmarshaller mzIdentMLUnmarshaller = new MzIdentMLUnmarshaller(f);
//MZIdentXLFDR mzidToCsv = new MZIdentXLFDR();
this.unmarshaller = mzIdentMLUnmarshaller;
this.init(passThreshHoldOnly);
}
private void init(boolean passThreshHoldOnly) {
try {
// BufferedWriter out = new BufferedWriter(new FileWriter(outputFile));
//Read all the objects we will need into hashes that are not automatically resolved by object reference
if (isVerbose) {
System.out.print("About to iterate over PepEvid...");
}
Logger.getLogger(this.getClass().getName()).log(Level.INFO,"About to iterate over PepEvid...");
Iterator<PeptideEvidence> iterPeptideEvidence = unmarshaller.unmarshalCollectionFromXpath(MzIdentMLElement.PeptideEvidence);
while (iterPeptideEvidence.hasNext()) {
PeptideEvidence peptideEvidence = iterPeptideEvidence.next();
peptideEvidenceIdHashMap.put(peptideEvidence.getId(), peptideEvidence);
}
if (isVerbose) {
System.out.println("...done");
System.out.print("About to iterate over uk.ac.ebi.jmzidml.model.mzidml.Peptide");
}
Logger.getLogger(this.getClass().getName()).log(Level.INFO,"About to iterate over uk.ac.ebi.jmzidml.model.mzidml.Peptide");
Iterator<uk.ac.ebi.jmzidml.model.mzidml.Peptide> iterPeptide = unmarshaller.unmarshalCollectionFromXpath(MzIdentMLElement.Peptide);
while (iterPeptide.hasNext()) {
uk.ac.ebi.jmzidml.model.mzidml.Peptide peptide = iterPeptide.next();
peptideIdHashMap.put(peptide.getId(), peptide);
}
if (isVerbose) {
System.out.println("...done");
System.out.print("About to iterate over Spectra Data");
}
Logger.getLogger(this.getClass().getName()).log(Level.INFO,"About to iterate over Spectra Data");
Iterator<SpectraData> iterSpectraData = unmarshaller.unmarshalCollectionFromXpath(MzIdentMLElement.SpectraData);
while (iterSpectraData.hasNext()) {
SpectraData spectraData = iterSpectraData.next();
spectraDataIdHashMap.put(spectraData.getId(), spectraData);
}
if (isVerbose) {
System.out.println("...done");
System.out.print("About to iterate over DBsequence");
}
Logger.getLogger(this.getClass().getName()).log(Level.INFO,"About to iterate over DBsequence");
Iterator<DBSequence> iterDBSequence = unmarshaller.unmarshalCollectionFromXpath(MzIdentMLElement.DBSequence);
while (iterDBSequence.hasNext()) {
DBSequence dbSequence = iterDBSequence.next();
dbSequenceIdHashMap.put(dbSequence.getId(), dbSequence);
}
if (isVerbose) {
System.out.println("...done");
System.out.print("About to iterate over PDH");
}
Logger.getLogger(this.getClass().getName()).log(Level.INFO,"About to iterate over PDH");
Iterator<ProteinDetectionHypothesis> iterPDH = unmarshaller.unmarshalCollectionFromXpath(MzIdentMLElement.ProteinDetectionHypothesis);
Integer pCounter = 0;
while (iterPDH.hasNext()) {
ProteinDetectionHypothesis pdh = iterPDH.next();
pdhIdHashMap.put(pdh.getId(), pdh);
for (CvParam cvParam : pdh.getCvParam()) {
if (cvParam.getAccession().equals("MS:1001591") || cvParam.getAccession().equals("MS:1001592") || cvParam.getAccession().equals("MS:1001593")
|| cvParam.getAccession().equals("MS:1001594") || cvParam.getAccession().equals("MS:1001595") || cvParam.getAccession().equals("MS:1001596")
|| cvParam.getAccession().equals("MS:1001597")
|| cvParam.getAccession().equals("MS:1001598")
|| cvParam.getAccession().equals("MS:1001599")) {
//do nothing - these are specifically handled
//ToDO this code could be improved using an array of values...
} else if (cvParam.getValue() != null) {
if (!columnToProtScoreMap.containsValue(cvParam.getName())) {
columnToProtScoreMap.put(pCounter, cvParam.getName());
pCounter++;
}
}
}
for (UserParam userParam : pdh.getUserParam()) {
if (!columnToProtScoreMap.containsValue(userParam.getName())) {
columnToProtScoreMap.put(pCounter, userParam.getName());
pCounter++;
}
}
}
for (int i = 0; i < pCounter; i++) {
pScoreHeader += columnToProtScoreMap.get(i) + sep;
}
//Now let's see what scores we have in the file
//TODO - I'm not sure this is the fastest way to parse the files; these are unmarshalled again below - inefficient?
//Iterator<SpectrumIdentificationItem> iterSII = unmarshaller.unmarshalCollectionFromXpath(MzIdentMLElement.SpectrumIdentificationItem);
Integer counter = 0;
if (isVerbose) {
System.out.println("...done");
System.out.print("About to iterate over SIR");
}
Logger.getLogger(this.getClass().getName()).log(Level.INFO,"About to iterate over SIR");
Iterator<SpectrumIdentificationResult> iterSIR = unmarshaller.unmarshalCollectionFromXpath(MzIdentMLElement.SpectrumIdentificationResult);
while (iterSIR.hasNext()) {
SpectrumIdentificationResult sir = iterSIR.next();
sirList.add(sir);
List<SpectrumIdentificationItem> listSII = sir.getSpectrumIdentificationItem();
for (SpectrumIdentificationItem sii : listSII) {
siiIdHashMap.put(sii.getId(), sii);
siiIdToSirHashMap.put(sii.getId(), sir);
boolean isCrosslinked = false;
for (CvParam cvParam : sii.getCvParam()) {
String cvpv = cvParam.getValue();
Double score = null;
if (cvpv != null) {
String accession = cvParam.getAccession();
if (accession.contentEquals(getCrosslinkedSIIAcc())) {
isCrosslinked = true;
ArrayList<SpectrumIdentificationItem> xlSIIs = crosslinkedPSM.get(cvpv);
if (xlSIIs == null) {
xlSIIs = new ArrayList<SpectrumIdentificationItem>();
crosslinkedPSM.put(cvpv, xlSIIs);
PSMidToCrosslinkedPSM.put(sii.getId(), xlSIIs);
}
xlSIIs.add(sii);
}
if (!columnToScoreMap.containsValue(cvParam.getName())) {
columnToScoreMap.put(counter, cvParam.getName());
counter++;
}
if (cvParam.getAccession().contentEquals(getPSMScore()) ||
cvParam.getName().contentEquals(getPSMScore()) ||
(score == null && cvParam.getName().toLowerCase().endsWith(":" + getPSMScore().toLowerCase()))) {
score = Double.parseDouble(cvParam.getValue());
psmScores.put(sii, score);
scoreFound =true;
}
}
}
if (!isCrosslinked) {
if (sii.isPassThreshold() || !passThreshHoldOnly)
linearPSM.add(sii);
}
}
}
for (int i = 0; i < counter; i++) {
scoreHeader += sep + columnToScoreMap.get(i);
}
if (isVerbose) {
System.out.println("...done");
if (scoreFound) {
System.out.println("Scores found");
} else {
System.err.println("!!!!!!!!!!!!!! NO SCORES FOUND !!!!!!!!!!!!!!!!!");
}
System.out.print("register linear matches");
}
if (scoreFound) {
Logger.getLogger(this.getClass().getName()).log(Level.INFO,"Scores found");
} else {
Logger.getLogger(this.getClass().getName()).log(Level.INFO,"!!!!!!!!!!!!!! NO SCORES FOUND !!!!!!!!!!!!!!!!!");
}
Logger.getLogger(this.getClass().getName()).log(Level.INFO,"register linear matches");
boolean linearDecoys = false;
for (SpectrumIdentificationItem sii : linearPSM) {
uk.ac.ebi.jmzidml.model.mzidml.Peptide pep = peptideIdHashMap.get(sii.getPeptideRef());
List<PeptideEvidenceRef> pepevs =sii.getPeptideEvidenceRef();
for (PeptideEvidenceRef pepevref : pepevs) {
PeptideEvidence pepev = peptideEvidenceIdHashMap.get(pepevref.getPeptideEvidenceRef());
String seqref = pepev.getDBSequenceRef();
int pepstart = pepev.getStart();
//int pepend = pepev.getEnd();
String pepSeq = pep.getPeptideSequence();
// if we have a loop link we need to get the link positions
ArrayList<Integer> linkPositions = new ArrayList<Integer>(2);
for (Modification m : pep.getModification()) {
pepSeq += " - " + m.getLocation() + " " + modToString(m);
// check wether we have a loop link
for (CvParam cvp : m.getCvParam()) {
String AccString = cvp.getAccession();
if (AccString.contentEquals(getCrosslinkedDonorModAcc()) || AccString.contentEquals(getCrosslinkedReceptorModAcc())) {
// ok we have a loop link
linkPositions.add(m.getLocation());
}
}
}
DBSequence seq = dbSequenceIdHashMap.get(seqref);
String acc = seq.getAccession();
String desc = seq.getName();
Double score = psmScores.get(sii);
if (score == null)
score = 0.0;
//boolean isDecoy = seq.getSearchDatabase().
if ( pepev.isIsDecoy())
linearDecoys = true;
if (linkPositions.size() > 0) {
if (linkPositions.size() == 2) {
addMatch(sii.getId(), pepSeq, null, pep.getPeptideSequence().length(), 0, linkPositions.get(0), linkPositions.get(1), pepev.isIsDecoy(), false, sii.getChargeState(),score, acc, desc, null, null, pepstart, pepstart, score,0, null);
} else {
System.err.println(sii.getId() + ": Currently only loop links with exactly two links within the peptide are supported - will add this match as linear (non-cross-linked) match");
Logger.getLogger(this.getClass().getName()).log(Level.WARNING, "{0}Currently only loop links with exactly two links within the peptide are supported - will add this match as linear (non-cross-linked) match", sii.getId());
addMatch(sii.getId(), pepSeq, null, pep.getPeptideSequence().length(), 0, -1, -1, pepev.isIsDecoy(), false, sii.getChargeState(),score, acc, desc, null, null, pepstart, pepstart, score,0, null);
}
} else {
addMatch(sii.getId(), pepSeq, null, pep.getPeptideSequence().length(), 0, -1, -1, pepev.isIsDecoy(), false, sii.getChargeState(),score, acc, desc, null, null, pepstart, pepstart, score,0, null);
}
}
}
if (isVerbose) {
System.out.println("...done");
if (linearDecoys) {
System.out.println("linear decoys found");
} else {
System.err.println("!!!!!!!!!! no linear decoys found!!!!!!!!!!!!!!");
}
System.out.print("register cross-linked matches");
}
Logger.getLogger(this.getClass().getName()).log(Level.INFO,"register cross-linked matches");
boolean crosslinkedDecoys = false;
int countCrosslinkedPSM = 0;
for (ArrayList<SpectrumIdentificationItem> xlsiis : crosslinkedPSM.values()) {
SpectrumIdentificationItem sii1 = xlsiis.get(0);
SpectrumIdentificationItem sii2 = xlsiis.get(1);
uk.ac.ebi.jmzidml.model.mzidml.Peptide pep1 = peptideIdHashMap.get(sii1.getPeptideRef());
List<PeptideEvidenceRef> pepevs1 =sii1.getPeptideEvidenceRef();
int peplinksite1 =-1;
String pepSeq1 = pep1.getPeptideSequence();
for (Modification m : pep1.getModification()) {
pepSeq1 += " - " + m.getLocation() + " " + modToString(m);
for (CvParam cvp : m.getCvParam()) {
if (cvp.getAccession().contentEquals(getCrosslinkedReceptorModAcc()) || cvp.getAccession().contentEquals(getCrosslinkedDonorModAcc())) {
peplinksite1 = m.getLocation();
break;
}
}
}
uk.ac.ebi.jmzidml.model.mzidml.Peptide pep2 = peptideIdHashMap.get(sii2.getPeptideRef());
List<PeptideEvidenceRef> pepevs2 =sii2.getPeptideEvidenceRef();
int peplinksite2 =-1;
String pepSeq2 = pep2.getPeptideSequence();
for (Modification m : pep2.getModification()) {
pepSeq2 += " - " + m.getLocation() + " " + modToString(m);
for (CvParam cvp : m.getCvParam()) {
if (cvp.getAccession().contentEquals(getCrosslinkedReceptorModAcc()) || cvp.getAccession().contentEquals(getCrosslinkedDonorModAcc())) {
peplinksite2 = m.getLocation();
break;
}
}
}
for (PeptideEvidenceRef pepevref1 : pepevs1) {
for (PeptideEvidenceRef pepevref2 : pepevs2) {
PeptideEvidence pepev1 = peptideEvidenceIdHashMap.get(pepevref1.getPeptideEvidenceRef());
String seqref1 = pepev1.getDBSequenceRef();
PeptideEvidence pepev2 = peptideEvidenceIdHashMap.get(pepevref2.getPeptideEvidenceRef());
String seqref2 = pepev2.getDBSequenceRef();
int pepstart1 = pepev1.getStart();
// int pepend1 = pepev1.getEnd();
int pepstart2 = pepev2.getStart();
// int pepend2 = pepev2.getEnd();
DBSequence seq1 = dbSequenceIdHashMap.get(seqref1);
String acc1 = seq1.getAccession();
String desc1 = seq1.getName();
Double score1 = psmScores.get(sii1);
DBSequence seq2 = dbSequenceIdHashMap.get(seqref2);
String acc2 = seq2.getAccession();
String desc2 = seq2.getName();
Double score2 = psmScores.get(sii2);
//boolean isDecoy = seq.getSearchDatabase().
double score = 0;
double peptide1score = 0;
double peptide2score = 0;
double scoreRatio = 0;
if (score1 == score2) {
score = score1;
scoreRatio = (4/5+pepSeq1.length()/pepSeq2.length())/2;
peptide1score = score *scoreRatio;
peptide1score = score * (1-scoreRatio);
} else {
// @TODO need to change that
score = score1+score2;
peptide1score = score1;
peptide2score = score2;
}
if (pepev1.isIsDecoy() || pepev2.isIsDecoy())
crosslinkedDecoys = true;
countCrosslinkedPSM++;
addMatch(sii1.getId(), pepSeq1, pepSeq2, pep1.getPeptideSequence().length(), pep2.getPeptideSequence().length(), peplinksite1, peplinksite2, pepev1.isIsDecoy(), pepev2.isIsDecoy(), sii1.getChargeState(),score, acc1, desc1, acc2, desc2, pepstart1, pepstart2, peptide1score, peptide2score, null);
}
}
}
if (isVerbose) {
System.out.println("...done");
}
if (crosslinkedDecoys == false) {
System.err.println("!!!!!!!!!!! NO crosslinked decoys found !!!!!!!!!!!!!!!!!");
Logger.getLogger(this.getClass().getName()).log(Level.WARNING,"!!!!!!!!!!! NO crosslinked decoys found !!!!!!!!!!!!!!!!!");
}
if (countCrosslinkedPSM == 0) {
System.err.println("!!!!!!!!!!! NO crosslinked PSM found !!!!!!!!!!!!!!!!!");
Logger.getLogger(this.getClass().getName()).log(Level.WARNING,"!!!!!!!!!!! NO crosslinked PSM found !!!!!!!!!!!!!!!!!");
}
} catch (Exception e) {
System.err.println(e.getMessage());
e.printStackTrace();
}
}
/*
* Method to convert an mzid Mod element into a string of type ModName@location
*/
private String modToString(Modification mod) {
String modString = "";
if (mod.getCvParam() != null) {
for (CvParam cvParam : mod.getCvParam()) {
modString += cvParam.getName();
}
} else {
if (mod.getMonoisotopicMassDelta() != null) {
modString += mod.getMonoisotopicMassDelta();
} else if (mod.getAvgMassDelta() != null) {
modString += mod.getAvgMassDelta();
}
}
if (mod.getLocation() != null) {
modString += ":" + mod.getLocation();
}
return modString;
}
/*
* Method to create and return a string representation of a substitution modification
*/
private String subModToString(SubstitutionModification subMod) {
return subMod.getOriginalResidue() + "=>" + subMod.getReplacementResidue() + "@" + subMod.getLocation();
}
// /**
// * Used to identify members of cross-linked PSMs
// * @return the crosslinkedSIIAcc
// */
// public String getCrosslinkedSIIAcc() {
// return crosslinkedSIIAcc;
// }
/**
// * Used to identify members of cross-linked PSMs
// * @param crosslinkedSIIAcc the crosslinkedSIIAcc to set
// */
// public void setCrosslinkedSIIAcc(String crosslinkedSIIAcc) {
// this.crosslinkedSIIAcc = crosslinkedSIIAcc;
// }
// /**
// * CvTerm that is used to identify modifications, that span several peptides
// * @return the crosslinkedModAcc
// */
// public String getCrosslinkedModAcc() {
// return crosslinkedModAcc;
// }
//
// /**
// * CvTerm that is used to identify modifications, that span several peptides
// * @param crosslinkedModAcc the crosslinkedModAcc to set
// */
// public void setCrosslinkedModAcc(String crosslinkedModAcc) {
// this.crosslinkedModAcc = crosslinkedModAcc;
// }
/**
* Used to identify modifications, that span several peptides
* @return the PSMScore
*/
public String getPSMScore() {
return PSMScore;
}
/**
* Used to identify modifications, that span several peptides
* @param PSMScore the PSMScore to set
*/
public void setPSMScore(String PSMScore) {
this.PSMScore = PSMScore;
}
protected void assignFDR(SpectrumIdentificationItem sii, String psmID) {
}
public void writeMZIdentML(String mzidFileName, FDRResult result) {
if (deletePassThreshold) {
for (SpectrumIdentificationItem sii : siiIdHashMap.values()) {
sii.setPassThreshold(false);
}
}
// for (PSM psm : getFDRLinearPSMs()) {
// String id = psm.getPsmID();
// SpectrumIdentificationItem sii = siiIdHashMap.get(id);
// sii.setPassThreshold(true);
// PeptidePair pp = psm.getFdrPeptidePair();
// if (pp != null) {
// CvParam cvParamfdrscore = new CvParam();
// Cv cv = new Cv();
// cv.setId("PSI-MS");
// cv.setUri("http://psidev.cvs.sourceforge.net/viewvc/*checkout*/psidev/psi/psi-ms/mzML/controlledVocabulary/psi-ms.obo");
// cv.setVersion("2.25.0");
// cv.setFullName("PSI-MS");
//
// cvParamfdrscore.setAccession("MS:1001364");
// cvParamfdrscore.setName("pep:global FDR");
// cvParamfdrscore.setValue(Double.toString(pp.getFDR()));
// cvParamfdrscore.setCv(cv);
// sii.getCvParam().add(cvParamfdrscore);
// }
// }
for (PSM psm : result.psmFDR) {
PeptidePair pp = psm.getFdrPeptidePair();
if (pp != null) {
if (pp.isLinear()) {
SpectrumIdentificationItem sii = siiIdHashMap.get(psm.getPsmID());
sii.setPassThreshold(true);
CvParam cvParamfdrscore = new CvParam();
Cv cv = new Cv();
cv.setId("PSI-MS");
cv.setUri("http://psidev.cvs.sourceforge.net/viewvc/*checkout*/psidev/psi/psi-ms/mzML/controlledVocabulary/psi-ms.obo");
cv.setVersion("2.25.0");
cv.setFullName("PSI-MS");
cvParamfdrscore.setAccession("MS:1001364");
cvParamfdrscore.setName("pep:global FDR");
cvParamfdrscore.setValue(Double.toString(pp.getFDR()));
cvParamfdrscore.setCv(cv);
sii.getCvParam().add(cvParamfdrscore);
} else {
ArrayList<SpectrumIdentificationItem> siiList = PSMidToCrosslinkedPSM.get(psm.getPsmID());
for (SpectrumIdentificationItem sii : siiList) {
CvParam cvParamfdrscore = new CvParam();
Cv cv = new Cv();
cv.setId("PSI-MS");
cv.setUri("http://psidev.cvs.sourceforge.net/viewvc/*checkout*/psidev/psi/psi-ms/mzML/controlledVocabulary/psi-ms.obo");
cv.setVersion("2.25.0");
cv.setFullName("PSI-MS");
cvParamfdrscore.setAccession("MS:1001364");
cvParamfdrscore.setName("pep:global FDR");
cvParamfdrscore.setValue(Double.toString(pp.getFDR()));
cvParamfdrscore.setCv(cv);
sii.getCvParam().add(cvParamfdrscore);
sii.setPassThreshold(true);
}
}
}
}
try {
String outFile = mzidFileName;
if (!outFile.endsWith(".mzid")) {
outFile = outFile + ".mzid";
}
FileOutputStream fwriter = new FileOutputStream(outFile);
OutputStreamWriter writer = new OutputStreamWriter(fwriter, "UTF-8");
MzIdentMLMarshaller marshaller;
marshaller = new MzIdentMLMarshaller(MzIdentMLVersion.Version_1_3);
writer.write(marshaller.createXmlHeader() + "\n");
AnalysisSoftwareList analysisSoftwareList;
AuditCollection auditCollection;
Provider provider;
AnalysisProtocolCollection analysisProtocolCollection;
CvList cvList;
AnalysisCollection analysisCollection;
Inputs inputs;
String searchDatabase_Ref;
cvList = unmarshaller.unmarshal(MzIdentMLElement.CvList);
//analysisSoftwareList = mzIdentML.getAnalysisSoftwareList();
analysisSoftwareList = unmarshaller.unmarshal(MzIdentMLElement.AnalysisSoftwareList);
//auditCollection = mzIdentML.getAuditCollection();
auditCollection = unmarshaller.unmarshal(MzIdentMLElement.AuditCollection);
//provider = mzIdentML.getProvider();
provider = unmarshaller.unmarshal(MzIdentMLElement.Provider);
// analysisProtocolCollection = mzIdentML.getAnalysisProtocolCollection();
analysisProtocolCollection = unmarshaller.unmarshal(MzIdentMLElement.AnalysisProtocolCollection);
//analysisCollection = mzIdentML.getAnalysisCollection();
analysisCollection = unmarshaller.unmarshal(MzIdentMLElement.AnalysisCollection);
//inputs = mzIdentML.getDataCollection().getInputs();
inputs = unmarshaller.unmarshal(MzIdentMLElement.Inputs);
searchDatabase_Ref = inputs.getSearchDatabase().get(0).getId();
writer.write(marshaller.createMzIdentMLStartTag("12345") + "\n");
if (cvList != null) {
marshaller.marshal(cvList, writer);
}
writer.write("\n");
AnalysisSoftware asXi = new AnalysisSoftware();
Param xiNameParam = new Param();
UserParam xiUP = new UserParam();
xiUP.setName("XiFDR");
xiNameParam.setParam(xiUP);
asXi.setSoftwareName(xiNameParam);
asXi.setVersion(OfflineFDR.getXiFDRVersion().toString());
if (analysisSoftwareList != null) {
analysisSoftwareList.getAnalysisSoftware().add(asXi);
marshaller.marshal(analysisSoftwareList, writer);
} else {
AnalysisSoftwareList asl = new AnalysisSoftwareList();
asl.getAnalysisSoftware().add(asXi);
marshaller.marshal(analysisSoftwareList, writer);
}
writer.write("\n");
if (provider != null) {
marshaller.marshal(provider, writer);
}
writer.write("\n");
if (auditCollection != null) {
marshaller.marshal(auditCollection, writer);
}
writer.write("\n");
SequenceCollection sequenceCollection = unmarshaller.unmarshal(MzIdentMLElement.SequenceCollection);
if (sequenceCollection != null) {
marshaller.marshal(sequenceCollection, writer);
}
writer.write("\n");
if (analysisCollection != null) {
marshaller.marshal(analysisCollection, writer);
}
writer.write("\n");
if (analysisProtocolCollection != null) {
marshaller.marshal(analysisProtocolCollection, writer);
}
writer.write("\n");
writer.write(marshaller.createDataCollectionStartTag() + "\n");
writer.write("\n");
if (inputs != null) {
marshaller.marshal(inputs, writer);
}
writer.write("\n");
writer.write(marshaller.createAnalysisDataStartTag() + "\n");
String spectrumIdentificationListRef = "";
if (analysisCollection.getSpectrumIdentification().size() > 0) {
spectrumIdentificationListRef = analysisCollection.getSpectrumIdentification().get(0).getSpectrumIdentificationListRef();
}
SpectrumIdentificationList siList;
siList = new SpectrumIdentificationList();
siList.setId(spectrumIdentificationListRef);
Iterator<FragmentationTable> iterFragmentationTable = unmarshaller.unmarshalCollectionFromXpath(MzIdentMLElement.FragmentationTable);
while (iterFragmentationTable.hasNext()) {
FragmentationTable fr = iterFragmentationTable.next();
siList.setFragmentationTable(fr);
}
HashMap<String, String> sii_stringMap = new HashMap();
Iterator<SpectrumIdentificationResult> iterSpectrumIdentificationResult = sirList.iterator();
while (iterSpectrumIdentificationResult.hasNext()) {
SpectrumIdentificationResult sr = iterSpectrumIdentificationResult.next();
siList.getSpectrumIdentificationResult().add(sr);
}
// if (siListList != null) {
//
// }
marshaller.marshal(siList, writer);
writer.write("\n");
writer.write(marshaller.createProteinDetectionListStartTag("PDL_1", null) + "\n");
writer.write(marshaller.createProteinDetectionListClosingTag() + "\n");
writer.write(marshaller.createAnalysisDataClosingTag() + "\n");
writer.write(marshaller.createDataCollectionClosingTag() + "\n");
writer.write(marshaller.createMzIdentMLClosingTag());
writer.close();
System.out.println("Output written to " + outFile);
} catch (IOException e) {
e.printStackTrace();
}
}
@Override
public String getSource() {
return xmlFile;
}
/**
* cvTerm used to identify modifications, that span several peptides
* This is the cvTerm for the Modification, that holds the mass of the cross-linker
* @return the crosslinkedDonorModAcc
*/
public String getCrosslinkedDonorModAcc() {
return crosslinkedDonorModAcc;
}
/**
* cvTerm used to identify modifications, that span several peptides
* This is the cvTerm for the Modification, that holds the mass of the cross-linker
* @param crosslinkedDonorModAcc the crosslinkedDonorModAcc to set
*/
public void setCrosslinkedDonorModAcc(String crosslinkedDonorModAcc) {
this.crosslinkedDonorModAcc = crosslinkedDonorModAcc;
}
/**
* cvTerm used to identify modifications, that span several peptides
* This is the cvTerm for the Modification, that holds a zero mass to denote the second (third, forth ...) site a cross-linker is attached to
* @return the crosslinkedReceptorModAcc
*/
public String getCrosslinkedReceptorModAcc() {
return crosslinkedReceptorModAcc;
}
/**
* cvTerm used to identify modifications, that span several peptides
* This is the cvTerm for the Modification, that holds a zero mass to denote the second (third, forth ...) site a cross-linker is attached to
* @param crosslinkedReceptorModAcc the crosslinkedReceptorModAcc to set
*/
public void setCrosslinkedReceptorModAcc(String crosslinkedReceptorModAcc) {
this.crosslinkedReceptorModAcc = crosslinkedReceptorModAcc;
}
/**
* cvTerm used to identify members of cross-linked PSMs
* @return the crosslinkedDonorSIIAcc
*/
public String getCrosslinkedSIIAcc() {
return crosslinkedSIIAcc;
}
/**
* cvTerm used to identify members of cross-linked PSMs
* @param crosslinkedDonorSIIAcc the crosslinkedDonorSIIAcc to set
*/
public void setCrosslinkedSIIAcc(String crosslinkedDonorSIIAcc) {
this.crosslinkedSIIAcc = crosslinkedDonorSIIAcc;
}
/**
* Identifies a mzIdentML-file, that represents a cross-link search
* @return the crosslinkedSearchAcc
*/
public String getCrosslinkedSearchAcc() {
return crosslinkedSearchAcc;
}
/**
* Identifies a mzIdentML-file, that represents a cross-link search
* @param crosslinkedSearchAcc the crosslinkedSearchAcc to set
*/
public void setCrosslinkedSearchAcc(String crosslinkedSearchAcc) {
this.crosslinkedSearchAcc = crosslinkedSearchAcc;
}
}