-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsipros_post_module.py
1135 lines (959 loc) · 38.1 KB
/
sipros_post_module.py
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
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#!/usr/bin/python
"""
sipros_post_module.py
sipros_post_module.py module includes common classes, definitions,
and funcitons for sipros post-processing programs.
Created by Tae-Hyuk (Ted) Ahn on 10/10/2012.
Copyright (c) 2012 Tae-Hyuk Ahn (ORNL). Allrights reserved.
Modified by Xuan Guo on 07/13/2016
Updated by Shengze Wang on 07/29/2022.
"""
# # Import standard modules
import sys, os, re, math
from datetime import datetime
from collections import namedtuple
from multiprocessing import Process
try:
from sets import Set
except ImportError:
pass
SIP_WDP_score_idx = 0
# # Class Usage
class Usage(Exception):
def __init__(self, msg):
self.msg = msg
# # Class for ignoring comments '#' in sipros file
class CommentedFile:
def __init__(self, f, comment_string="#"):
self.f = f
self.comment_string = comment_string
def __next__(self):
line = self.f.__next__()
while line.startswith(self.comment_string):
line = self.f.__next__()
return line
def __iter__(self):
return self
# # Exit system with error message
def die(msg=None):
if msg is not None:
print >> sys.stderr, msg
sys.exit(1)
# # Returns the current time in a nice format
def curr_time():
curr_time = datetime.now()
return curr_time.strftime("%c")
# # Format time as a pretty string
def format_time(td):
hours = td.seconds // 3600
minutes = (td.seconds % 3600) // 60
seconds = td.seconds % 60
return '%02d:%02d:%02d' % (hours, minutes, seconds)
# # Find string between two substrings
def find_between(s, first, last):
try:
start = s.index(first) + len(first)
end = s.index(last, start)
return s[start:end]
except ValueError:
return ""
# # Division error handling
def divide(x, y):
try:
result = x / y
except ZeroDivisionError as detail:
print >> sys.stderr, 'Handling run-time error:', detail
die('Program exit!')
else:
return result
# # Check file exist
def check_file_exist(filename):
try:
with open(filename) as _f:
pass
except IOError as _e:
print >> sys.stderr, '\nCannot open', filename
die("Program exit!")
# # Get file(s) list in working dir with specific file extension
def get_file_list_with_ext(working_dir, file_ext):
# define sipros file extension
file_list = []
# working directory
if os.path.exists(working_dir):
for file_name in os.listdir(working_dir):
# check the file extension
if file_name.endswith(file_ext):
file_path_name = os.path.join(working_dir, file_name)
# file_path_name = working_dir + file_name
file_list.append(file_path_name)
if len(file_list) == 0:
print >> sys.stderr, "\nCannot open %s file(s)." % (file_ext)
die("Program exit!")
file_list = sorted(file_list)
else:
print >> sys.stderr, "\nCannot open working directory", working_dir
die("Program exit!")
return file_list
# # Get base output filename with input file list and base_out_default
def get_base_out(file_list, base_out_default, working_dir):
# Get base output with common prefix
base_out = os.path.commonprefix(file_list)
base_out_filename = base_out.split('/')[-1]
# If base common prefix ends with '.pep.txt', then remove '.pep.txt'
base_out = base_out.replace(".pep.txt", "_")
# If base common prefix ends with '.tab', then remove '.tab'
base_out = base_out.replace(".tab", "_")
# If base_out file name is less than 5, then use default baseout
if len(base_out_filename) < 5:
base_out = os.path.join(working_dir, base_out_default)
# If base common prefix ends with '_' or '.', then remove
base_out = base_out[:-1] if (base_out[-1] in ('_', '.')) else base_out
return base_out
# # list_to_string
# # if single element, then just convert to string
# # if multiple elements, then bracket {A,B}
def list_to_string(input_list):
if len(input_list) > 1:
converted_str = '{' + ','.join(input_list) + '}'
else:
converted_str = ''.join(input_list)
return converted_str
# # list_to_bracket
# # bracket the list
def list_to_bracket(input_list):
converted_str = '{' + ','.join(input_list) + '}'
return converted_str
# # Class for sipros fields object
class SiprosFields(namedtuple('SiprosFields',
['Filename',
'ScanNumber',
'ParentCharge',
'MeasuredParentMass',
'CalculatedParentMass',
'ScanType',
'SearchName',
'ScoringFunction',
'Rank',
'Score',
'IdentifiedPeptide',
'OriginalPeptide',
'ProteinNames'])):
def __init__(self):
self.data = self
# # Class for spectrum fields object
class SpectrumFields(namedtuple('SpectrumFields',
['x',
'Filename',
'ScanNumber',
'ParentCharge',
'MeasuredParentMass',
'ScanType',
'SearchName',
'TotalIntensity',
'MaxIntensity'])):
def __init__(self):
self.data = self
# # Class for psm fields object
class PsmFields(namedtuple('PsmFields',
['x',
'OriginalPeptide',
'IdentifiedPeptide',
'CalculatedParentMass',
'MVH',
'Xcorr',
'WDP',
'ProteinNames'])):
def __init__(self):
self.data = self
# # Class for sipros4 fields object
class Sipros4Fields(namedtuple('SiprosFields',
['Filename',
'ScanNumber',
'ParentCharge',
'MeasuredParentMass',
'CalculatedParentMass',
'ScanType',
'SearchName',
'Rank',
'MVH',
'Xcorr',
'WeightDotSum',
'IdentifiedPeptide',
'OriginalPeptide',
'ProteinNames',
'AveAtom',
'StdAtom'])):
def __init__(self):
self.data = self
# # Class for PsmOutFields object
class PsmOutFields(namedtuple('PsmOutFields',
['Filename',
'ScanNumber',
'ParentCharge',
'MeasuredParentMass',
'CalculatedParentMass',
'MassErrorDa', # CalculatedParentMass - MeasuredParentMass
'MassErrorPPM', # MassErrorDa / CalculatedParentMass
'ScanType',
'SearchName',
'ScoringFunction',
'Score',
'DeltaZ', # The difference score between the rank 1 and 2
'DeltaP', # The difference score between isoform
'IdentifiedPeptide',
'OriginalPeptide',
'ProteinNames',
'ProteinCount',
'TargetMatch'])):
def __init__(self):
self.data = self
# # Class for PsmOutFields object (for sipro4)
class Psm4OutFields(namedtuple('PsmOutFields',
['Filename',
'ScanNumber',
'ParentCharge',
'MeasuredParentMass',
'CalculatedParentMass',
'MassErrorDa', # CalculatedParentMass - MeasuredParentMass
'MassErrorPPM', # MassErrorDa / CalculatedParentMass
'ScanType',
'SearchName',
'ScoringFunction',
'Score',
'DeltaZ', # The difference score between the rank 1 and 2
'DeltaP', # The difference score between isoform
'IdentifiedPeptide',
'OriginalPeptide',
'ProteinNames',
'ProteinCount',
'TargetMatch',
'AveAtom',
'StdAtom'])):
def __init__(self):
self.data = self
# # Class for PepSubFields object
class PepSubFields(namedtuple('PepSubFields',
['IdentifiedPeptide',
'ParentCharge',
'OriginalPeptide',
'ProteinNames',
'Score',
'Filename',
'ScanNumber',
'ScanType',
'SearchName'])):
def __init__(self):
self.data = self
# # Class for PepSubFields object
class PepDataFields(namedtuple('PepDataFields',
['IdentifiedPeptide',
'ParentCharge',
'BestScore',
'ProteinNames'])):
def __init__(self):
self.data = self
# # Class for PepOutFields object
class PepOutFields(namedtuple('PepOutFields',
['IdentifiedPeptide', # 0
'ParentCharge', # 1
'OriginalPeptide', # 2
'ProteinNames', # 3
'ProteinCount', # 4
'TargetMatch', # 5
'SpectralCount', # 6 number of PSMs matched to this peptide
'BestScore', # 7 the highest score of those PSMs
'PSMs',
# 8 a list of PSMs matched to this peptide. Use {Filename[ScanNumber],Filename[ScanNumber]} format
'ScanType', # 9 ScanType
'SearchName'])): # 10 SearchName
def __init__(self):
self.data = self
# # Class for pretty float
class PrettyFloat(float):
def __repr__(self):
return "%0.5f" % self
# # A range function, that does accept float increments
def frange(start, end=None, inc=None):
if end == None:
end = start + 0.0
start = 0.0
if inc == None:
inc = 1.0
L = []
while 1:
fnext = start + len(L) * inc
if inc > 0 and fnext >= end:
break
elif inc < 0 and fnext <= end:
break
L.append(fnext)
return L
# # check sub list
def check_sub_list(list_A, list_B):
check_status = True
for list_A_item in list_A:
if list_A_item not in list_B:
check_status = False
else:
continue
return check_status
# # get item list from parenthesis string as {AA,BB}
def get_item_list(input_string):
input_string = input_string[1:-1]
item_list = re.split(r"\s*[,]\s*", input_string.strip())
item_list_new = []
for item_one in item_list:
if item_one not in item_list_new:
item_list_new.append(item_one)
return item_list_new
# # get_protein_count
def get_protein_count(protein_names):
input_string = protein_names[1:-1]
item_list = re.split(r"\s*[,]\s*", input_string.strip())
protein_count = len(item_list)
return protein_count
# # set float digit
def set_float_digit(input_val):
if input_val is float:
output_val = str("{0:.5f}".format(round(input_val, 5)))
else:
output_val = str(input_val)
return output_val
# # peptide delete residues
def peptide_delete_residues(peptide_string):
try:
left_braket_index = peptide_string.index('[')
right_braket_index = peptide_string.index(']')
if len(peptide_string) > right_braket_index + 1:
if peptide_string[right_braket_index + 1].isalpha():
peptide_output = peptide_string[left_braket_index:right_braket_index + 1]
else:
peptide_output = peptide_string[left_braket_index:right_braket_index + 2]
else:
peptide_output = peptide_string[left_braket_index:right_braket_index + 1]
return peptide_output
except AttributeError:
print >> sys.stderr, '\nCannot parse peptide correctly.\n'
die("Program exit!")
# # merge protein names
def merge_protein_names(first_protein_names, second_protein_names):
first_protein_list = get_item_list(first_protein_names)
second_protein_list = get_item_list(second_protein_names)
merge_protein_list = list(set(first_protein_list + second_protein_list))
merge_protein_names = list_to_bracket(merge_protein_list)
return merge_protein_names
# # Task wrapper
class PsmPack:
def __init__(self, _iSize=1000, _iStartScanNumber=0):
self.iSize = _iSize
self.lPsm = []
for _i in range(_iSize):
self.lPsm.append([])
self.iStartScanNumber = _iStartScanNumber
self.bEmpty = True
self.current = 0
def add(self, lOnePsm, iScanNumber):
self.lPsm[iScanNumber - self.iStartScanNumber].append(lOnePsm)
self.bEmpty = False
def empty(self):
return self.bEmpty
def __iter__(self):
self.current = 0
return self
def next(self):
if self.current >= self.iSize:
raise StopIteration
else:
while not self.lPsm[self.current]:
self.current += 1
if self.current >= self.iSize:
raise StopIteration
self.current += 1
return self.lPsm[self.current - 1]
fNeutronMass = 1.00867108694132 # it is Neutron mass
# # the mass difference, inverted, larger better.
def MassDiff(oPepScores):
fDiff = oPepScores.fCalculatedParentMass - oPepScores.fMeasuredParentMass
fTemp = fDiff
fCeil = 0
fDown = 0
if fDiff >= 0:
fDiff = fTemp
fCeil = math.ceil(fTemp) * fNeutronMass
fFloor = math.floor(fTemp) * fNeutronMass
if fFloor > fTemp:
fFloor -= fNeutronMass
if fCeil - fNeutronMass > fTemp:
fCeil -= fNeutronMass
if fTemp > fCeil - fTemp:
fTemp = fCeil - fTemp
if fDiff > fDiff - fFloor:
fDiff = abs(fDiff - fFloor)
if abs(fTemp) < abs(fDiff):
fDiff = fTemp
else:
fCeil = math.ceil(fDiff) * fNeutronMass
if fCeil < fDiff:
fCeil += fNeutronMass
fFloor = math.floor(fDiff) * fNeutronMass
if fFloor + fNeutronMass < fDiff:
fFloor += fNeutronMass
fDiff = fTemp
if abs(fTemp) > fCeil - fTemp:
fTemp = fCeil - fTemp
if abs(fDiff) > fDiff - fFloor:
fDiff = fDiff - fFloor
fTemp = abs(fTemp)
fDiff = abs(fDiff)
if fTemp < fDiff:
fDiff = fTemp
return -fDiff
# # the PTM score, the count of PTM, larger better
def PtmScore(oPepScores):
s1 = ''.join([char if char.isalnum() else '$' for char in oPepScores.sIdentifiedPeptide])
return -(s1.count('$') - 2)
# # Rank Product
def RankProductInvert(liRank):
fProduct = 1.0
for i in liRank:
fProduct *= i
return 1 / fProduct
# # Pep info in the Spe2Pep file
# # non SIP mode
class PepScores:
def __init__(self, _fMeasuredParentMass, _iCharge, _sSearchName, sPeptideLine, isSIP=False):
self.fMeasuredParentMass = _fMeasuredParentMass
self.iCharge = _iCharge
asWords = sPeptideLine.split('\t')
self.sIdentifiedPeptide = peptide_delete_residues(asWords[1])
self.sOriginalPeptide = peptide_delete_residues(asWords[2])
# self.sIdentifiedPeptide = peptide_delete_residues(asWords[2])
# self.sOriginalPeptide = peptide_delete_residues(asWords[1])
self.fCalculatedParentMass = float(asWords[3])
self.lfScores = []
self.liRanks = []
self.lfScoreDiff = [] # difference between the current one with the second best one
for e in asWords[4:-1]:
self.lfScores.append(float(e))
# remove the {}
self.sProteinNames = (asWords[-1])[1:-1]
self.fRankProduct = 0.0
self.iRank = 0
self.sSearchName = _sSearchName
self.fPct = 0
if isSIP:
self.fPct = float(_sSearchName[_sSearchName.find('_') + 1:_sSearchName.find("Pct")])
# delta -> comet way
# diff -> difference between current one to the next best one
# diffnor -> difference between current one to the next best one normalized by the current one
self.lfDeltaRankProduct = []
self.lfDeltaRankScore = []
self.lfDiffRankProduct = []
self.lfDiffRankScore = []
self.lfDiffNorRankProduct = []
self.lfDiffNorRankScore = []
self.DeltaP = 'NA'
if len(self.sOriginalPeptide) != len(self.sIdentifiedPeptide):
self.DeltaP = 1
def numberTopRanks(liRanks):
iCount = 0
for i in liRanks:
if i == 1:
iCount += 1
return iCount
# # count pep ranked as top by how many scores
def get_number_Top_Ranks(pep, all_top_peps):
iCount = 0
for i in range(len(pep.lfScores)):
if pep.lfScores[i] >= all_top_peps[i].lfScores[i]:
pep.liRanks[i] = 1
iCount += 1
return iCount
def zero_divide(a, b):
if b != 0:
return a / b
else:
return 0
# # PSM info in the Spe2Pep file
class PepSpectrumMatch:
iPurgeTrigger = 100
iRankSave = 20
def __init__(self, sSpectrumLine, SIP=False):
asWords = sSpectrumLine.split('\t')
self.sFileName = asWords[1]
self.iScanNumber = int(asWords[2])
self.iParentCharge = int(asWords[3])
self.fMeasuredParentMass = float(asWords[4])
self.sScanType = asWords[5]
self.sSearchName = asWords[6]
# self.fTotalIntensity = float(asWords[7])
# self.fMaxIntensity = float(asWords[8])
self.sRTime = asWords[7]
self.lPepScores = []
self.oBestPep = None
self.oSecondBestPep = None
self.oRestPep = None
self.lTopPep = []
self.pep_rank_list = []
self.SIPmode = SIP
def addPepScores(self, pep):
for e in self.lPepScores:
if e.sIdentifiedPeptide == pep.sIdentifiedPeptide:
if e.lfScores[0] < pep.lfScores[0]:
e.lfScores = pep.lfScores
e.fCalculatedParentMass = pep.fCalculatedParentMass
e.sSearchName = pep.sSearchName
e.fPct = pep.fPct
elif e.lfScores[0] == pep.lfScores[0] and self.SIPmode:
if e.fPct > pep.fPct:
e.fCalculatedParentMass = pep.fCalculatedParentMass
e.sSearchName = pep.sSearchName
e.fPct = pep.fPct
words = pep.sProteinNames.split(',')
for sProtein in words:
if e.sProteinNames.find(sProtein) == -1:
e.sProteinNames += ','
e.sProteinNames += sProtein
return
self.lPepScores.append(pep)
if len(self.lPepScores) > self.iPurgeTrigger:
if self.SIPmode:
self.purgeSIP()
else:
self.purge()
def purgeSIP(self):
# sort pep accoring to SIP_WDP_score
lPep = sorted(self.lPepScores, key=lambda pep: (-pep.lfScores[SIP_WDP_score_idx], pep.fPct))
pep_new_list = []
# keep all the pep with the highest score
pep_new_list.extend(lPep[0:self.iPurgeTrigger / 2])
def purge(self):
iNumScores = len(self.lPepScores[0].lfScores)
for j in self.lPepScores:
del j.liRanks[:]
for i in range(iNumScores):
lPep = sorted(self.lPepScores, key=lambda pep: (
-pep.lfScores[i], -MassDiff(pep), -PtmScore(pep), pep.sIdentifiedPeptide, pep.fPct))
iRank = 1
for j in lPep:
j.liRanks.append(iRank)
iRank += 1
liRanksNew = []
for j in self.lPepScores:
if any(i <= self.iRankSave for i in j.liRanks):
liRanksNew.append(j)
self.lPepScores = liRanksNew
def ranking_sip(self):
# only kep the top score psm, or the least mass difference psm, or lowest ptm score psm, or the psm with the smallest percent
lPep = sorted(self.lPepScores, \
key=lambda pep: (-pep.lfScores[SIP_WDP_score_idx], \
-MassDiff(pep), -PtmScore(pep), \
pep.fPct, pep.sIdentifiedPeptide))
self.pep_rank_list.append(lPep)
iRank = 1
for j in lPep:
j.liRanks.append(iRank)
iRank += 1
# score rank -> score differential
for j in range(0, len(lPep) - 1):
lPep[j].lfDiffRankScore.append(lPep[j].lfScores[SIP_WDP_score_idx] - \
lPep[j + 1].lfScores[SIP_WDP_score_idx])
lPep[j].lfDiffRankScore.append(0) # for the format sake, SIP does not use the other two score
lPep[j].lfDiffRankScore.append(0) # for the format sake, SIP does not use the other two score
lPep[len(lPep) - 1].lfDiffRankScore.append(0)
lPep[len(lPep) - 1].lfDiffRankScore.append(0)
lPep[len(lPep) - 1].lfDiffRankScore.append(0)
del self.lTopPep[:]
self.lTopPep.append(lPep[0])
def ranking(self):
del self.lTopPep[:]
for j in self.lPepScores:
del j.liRanks[:]
del j.lfScoreDiff[:]
iNumScores = len(self.lPepScores[0].lfScores)
for i in range(iNumScores):
lPep = sorted(self.lPepScores, key=lambda pep: (
-pep.lfScores[i], -MassDiff(pep), -PtmScore(pep), pep.sIdentifiedPeptide, pep.fPct))
self.pep_rank_list.append(lPep)
iRank = 1
for j in lPep:
j.liRanks.append(iRank)
iRank += 1
# score rank -> score differential
for j in range(0, len(lPep) - 1):
lPep[j].lfDiffRankScore.append(lPep[j].lfScores[i] - lPep[j + 1].lfScores[i])
lPep[len(lPep) - 1].lfDiffRankScore.append(0)
self.lTopPep.append(lPep[0])
# if SA == 1, calculate DeltaP individually
for s in range(iNumScores):
for lPep_local in self.pep_rank_list:
# contain PTM
if len(lPep_local[0].sIdentifiedPeptide) != len(lPep_local[0].sOriginalPeptide):
# pep_sorted_str = ''.join(sorted(lPep_local[0].sIdentifiedPeptide))
for pep in lPep_local:
# pep_sorted_compared_str = ''.join(sorted(pep.sIdentifiedPeptide))
# if pep_sorted_str == pep_sorted_compared_str and \
# pep.sIdentifiedPeptide != lPep_local[0].sIdentifiedPeptide :
if pep.sIdentifiedPeptide != lPep_local[0].sIdentifiedPeptide and \
abs(pep.fCalculatedParentMass - lPep_local[0].fCalculatedParentMass) < 0.00005 and \
abs(math.fabs(pep.fMeasuredParentMass - pep.fCalculatedParentMass) - math.fabs(
lPep_local[0].fMeasuredParentMass - lPep[0].fCalculatedParentMass)) < 0.00005 and \
pep.sOriginalPeptide == lPep_local[0].sOriginalPeptide and \
len(pep.sIdentifiedPeptide) == len(lPep_local[0].sIdentifiedPeptide):
avg_deltaP = 0.0
for si in range(iNumScores):
# if self.lTopPep[si].lfScores[si] != 0 and lPep_local[0].liRanks[si] == 1:
if self.lTopPep[si].lfScores[si] != 0:
avg_deltaP += (lPep_local[0].lfScores[si] - pep.lfScores[si]) / \
self.lTopPep[si].lfScores[si]
avg_deltaP /= float(iNumScores)
lPep_local[0].DeltaP = avg_deltaP
break
def all_top_ranked_psm(self):
str_list = []
for pep in self.lTopPep:
feature_list = []
feature_list.append(self.sFileName)
feature_list.append(str(self.iScanNumber))
feature_list.append(str(pep.iCharge))
feature_list.append(str(self.fMeasuredParentMass))
feature_list.append(self.sScanType)
feature_list.append(pep.sSearchName)
feature_list.append(pep.sIdentifiedPeptide)
feature_list.append(pep.sOriginalPeptide)
feature_list.append(str(pep.fCalculatedParentMass))
feature_list.extend((str(x) for x in pep.lfScores))
feature_list.append('{' + pep.sProteinNames + '}')
# feature_list.append(str(num_agreement(pep.liRanks)))
# feature_list.extend((str(x) for x in pep.lfDeltaRankProduct))
# feature_list.extend((str(x) for x in pep.lfDeltaRankScore))
# feature_list.extend((str(x) for x in pep.lfDiffRankProduct))
feature_list.extend((str(x) for x in pep.lfDiffRankScore))
# feature_list.extend((str(x) for x in pep.lfDiffNorRankProduct))
# feature_list.extend((str(x) for x in pep.lfDiffNorRankScore))
feature_list.append(self.sRTime)
# feature_list.append((str(pep.iRank)))
feature_list.append((str(pep.DeltaP)))
str_list.append('\t'.join(feature_list))
return '\n'.join(str_list)
def all_top_5_ranked_psm(self, top_n=5):
str_list = []
pep_set = Set()
for l in self.pep_rank_list:
n = len(l)
if n > top_n:
n = top_n
for i in range(n):
pep_set.add(l[i])
for pep in pep_set:
feature_list = []
feature_list.append(self.sFileName)
feature_list.append(str(self.iScanNumber))
feature_list.append(str(self.iParentCharge))
feature_list.append(str(self.fMeasuredParentMass))
feature_list.append(self.sScanType)
feature_list.append(pep.sSearchName)
feature_list.append(pep.sIdentifiedPeptide)
feature_list.append(pep.sOriginalPeptide)
feature_list.append(str(pep.fCalculatedParentMass))
feature_list.extend((str(x) for x in pep.lfScores))
feature_list.append('{' + pep.sProteinNames + '}')
# feature_list.append(str(num_agreement(pep.liRanks)))
# feature_list.extend((str(x) for x in pep.lfDeltaRankProduct))
# feature_list.extend((str(x) for x in pep.lfDeltaRankScore))
# feature_list.extend((str(x) for x in pep.lfDiffRankProduct))
feature_list.extend((str(x) for x in pep.lfDiffRankScore))
# feature_list.extend((str(x) for x in pep.lfDiffNorRankProduct))
# feature_list.extend((str(x) for x in pep.lfDiffNorRankScore))
feature_list.append(self.sRTime)
# feature_list.append((str(pep.iRank)))
feature_list.append((str(pep.DeltaP)))
str_list.append('\t'.join(feature_list))
return '\n'.join(str_list)
def removeReverse(self, lProteins):
newlist = []
for sProtein in lProteins:
if not sProtein.startswith('Rev_'):
newlist.append(sProtein)
return newlist
# # lOnePsm:
# # + spectrum
# # * peptide
# # * peptide
def SelectTopRankedPsm(lOnePsm, isSIP=False):
psm_dict = {}
psm = PepSpectrumMatch(lOnePsm[0][0], isSIP)
psm_dict[psm.iParentCharge] = psm
'''
if psm.iScanNumber == 4299:
print 'check'
'''
iCharge = 0
sSearchName = ''
for PsmInOneFile in lOnePsm:
for sline in PsmInOneFile:
if sline[0] == '+':
# a spectrum line
iCharge = get_charge(sline)
sSearchName = get_search_name(sline)
if iCharge not in psm_dict:
psm = PepSpectrumMatch(sline, isSIP)
psm_dict[iCharge] = psm
else:
# a peptide line
pep = PepScores(psm_dict[iCharge].fMeasuredParentMass, iCharge, sSearchName, sline, isSIP)
psm_dict[iCharge].addPepScores(pep)
# sorting and then ranking
psm_list = []
for k, v in psm_dict.items():
if isSIP:
v.ranking_sip()
if v.lTopPep[0].lfScores[SIP_WDP_score_idx] <= 0:
continue
else:
v.ranking()
psm_list.append(v)
return psm_list
# # peak a line from the file
def peek_line(f):
pos = f.tell()
sline = f.readline()
f.seek(pos)
return sline
# # get the scan number from a line
def get_scan_number(sLine, sDelimiter='\t', iFirstDelimiter=2):
iPos = -1
while iFirstDelimiter > 0:
iPos = sLine.find(sDelimiter, iPos + 1)
iFirstDelimiter -= 1
iBegin = iPos + 1
iPos = sLine.find(sDelimiter, iBegin)
iScanNumber = int(sLine[iBegin:iPos])
return iScanNumber
# # get the charge from a line
def get_charge(sLine, sDelimiter='\t', iFirstDelimiter=3):
iPos = -1
while iFirstDelimiter > 0:
iPos = sLine.find(sDelimiter, iPos + 1)
iFirstDelimiter -= 1
iBegin = iPos + 1
iPos = sLine.find(sDelimiter, iBegin)
iCharge = int(sLine[iBegin:iPos])
return iCharge
# # get the search name from a line
def get_search_name(sLine, sDelimiter='\t', iFirstDelimiter=6):
iPos = -1
while iFirstDelimiter > 0:
iPos = sLine.find(sDelimiter, iPos + 1)
iFirstDelimiter -= 1
iBegin = iPos + 1
iPos = sLine.find(sDelimiter, iBegin)
sSearchName = sLine[iBegin:iPos]
return sSearchName
# # get the PSM with scan number less than the upper scan number bound
def get_psm(f, lPsm, sSpectrum='+', sPeptide='*', iUpperScanNumber=0):
bEof = True
lOnePsm = []
while True:
pos = f.tell()
sline = f.readline().strip()
# # end of file
if not sline:
break
iScanNumber = 0
if sline[0] == sSpectrum:
iScanNumber = get_scan_number(sline)
if iScanNumber < iUpperScanNumber:
bEof = False
lOnePsm = []
lOnePsm.append(sline)
lPsm.add(lOnePsm, iScanNumber)
else:
# roll back the previous position
f.seek(pos)
bEof = False
break
else:
lOnePsm.append(sline)
return bEof
# # skip the comment area and the header
def skip_comment(f, sComment='#', iLineHeader=0):
pos = f.tell()
sline = f.readline()
while (sline[0] == sComment):
pos = f.tell()
sline = f.readline()
f.seek(pos)
for _i in range(iLineHeader):
f.readline()
# print f.readline()
# # Spe2Pep reader
class Spe2PepReader(Process):
def __init__(self, queue=None, name=None, searchname=None, inputFolder=None):
super(Spe2PepReader, self).__init__()
self.name = name
self.qPsmUnprocessed = queue;
self.iNumScanProcessed = 0
self.sSearchName = searchname
self.FileList = []
self.iScanInterval = 1000
self.sInputFolder = inputFolder
# # list files with 'Spe2Pep.txt' extensions
# # put the search results for the same FT2 file into a list
def categorizeSpe2PepFile(self, sWorkingDirectory):
lFileList = get_file_list_with_ext(sWorkingDirectory, 'Spe2Pep.txt')
sFt2Name = ''
lFt2Name = []
iIndexFt2 = 0
for sFileName in lFileList:
iPos = sFileName.rfind(self.sSearchName)
if iPos != -1:
# a '.' is before the search name, so iPos-1
sFt2Name = sFileName[0:iPos - 1]
if sFt2Name in lFt2Name:
iIndexFt2 = lFt2Name.index(sFt2Name)
self.FileList[iIndexFt2].append(sFileName)
else:
lFt2Name.append(sFt2Name)
lNewFt2 = []
lNewFt2.append(sFileName)
self.FileList.append(lNewFt2)
def readData(self):
for _id, lFiles in enumerate(self.FileList):
lFileReader = []
for sFiles in lFiles:
oFile = open(sFiles, 'r')
skip_comment(oFile, iLineHeader=2)
lFileReader.append(oFile)
# # peek the first scan number
iSmallestScanNumer = sys.maxint
for f in lFileReader:
sLine = peek_line(f)
iScanNumber = get_scan_number(sLine)
if iScanNumber < iSmallestScanNumer:
iSmallestScanNumer = iScanNumber
# #
iLastScanExcluded = iSmallestScanNumer
bReachEof = False
while (not bReachEof):
psmPack = PsmPack(_iSize=self.iScanInterval, _iStartScanNumber=iLastScanExcluded)
iLastScanExcluded = iLastScanExcluded + self.iScanInterval
bReachEof = True
for f in lFileReader:
bReachEof = bReachEof & (get_psm(f, psmPack, iUpperScanNumber=iLastScanExcluded))
if not psmPack.empty():