-
Notifications
You must be signed in to change notification settings - Fork 6
Expand file tree
/
Copy pathProtein.java
More file actions
669 lines (586 loc) · 18.5 KB
/
Copy pathProtein.java
File metadata and controls
669 lines (586 loc) · 18.5 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
/*
* 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.entities;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collection;
import java.util.HashSet;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import org.rappsilber.fdr.utils.FDRGroupNames;
import org.rappsilber.utils.DoubleArrayList;
import java.util.regex.*;
/**
* Represents a single protein.
* @author lfischer
*/
public class Protein extends AbstractFDRElement<Protein> {//implements Comparable<Protein> {
private long id;
private String searchId;
private String accession;
private String name;
private String description;
private String sequence="";
//private HashSet<PeptidePair> peps = new HashSet<PeptidePair>();
private HashSet<Peptide> peps = new HashSet<Peptide>();
private HashSet<PeptidePair> peppairs = new HashSet<PeptidePair>();
private double score = 0;
private DoubleArrayList scores = new DoubleArrayList(2);
private boolean scoresSorted = true;
private boolean isDecoy;
private boolean linearSupport = false;
private boolean internalSupport = false;
private boolean betweenSupport = false;
private boolean crosslinkerModSupport = false;
//private boolean isAmbigious = true;
private double m_fdr = -1;
private int size = -1;
private Protein decoy_complement = null;
public static String DECOY_PREFIX = null;
String zero = ""+(char)0;
private Pattern zerosplit = Pattern.compile(zero);
private Pattern spacesplit = Pattern.compile("^\\s*(\"[^\"]*\"|'[^']*'|\\([^)]*\\)|[^\\s]+)");
private String fdrgroup = null;
/**
* Used as the protein of origin for the NOPEPTIDE peptide
*/
public static Protein NOPROTEIN = new Protein(Integer.MAX_VALUE, "", "", false,true,true, true);
private String validated;
/**
* Constructor
* @param id
* @param accession
* @param description
* @param isDecoy
* @param linear
* @param internal
* @param between
*/
public Protein(long id, String accession, String name, String description, boolean isDecoy, boolean linear, boolean internal, boolean between, String decoyprefix) {
this(id, accession, description, isDecoy, linear, internal, between, decoyprefix);
this.name = name;
}
/**
* Constructor
* @param id
* @param accession
* @param description
* @param isDecoy
* @param linear
* @param internal
* @param between
*/
public Protein(long id, String accession, String name, String description, boolean isDecoy, boolean linear, boolean internal, boolean between) {
this(id, accession, name, description, isDecoy, linear, internal, between, DECOY_PREFIX);
}
/**
* Constructor
* @param id
* @param accession
* @param description
* @param isDecoy
* @param linear
* @param internal
* @param between
*/
public Protein(long id, String accession, String description, boolean isDecoy, boolean linear, boolean internal, boolean between) {
this(id, accession, description, isDecoy, linear, internal, between, DECOY_PREFIX);
}
/**
* Constructor
* @param id
* @param accession
* @param description
* @param isDecoy
* @param linear
* @param internal
* @param between
*/
public Protein(long id, String accession, String description, boolean isDecoy, boolean linear, boolean internal, boolean between, String decoyPrefix) {
this.id = id;
setAccession(accession, decoyPrefix);
this.description = description;
this.isDecoy = isDecoy;
this.linearSupport = linear;
this.internalSupport = internal;
this.betweenSupport = between;
if (description != null && description.length() > 0) {
if (description.indexOf(zero)>=0) {
this.name = zerosplit.split(description)[1];
this.description = zerosplit.split(description)[0];
} else {
// try parsing a name from description
Matcher m = spacesplit.matcher(description);
if (m.find()) {
this.name = m.group(1);
} else {
this.name = this.description;
}
}
}
}
@Override
public int hashCode() {
return accession.hashCode();
}
@Override
public boolean equals(Object p) {
return ((Protein) p).accession.contentEquals(accession) &&
isDecoy == ((Protein) p).isDecoy &&
(
(!isDecoy) || // unluckily we have different ways to do decoy at times - so we have to ignore the sequence here
sequence.contentEquals(((Protein) p).sequence) ||
sequence.isEmpty() ||
((Protein) p).sequence.isEmpty()
); // && ((Protein) p).sequence.contentEquals(sequence);
}
/**
* Is p the same protein as this.
* Decoy is ignored
* @param p
* @return
*/
public boolean equalsDecoysUnaware(Protein p) {
return (p.accession.toLowerCase().contentEquals(accession.toLowerCase()) || p.accession.toLowerCase().contentEquals("rev_" + accession.toLowerCase()) ||
accession.toLowerCase().contentEquals("rev_" + p.accession.toLowerCase())) && (p.sequence.length() == sequence.length() || p.sequence.isEmpty() || sequence.isEmpty()); // && ((Protein) p).sequence.contentEquals(sequence);
}
/**
* Add the information (supporting peptides) from the given protein instance
* to this instance
* @param o
*/
@Override
public void add(Protein o) {
if (o == this)
return;
this.score = Math.sqrt(this.score*this.score + o.score*o.score);
scores.add(o.score);
scoresSorted = false;
this.scoresSorted = false;
peps.addAll(o.peps);
linearSupport |= o.linearSupport;
internalSupport |= o.internalSupport;
this.betweenSupport |= o.betweenSupport;
if (this.description == null || this.description.length()==0)
this.description = o.description;
//setFDRGroup();
//isAmbigious &= o.isAmbigious;
// score+= o.score;
// if (o.hasPositiveGrouping()) {
// if (this.validated == null ) {
// this.validated = o.getPositiveGrouping();
// } else if (!this.validated.contentEquals(o.getPositiveGrouping())) {
// this.validated += " " + o.getPositiveGrouping();
// }
// }
}
/**
* Adds the peptide pair as support for this protein.
* @param pp
* @param score
*/
public void add(PeptidePair pp, double score) {
// double s = pp.score/(pp.peptide1Positions.size() * pp.peptide1Positions.size());
// score += s*s;
peppairs.add(pp);
this.score = Math.sqrt(this.score*this.score + score*score);
scoresSorted = false;
this.internalSupport |= pp.isInternal();
this.betweenSupport |= !(pp.isInternal() || pp.isLinear);
this.linearSupport |= pp.isLinear;
//setFDRGroup();
// if (pp.hasPositiveGrouping()) {
// if (this.validated == null) {
// this.validated = pp.getPositiveGrouping();
// } else if (!this.validated.contentEquals(pp.getPositiveGrouping())) {
// this.validated += " " + pp.getPositiveGrouping();
// }
// }
}
@Override
public double getScore() {
return score;
}
@Override
public double getScore(int topN) {
if (!scoresSorted) {
java.util.Collections.sort(scores);
scoresSorted = true;
}
int i = 0;
double score = 0;
double lastScore = Double.NaN;
for (double s : this.scores) {
if (lastScore != s) {
lastScore = s;
if (i++>topN)
break;
}
score+= s;
}
return score;
}
public int compare(Protein p) {
if (p.equals(this)) {
return 0;
}
if (this.id < p.id) {
return -1;
}
return 1;
}
public int compareDecoyUnAware(Protein p) {
if (p.equalsDecoysUnaware(this)) {
return 0;
}
if (this.id < p.id) {
return -1;
}
return 1;
}
/**
* @return the id
*/
public long getId() {
return id;
}
/**
* @return the search id
*/
public String getSearchId() {
return searchId;
}
/**
* @return the accession
*/
public String getAccession() {
return accession;
}
/**
* @return the description
*/
public String getDescription() {
return description;
}
/**
* @return the description
*/
public String getName() {
return this.name;
}
Pattern sptr_regex = Pattern.compile("^(?:sp|tr)\\|([^\\|]*)\\|([^ ]*)?.*");
/**
* @return the accession
*/
public void setAccession(String a, String decoy_prefix) {
accession = a;
Matcher m = sptr_regex.matcher(a);
if (m.matches()) {
accession = m.group(1);
//if (m.groupCount()>1 && m.group(2) != null && m.group(2).trim().length()>0)
// this.setName(m.group(2));
}
if (decoy_prefix == null) {
// at some points we consider decoy and non decoy proteins the same. So
// both get the same accession to make my life easier
if (accession.toUpperCase().startsWith("REV_") || accession.toUpperCase().startsWith("RAN_")) {
this.accession = accession.substring(4);
this.isDecoy = true;
} else if (accession.toUpperCase().startsWith("DECOY:")) {
this.isDecoy = true;
this.accession = accession.substring(6);
} else if (accession.toUpperCase().startsWith("REVERSE_")) {
this.isDecoy = true;
this.accession = accession.substring(8);
} else if (accession.toUpperCase().startsWith("RANDOM_")) {
this.isDecoy = true;
this.accession = accession.substring(7);
} else if (accession.toUpperCase().startsWith("SHUFFLE_")) {
this.isDecoy = true;
this.accession = accession.substring(8);
}
} else {
if (accession.toUpperCase().startsWith(decoy_prefix.toUpperCase())) {
this.isDecoy = true;
this.accession = accession.substring(decoy_prefix.length());
}
}
this.decoy_complement = null;
}
/**
* @return the accession
*/
public void setAccession(String a) {
setAccession(a,null);
}
/**
* @return the accession
*/
public void setName(String n) {
name = n;
this.decoy_complement = null;
}
/**
* @return the description
*/
public void setDescription(String d) {
description = d;
if (description != null && description.length() > 0) {
if (description.indexOf(zero)>=0) {
this.name = zerosplit.split(description)[1];
this.description = zerosplit.split(description)[0];
}
}
this.decoy_complement = null;
}
/**
* @return the peps
*/
public HashSet<PeptidePair> getPeptidePairs() {
return peppairs;
}
/**
* @return the isDecoy
*/
@Override
public boolean isDecoy() {
return isDecoy;
}
/**
* @return the linearSupport
*/
public boolean hasLinearSupport() {
return linearSupport;
}
/**
* @return the internalSupport
*/
public boolean hasInternalSupport() {
return internalSupport;
}
public int compareTo(Protein o) {
return Double.compare(o.getScore(), this.getScore());
}
private void setFDRGroup() {
fdrgroup = "";
if (hasLinearSupport()) {
fdrgroup = "Linear";
}
if (hasInternalSupport()) {
fdrgroup += "Internal";
}
if (fdrgroup.isEmpty()){
fdrgroup = "Between";
}
fdrgroup=FDRGroupNames.get(fdrgroup);
}
@Override
public String getFDRGroup() {
if (fdrgroup == null)
setFDRGroup();
return fdrgroup;
}
// @Override
// public String getFDRGroupName() {
// return getFDRGroupName(fdrgroup);
// }
//
// public static String getFDRGroupName(int group) {
// switch (group) {
// case -1 : return "all combined";
// case 0 : return "Only PPI";
// case 1 : return "Linear" ;
// case 2 : return "Within";
// case 3 : return "Linear + Within";
// case 4 : return "between only";
//
// default : return "?no support?";
// }
// }
@Override
public boolean isTT() {
return !isDecoy();
}
@Override
public boolean isTD() {
return isDecoy();
}
@Override
public boolean isDD() {
return false;
}
public Collection<Protein> getProteins() {
ArrayList<Protein> ret = new ArrayList<Protein>();
ret.add(this);
return ret;
}
public Collection<Peptide> getPeptides() {
HashSet<Peptide> ret = new HashSet<Peptide>();
for (PeptidePair pp : peppairs) {
ret.addAll(pp.getPeptides());
}
return ret;
}
@Override
public String toString() {
if (isDecoy)
return "D - " + accession;
return "T - " + accession;
}
public Protein decoyComplement() {
if (this.decoy_complement == null) {
this.decoy_complement = new Protein(id, accession, name, description, !isDecoy, linearSupport, internalSupport, betweenSupport);
}
return this.decoy_complement;
}
public int support() {
return peppairs.size();
}
@Override
public void setFDR(double fdr) {
m_fdr = fdr;
}
@Override
public double getFDR() {
return m_fdr;
}
public void resetFDR() {
score = 0;
peps = new HashSet<Peptide>();
peppairs = new HashSet<PeptidePair>();
m_fdr = Double.MAX_VALUE;
setLinkedSupport(1);
}
@Override
public int getPeptidePairCount() {
return peppairs.size();
}
/**
* @return the betweenSupport
*/
public boolean hasBetweenSupport() {
return betweenSupport;
}
/**
* @param betweenSupport the betweenSupport to set
*/
public void setBetweenSupport(boolean betweenSupport) {
this.betweenSupport = betweenSupport;
}
@Override
public Object getSite(int n) {
return this;
}
@Override
public int getSites() {
return 1;
}
/**
* @return the sequence
*/
public String getSequence() {
return sequence;
}
/**
* @param sequence the sequence to set
*/
public void setSequence(String sequence) {
this.sequence = sequence;
}
@Override
public boolean isLinear() {
return true;
}
@Override
public boolean isInternal() {
return false;
}
@Override
public boolean isBetween() {
return false;
}
@Override
public Site getLinkSite1() {
return new ProteinSite(this);
}
@Override
public Site getLinkSite2() {
return null;
}
@Override
public ProteinGroup getProteinGroup1() {
ArrayList<Protein> pg = new ArrayList<>(1);
pg.add(this);
return new ProteinGroup(pg, peppairs);
}
@Override
public ProteinGroup getProteinGroup2() {
return ProteinGroup.NOPROTEINGROUP;
}
@Override
public void setFDRGroup(String fdrGroup) {
this.fdrgroup = FDRGroupNames.get(fdrGroup);
}
//
// @Override
// public boolean hasPositiveGrouping() {
// throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
// }
//
// @Override
// public void setPositiveGrouping(String av) {
// throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
// }
//
// @Override
// public HashSet<String> getPositiveGrouping() {
// throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
// }
//
// @Override
// public boolean hasNegativeGrouping() {
// throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
// }
//
// @Override
// public void setNegativeGrouping(String v) {
// throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
// }
//
// @Override
// public HashSet<String> getNegativeGrouping() {
// throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
// }
//
/**
* @return the size
*/
public int getSize() {
if (size < 0) {
this.size = this.sequence.replaceAll("[^A-Z]", "").length();
}
return size;
}
/**
* @param size the size to set
*/
public void setSize(int size) {
this.size = size;
}
public boolean isNonCovalent() {
return false;
}
}