-
Notifications
You must be signed in to change notification settings - Fork 7
/
fasta_lib.py
1501 lines (1333 loc) · 58.2 KB
/
fasta_lib.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
"""'fasta_lib.py' Written by Phil Wilmarth, OHSU.
The MIT License (MIT)
Copyright (c) 2017 Phillip A. Wilmarth and OHSU
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.
Direct questions to:
Technology & Research Collaborations, Oregon Health & Science University,
Ph: 503-494-8200, FAX: 503-494-4729, Email: techmgmt@ohsu.edu.
"""
# 6/5/2009 PW - minor bug fixes for processing nr databases
# - changed how reversed accessions are written for nr databases
# 7/8/2009 PW - revised fasta reader method to check amino acid characters
# - added error checking flags to FasterReader & Protein methods
# 4/23/2010 PW - fixed findPeptide wrt I/L being indistinguishable
# - improved adding decoy_string to accessions in reverseProtein
# - added more support for Ref_Seq entries in NCBI databases
# 4/12/2011 PW - added a tryptic digest function
# 2011 PW - Switched to arrays to hold Gi to Taxon data to reduce memory use
# 2016 PW - rewritten for Python 3
# 2017 PW - removed any support for IPI databases and for cleaning accessions
#
import os
import sys
import gzip
import tarfile
import urllib.request
import socket
import sqlite3
import tkinter
from tkinter import filedialog
import fasta_lib
def get_folder(default_location, title_string=None):
"""Dialog box to browse to a folder. Returns folder path.
Usage: full_folder_name = get_folder(default_location, [title]),
where "default_location" is a starting folder location,
"title" is an optional message to list in the dialog box,
and "full_folder_name" is the complete selected folder name.
Written by Phil Wilmarth, 2008, 2016
"""
# set up GUI elements
root = tkinter.Tk()
root.withdraw()
try:
root.tk.call('console', 'hide')
except:
pass
# set default title string and location if not passed
if title_string is None:
title_string = 'Select a folder with desired files/dirs'
if not default_location:
default_location = os.getcwd()
# create dialog box for folder selection
root.update() # helps make sure dialog box goes away after selection
full_folder_name = filedialog.askdirectory(parent=root, initialdir=default_location,
title=title_string, mustexist=True)
# return full folder name
return full_folder_name
def get_file(default_location, ext_list, title_string=None):
"""Dialog box to browse to a file. Returns full file name.
Usage: full_file_name = get_file(default_location, ext_list, [title]),
where "default_location" is a starting folder location,
ext_list is a list of (label, pattern) tuples,
e.g. ext_list = [('Text files', '*.txt')],
"title" is an optional message to list in the dialog box, and
"full_file_name" is the complete name of the selected file.
Written by Phil Wilmarth, OHSU, 2008, 2016.
"""
# set up GUI elements
root = tkinter.Tk()
root.withdraw()
try:
root.tk.call('console', 'hide')
except:
pass
# set default title string and ext list if not passed
if title_string is None:
title_string = 'Select a single FILE'
if not ext_list:
ext_list = [('All files', '*.*')]
if not default_location:
default_location = os.getcwd()
# create dialog box for file selection
root.update() # helps make sure dialog box goes away after selection
filename = filedialog.askopenfilename(parent=root, initialdir=default_location,
filetypes=ext_list, title=title_string)
# return full filename
return filename
def save_file(default_location, ext_list, default_file='', title_string=None):
"""Dialog box to save a file. Returns full name of desired file.
Usage: full_file_name = save_file(def_loc, ext_list, [def_file], [title]),
where "def_loc" is a starting folder location,
ext_list is a list of (label, pattern) tuples,
e.g. ext_list = [('Text files', '*.txt')],
"def_file" is an optional default filename,
"title" is an optional message to list in dialog box, and
"full_file_name" is the complete name of the desired file.
Written by Phil Wilmarth, OHSU, 2009, 2016.
"""
# set up GUI elements
root = tkinter.Tk()
root.withdraw()
try:
root.tk.call('console', 'hide')
except:
pass
# set default title string if not passed
if title_string is None:
title_string = 'Select a single FILE'
if not ext_list:
ext_list = [('All files', '*.*')]
if not default_location:
default_location = os.getcwd()
# create dialog box for file selection
root.update() # helps make sure dialog box goes away after selection
filename = filedialog.asksaveasfilename(parent=root, initialdir=default_location,
initialfile=default_file, filetypes=ext_list,
title=title_string)
# return full filename
return filename
def get_files(default_location, ext_list, title_string=None):
"""Dialog box to browse for files. Returns a tuple of file names.
Usage: file_name_list = get_files(default_location, ext_list, [title]),
where "default_location" is a starting folder location,
ext_list is a list of (label, pattern) tuples,
e.g. ext_list = [('Text files', '*.txt')],
"title" is an optional message to list in the dialog box, and
"file_name_list" is a tuple of file name(s).
Written by Phil Wilmarth, OHSU, 2010, 2016.
"""
# set up GUI elements
root = tkinter.Tk()
root.withdraw()
# set default title string if not passed
if title_string is None:
title_string = 'Select one or more FILE(s)'
if not ext_list:
ext_list = [('All files', '*.*')]
if not default_location:
default_location = os.getcwd()
# create dialog box for file selection
root.update() # helps make sure dialog box goes away after selection
filenames = filedialog.askopenfilenames(parent=root, initialdir=default_location,
filetypes=ext_list, multiple=True,
title=title_string)
return filenames
def get_string(title, prompt='Enter a string', initial=''):
"""Function to wrapper tkSimpleDialog.askstring function
Written by Phil Wilmarth, OHSU, 2010.
"""
from tkinter.simpledialog import askstring
return askstring(title, prompt, initialvalue=initial)
def taxon_cmd_line_checker(argv):
"""Checks command line arguments for correctness.
Returns dictionary of taxon, name pairs or empty dictionary.
Written by Phil Wilmarth, OHSU, 2009.
"""
tax_dict = {}
if argv[0].endswith('.py'):
argv = argv[1:]
# need to have an even number of (integer, name) arguments
try:
pairs = [(argv[i], argv[i+1]) for i in range(0, len(argv), 2)]
for (taxon, name) in pairs:
for char in taxon:
if not char.isdigit():
break
tax_dict[abs(int(taxon))] = name
# print usage information if error in format
except:
print('\n ### invalid command line argument format ###\n')
print(' arguments must be a series of "taxonomy number" "name" pairs')
print(' where "taxonomy number" is integer and "name" is text string.')
print(' example: prompt>python program.py 9606 human 4932 yeast\n')
# return dictionary, it will be empty if any errors encountered
return tax_dict
def string_cmd_line_checker(argv):
"""Checks command line arguments for correctness.
Returns dictionary of string, name pairs or empty dictionary.
Written by Phil Wilmarth, OHSU, 2009.
"""
str_dict = {}
if argv[0].endswith('.py'):
argv = argv[1:]
# need to have an even number of (string, name) arguments
try:
pairs = [(argv[i], argv[i+1]) for i in range(0, len(argv), 2)]
for (string, name) in pairs:
str_dict[string] = name
# print usage information if error in format
except:
print('\n ### invalid command line argument format ###\n')
print(' arguments must be a series of "string" "name" pairs')
print(' where "string" is a text pattern and "name" is text string.')
print(' Note: enclose "string" in double quotes if it has whitespace.')
print(' example: prompt>python program.py "Homo sapiens" human\n')
# return dictionary, it will be empty if any errors encountered
return str_dict
class Peptide:
"""Data structure for some basic peptide information
"""
def __init__(self, sequence='', begin=0, end=0, mass=0, missed=0):
self.seq = sequence
self.beg = begin
self.end = end
self.mass = mass
self.missed = missed
return
class Protein:
"""Object to hold protein accession numbers, descriptions, and sequences.
Methods:
__init_:standard constructor, no parameters.
readProtein: returns next protein from "fasta_reader"
printProtein: prints sequence in FASTA format
parseNCBI: cleans up nr entries
parseUniProt: cleans up Sprot/Trembl entries
parseCONT: cleans up Contaminant entries
reverseProtein: reverses sequences and modifies accession/descriptions
molwtProtein: computes average MW of sequence
frequencyProtein: returns aa composition dictionary
seqlenProtein: returns aa sequence length
findPeptide: finds location of peptide in protein sequence
coverage: calculates coverage and aa counts from peptide list
Written by Phil Wilmarth, OHSU, 2009.
"""
def __init__(self):
"""Basic constructor, no parameters.
"""
# bare bones __init__ function
self.accession = 'blank'
self.new_acc = 'blank'
self.description = 'blank'
self.new_desc = 'blank'
self.sequence = ''
self.sequence_padded = None
self.sequence_masked = None
self.pad_count = None
self.length = 0
self.peptides = []
return
def readProtein(self, fasta_reader):
"""Gets the next FASTA protein entry from FastaReader object.
Usage: Boolean = object.readProtein(fasta_reader),
where "object" is an instance of a Protein object and
"fasta_reader" is an instance of a FastaReader object.
Return value is "False" when EOF encountered.
Written by Phil Wilmarth, OHSU, 2009.
"""
status = fasta_reader.readNextProtein(self)
self.new_acc = self.accession
self.new_desc = self.description
return status
def printProtein(self, file_obj=None, length=80):
"""Prints FASTA protein entry to file (stdout is default).
Usage: object.printProtein([file_obj=None, length=80]),
where "object" is an instance of a Protein object, and
"file_obj" is a file object (a value of None will print
to standard out stream. Optional "length" is number of
characters per line for the protein sequence.
Written by Phil Wilmarth, OHSU, 2009.
"""
if file_obj == None:
file_obj = sys.stdout
# print new accession and new descriptor on first line
if self.new_desc == '':
print('>'+self.new_acc, file=file_obj)
else:
print('>'+self.new_acc, self.new_desc, file=file_obj)
# initialize some things
char_count = 0
char_line = ''
# build up sequence line with "length" characters per line
for char in self.sequence:
if char_count < length: # do not have "width" chars yet
char_line += char
char_count += 1
else: # line is "width" long so print and reset
print(char_line, file=file_obj)
char_line = char
char_count = 1
# print last sequence line (often less than "width" long) and return
if len(char_line):
print(char_line, file=file_obj)
return
def parseNCBI(self, REF_SEQ_ONLY=False):
"""Parses NCBI nr database accession numbers and descriptions
Written by Phil Wilmarth, OHSU, 2009.
"""
#===================================================
# Might need to check this with new NCBI formats
# maybe a regex would be more elegant if doable
#===================================================
# keep the gi number (or RefSeq) for the accession, get rid of others
temp = self.accession.split('|')
if REF_SEQ_ONLY:
try:
iacc = temp.index('ref')
except ValueError:
iacc = temp.index('gi')
else:
iacc = temp.index('gi')
self.new_acc = '|'.join(temp[iacc:iacc+2])
# keep the first description string if more than one
new_desc = self.description.split(chr(1))[0]
new_desc = new_desc.rstrip()
# add a period to the end of description
if not new_desc.endswith('.'):
new_desc = new_desc + '.'
self.new_desc = new_desc
return
def parseUniProt(self, KEEP_UNIPROT_ID=False):
"""Parses UniProt database accession numbers and descriptions.
Written by Phil Wilmarth, OHSU, 2009.
"""
if len(self.accession.split('|')) < 3:
print(' parseUniProt WARNING: fewer than 3 accession elements')
# if KEEP_UNIPROT_ID is set to True, keep the more human readible part of the accession numbers
if KEEP_UNIPROT_ID:
self.new_acc = self.accession.split('|')[-1]
acc_num = self.accession.split('|')[-2]
else:
self.new_acc = self.accession.split('|')[-2]
acc_num = self.accession.split('|')[-1]
# keep the desciption text and get rid of the trailing stuff
new_desc = self.description.split(chr(1))[0]
new_desc = new_desc.split('OS=')[0]
new_desc = new_desc.rstrip()
# add other accession text to end of description
if new_desc.endswith('.'):
new_desc = new_desc[:-1]
self.new_desc = '%s (%s).' % (new_desc, acc_num)
return
def parseCONT(self):
"""Parses contaminant accession numbers and descriptions
Written by Phil Wilmarth, OHSU, 2009.
"""
# keep the contaminant tag for the accession, get rid of others
old_acc = ''
temp = self.accession.split('|')
if len(temp) > 1:
self.new_acc = temp[0]
old_acc = '|'.join(temp[1:])
# keep the first description string if more than one
new_desc = self.description.split(chr(1))[0]
new_desc = new_desc.strip()
# add other accessions to description (if any)
if new_desc.endswith('.'):
new_desc = new_desc[:-1]
if old_acc:
self.new_desc = '%s (%s).' % (new_desc, old_acc)
else:
self.new_desc = new_desc + '.'
return
def reverseProtein(self, decoy_string):
"""Reverses protein sequence and returns new Protein object.
Usage: rev_prot = object.reverseProtein(decoy_string),
where "object" is a Protein object, "decoy_string" is the
unique identifier text to add to the beginning of the
protein accesion number, and "rev_prot" is new Protein object.
Written by Phil Wilmarth, OHSU, 2009.
"""
# make sure decoy_string ends with an undescore
if not decoy_string.endswith('_'):
decoy_string = decoy_string + '_'
# create a new Protein instance
rev_prot = Protein()
# prefix the decoy_sting to all important parts of accession
temp = self.accession.split('|')
if self.accession.startswith('gi|'): # modify nr DB accessions
temp = [temp[i] for i in range(1, len(temp), 2)]
temp = temp[0:1] # keep the gi number
elif self.accession.startswith('sp|') or \
self.accession.startswith('tr|'): # modifiy Sprot or Trembl accs
temp = temp[1:]
temp = temp[0:1] # keep the accession number
elif self.accession.startswith('CONT_'):
temp = temp[0:1] # keep the contaminant number
else:
pass
#
if len(temp) > 1: # make sure temp is a list not a string
rev_prot.accession = '|'.join([decoy_string+x for x in temp])
else:
rev_prot.accession = decoy_string + temp[0]
rev_prot.new_acc = rev_prot.accession
# change the desciptions, too.
rev_prot.description = 'REVERSED.'
rev_prot.new_desc = 'REVERSED.'
# now reverse the sequence
rev_prot.sequence = self.sequence[::-1]
return rev_prot
def molwtProtein(self, show_errs=True):
"""Returns protein molecular weight as the sum of average aa masses.
If "show_errs" flag set, invalid amino acid characters are reported.
Written by Phil Wilmarth, OHSU, 2009.
"""
# start with water and H+ masses, then add aa masses
bad_char = {}
self.setMasses()
molwt = 18.01 + 1.007825
for i in self.sequence:
try:
molwt += self.ave_masses[i]
except: # keep track of bad characters
bad_char[i] = True
#
bad_char = sorted(list(bad_char.keys()))
if len(bad_char) > 0 and show_errs: # report bad chars if desired
print(' WARNING: unknown symbol(s) (%s) in %s:\n%s' %
(''.join(bad_char), self.accession, self.sequence))
return molwt
def frequencyProtein(self, show_errs=True):
"""Returns aa frequency distrubution as a dictionary.
If "show_errs" flag set, invalid amino acid characters are reported.
Written by Phil Wilmarth, OHSU, 2009.
"""
freq = {'X':0, 'G':0, 'A':0, 'S':0,
'P':0, 'V':0, 'T':0, 'C':0,
'L':0, 'I':0, 'J':0, 'N':0,
'O':0, 'B':0, 'D':0, 'Q':0,
'K':0, 'Z':0, 'E':0, 'M':0,
'H':0, 'F':0, 'R':0, 'Y':0,
'W':0, 'U':0, '*':0, '-':0}
# count the amino acids for all residues in sequence
bad_char = {}
for i in self.sequence:
try:
freq[i] += 1
except: # keep track of bad characters
bad_char[i] = True
#
bad_char = list(bad_char.keys())
bad_char.sort()
if len(bad_char) > 0 and show_errs: # report any bad chars, if desired
print(' WARNING: unknown symbol(s) (%s) in %s:\n%s' %
(''.join(bad_char), self.accession, self.sequence))
return freq
def seqlenProtein(self):
"""Calculates protein sequence length.
Written by Phil Wilmarth, OHSU, 2009.
"""
return (len(self.sequence))
def findPeptide(self, peptide, pad_count=1):
"""Calculates location of all 'peptide' matches in 'self.sequence.'
Written by Phil Wilmarth, OHSU, 2009, 2016.
"""
import re
matches = []
# get rid of bounding residues, if any
try:
peptide = peptide.split('.')[1]
except IndexError:
pass
# remove any modification symbols and mask I/L:
# '*', '#', '@', '^', '~', '$', '%', '!', '+', 'n', 'c', '[', ']' (Current Comet along with old style nt, ct)
splitter = re.compile(r'[*#@^~$%!+nc\[\]]')
base_pep = ''.join(splitter.split(peptide))
base_pep_masked = re.sub(r'[IL]', 'j', base_pep)
# fix the protein sequence for peptide lookups (pad and mask I/L). Save the results to improve performance
if (not self.sequence_masked) or (pad_count != self.pad_count):
self.sequence_padded = ('-' * pad_count) + self.sequence + ('-' * pad_count) # add bounding symbols
self.sequence_masked = re.sub(r'[IL]', 'j', self.sequence_padded)
self.pad_count = pad_count
# find all matches of base_pep_masked to protein sequence (padded and masked)
for match in re.finditer(base_pep_masked, self.sequence_masked):
start, end = match.span()
start_prot, end_prot = start - self.pad_count + 1, end - self.pad_count
# add bounding AAs, periods, and put back modification special chars
pre = self.sequence_padded[start-self.pad_count:start]
post = self.sequence_padded[end:end+self.pad_count]
full_seq = pre + '.' + peptide + '.' + post
matches.append((start_prot, end_prot, full_seq))
# return the match list (empty list if no matches)
return matches
def calcCoverage(self, peptide_list):
"""Calculates % coverage and aa frequency map of matched peptides.
"peptide_list" is list of sequences with optional counts (as tuples).
Written by Phil Wilmarth, OHSU, 2009.
"""
freq_dict = {}
try: # see if peptide_list is a list of tuples or not
for peptide, count in peptide_list:
for (beg, end, seq) in self.findPeptide(peptide):
for key in [str(i) for i in range(beg, end+1)]:
if freq_dict.get(key, False):
freq_dict[key] = freq_dict[key] + count
else:
freq_dict[key] = count
except ValueError:
for peptide in peptide_list:
for (beg, end, seq) in self.findPeptide(peptide):
for key in [str(i) for i in range(beg, end+1)]:
if freq_dict.get(key, False):
freq_dict[key] = freq_dict[key] + 1
else:
freq_dict[key] = 1
#
coverage = 100.0*float(len(freq_dict))/float(len(self.sequence))
coverage_map = []
for i, aa in enumerate(self.sequence):
coverage_map.append((str(i+1), aa, freq_dict.get(str(i+1), 0)))
return (coverage, coverage_map)
def enzymaticDigest(self, enzyme_regex=None, low=500.0, high=5000.0, length=7, missed=2, mass='mono'):
"""Performs a tryptic digest of a protein sequence. This does not
do any modifications to residues except for reduction/alkylation of
cys residues (C+57). Mass filters should be relaxed.
Returns a list of digested peptides.
enzyme_regex is a compiled re object for the enzyme cleavage
(if enzyme_regex not defined, do tryptic digest by default)
low, high - mass limits for peptides.
length - minimum amino acid length
missed - maximum number of missed cleavages.
mass - 'ave' average or 'mono' monoisotopic masses.
written by Phil Wilmarth, OHSU, 2011, 2016.
"""
"""Regular expression digestion table:
trypsin from: http://stackoverflow.com/questions/18364380/python-3-cut-peptide-regular-expression
regex = re.compile(r".") # no enzyme
regex = re.compile(r".(?:(?<![KR](?!P)).)*") # trypsin strict
regex = re.compile(r".(?:(?<![KR]).)*") # trypsin with cleavage at P
regex = re.compile(r".(?:(?<![K](?!P)).)*") # Lys-C strict
regex = re.compile(r".(?:(?<![K]).)*") # Lys-C with cleavage at P
regex = re.compile(r".(?:(?![K]).)*") # Lys-N
regex = re.compile(r".(?:(?<![R](?!P)).)*") # Arg-C strict
regex = re.compile(r".(?:(?![D]).)*") # Asp-N
regex = re.compile(r".(?:(?<![M]).)*") # CnBr
regex = re.compile(r".(?:(?<![DE](?!P)).)*") # Glu-C
regex = re.compile(r".(?:(?<![FL](?!P)).)*") # PepsinA
regex = re.compile(r".(?:(?<![FWYL](?!P)).)*") # chymotrypsin
"""
import copy
import re
# skip if there is no sequence to digest
if len(self.sequence) == 0:
return []
# tryptic digestion is the default
if not enzyme_regex:
enzyme_regex = re.compile(r".(?:(?<![KR](?!P)).)*")
# set up masses, default is alkylated cysteine. No mechanism for other modifications yet.
self.setMasses()
if mass == 'ave':
masses = copy.deepcopy(self.ave_masses)
masses['C'] = 160.197
elif mass == 'mono':
masses = copy.deepcopy(self.mono_masses)
masses['C'] = 160.03065
else:
print('...WARNING: masses must be "ave" or "mono"')
# digest the sequence
digest_matches = [x for x in enzyme_regex.finditer(self.sequence)] # list of re match objects
# get info from match objects into Peptide object attributes
digest = [Peptide(mass=masses['water']) for x in digest_matches]
for i, match in enumerate(digest_matches):
digest[i].seq = match.group()
digest[i].beg, digest[i].end = match.span()
digest[i].beg += 1
for aa in match.group():
try:
digest[i].mass += masses[aa]
except KeyError:
print('...WARNING: unrecognized amino acid character!')
print('...bad character:', aa)
print('...in protein:', self.accession, self.description)
# test peptides and missed cleavage peptides for mass ranges and min length
valid_digest = []
for i in range(len(digest)):
# check if peptide is within the mass range and meets min length
if (low <= digest[i].mass <= high) and (len(digest[i].seq) >= length):
valid_digest.append(digest[i])
# create and check missed cleavages
for j in range(1, missed+1):
if (i+j) > len(digest)-1:
continue
temp = Peptide(begin=100000) # a peptide object for missed cleavages
# calculate running sums for each number of missed cleavages
for k in range(j+1):
if (i+k) > len(digest)-1:
continue
temp.seq += digest[i+k].seq
temp.beg = min(temp.beg, digest[i+k].beg)
temp.end = max(temp.end, digest[i+k].end)
temp.mass += (digest[i+k].mass - masses['water'])
temp.mass += masses['water']
temp.missed = k
# check missed cleavage peptide for valid mass range and length
if (low <= temp.mass <= high) and (len(temp.seq) >= length):
valid_digest.append(temp)
# return the list of digested peptides
self.peptides = valid_digest
return valid_digest
def setMasses(self):
"""Set average and monoisotopic mass dictionaries.
"""
self.ave_masses = {'X': 0.0000, 'G': 57.0513, 'A': 71.0779, 'S': 87.0773, 'P': 97.1152,
'V': 99.1311, 'T':101.1039, 'C':103.1429, 'L':113.1576, 'I':113.1576,
'J':113.1576, 'N':114.1026, 'O':114.1472, 'B':114.5950, 'D':115.0874,
'Q':128.1292, 'K':128.1723, 'Z':128.6216, 'E':129.1140, 'M':131.1961,
'H':137.1393, 'F':147.1739, 'R':156.1857, 'Y':163.1733, 'W':186.2099,
'U':150.0379, '*': 0.00000, '-': 0.00000, 'water':18.02}
self.mono_masses = {'X': 0.000000, 'G': 57.021464, 'A': 71.037114, 'S': 87.032028, 'P':97.052764,
'V': 99.068414, 'T':101.047679, 'C':103.009185, 'L':113.084064, 'I':113.084064,
'J':113.084064, 'N':114.042927, 'O':114.147200, 'B':114.595000, 'D':115.026943,
'Q':128.058578, 'K':128.094963, 'Z':128.621600, 'E':129.042593, 'M':131.040485,
'H':137.058912, 'F':147.068414, 'R':156.101111, 'Y':163.063320, 'W':186.079313,
'U':150.953630, '*': 0.000000, '-': 0.000000, 'water':18.01057}
return
# end class
class FastaReader:
"""Reads FASTA entries from a file-like object.
methods:
__init__: basic constructor, no parameters.
readProtein: reads one FASTA entry from a file object (text or zipped)
arguments are "next_protein" and "file_obj"
returns True (next protein) or False (EOF or not FASTA).
written by Phil Wilmarth, OHSU, 2009.
"""
def __init__(self, fasta_file):
"""Basic constructor function. No parameters
self._last_line retains the previous '>' line and
self._valid is a dictionary of valid protein FASTA chars.
"""
# attribute to save last line from previous read
self._last_line = 'start value'
self._file_obj = None
self._fasta_file = fasta_file
# list of valid amino acid characters
self._valid = {'X':True, 'G':True, 'A':True, 'S':True, 'P':True,
'V':True, 'T':True, 'C':True, 'L':True, 'I':True,
'J':True, 'N':True, 'O':True, 'B':True, 'D':True,
'Q':True, 'K':True, 'Z':True, 'E':True, 'M':True,
'H':True, 'F':True, 'R':True, 'Y':True, 'W':True,
'U':True, '*':True, '-':True }
## if not os.path.exists(fasta_file):
## ext_list = [('FASTA files', '*.fasta'),
## ('Zipped FASTA files', '*.gz'),
## ('All files', '*.*')]
## fasta_file = get_file(os.getcwd(), ext_list, title_string="Select FASTA file")
# get file object and save as attribute
try:
if fasta_file.endswith('.gz'):
self._file_obj = gzip.open(fasta_file, 'rt')
else :
self._file_obj = open(fasta_file, 'rt')
except IOError:
print(' WARNING:', fasta_file, 'could not be opened!')
raise
return
def readNextProtein(self, next_protein, check_for_errs=False):
"""Loads one FASTA protein text entry into a Protein object.
Returns True (protein entry found) or False (end of file).
If "check_for_errs" flag is set, amino acid chars are checked.
Written by Phil Wilmarth, OHSU, 2009.
"""
# at first call, start reading lines
if self._last_line == 'start value':
self._last_line = self._file_obj.readline()
if not self._last_line:
self._file_obj.close()
return(False)
self._last_line = self._last_line.strip()
# get next protein's info from _last_line
if self._last_line.startswith('>'):
next_protein.accession = self._last_line.split()[0][1:]
next_protein.new_acc = next_protein.accession
start = len(next_protein.accession)+2
next_protein.description = self._last_line[start:]
next_protein.new_desc = next_protein.description
# return if empty line (EOF) or non-description line
else:
self._file_obj.close()
return(False)
# reset variables and read in next entry
next_protein.sequence = ""
line = self._last_line
self._last_line = ""
bad_char = {}
while line:
line = self._file_obj.readline()
if not line:
break
else:
testline = line.strip()
if testline == '':
continue
# stop reading at next descriptor line (and save line)
if line.startswith('>'):
self._last_line = line.strip()
# report bad characters if conditions were met
bad_char = sorted(bad_char.keys())
if len(bad_char) > 0 and check_for_errs:
print(' WARNING: unknown symbol(s) (%s) in %s' %
(''.join(bad_char), next_protein.accession))
break
# add next sequence line to protein's sequence
else:
line = line.rstrip()
line = line.upper()
if check_for_errs: # checking chars slows down the program
for char in line:
if self._valid.get(char, False):
next_protein.sequence += char
else:
bad_char[char] = True
else: # blindly adding the line is faster...
next_protein.sequence += line
# return (protein info retained in next_protein)
return True
# end class
def get_uniprot_version():
"""Gets UniProt version numbers from online release notes.
Written by Phil Wilmarth, OHSU, 2009.
"""
# set up to read the online UniProt release notes file
print('...getting database version numbers...')
versions = {'uniprot':'XX.X', 'sprot':'XX.X', 'trembl':'XX.X'}
address = 'ftp://ftp.expasy.org/databases/uniprot/current_release/knowledgebase/complete/reldate.txt'
reldate = urllib.request.urlopen(address)
charset = 'utf-8' # should try an get this dynamically (I was getting None with header calls)
# read release notes file and get UniProt, etc. version numbers
for line in reldate:
line = line.decode(charset)
if 'UniProt Knowledgebase' in line:
versions['uniprot'] = line.split()[3].replace('_', '.')
elif 'Swiss-Prot' in line:
versions['sprot'] = line.split()[2].replace('_', '.')
elif 'TrEMBL' in line:
versions['trembl'] = line.split()[2].replace('_', '.')
return(versions)
def download_uniprot(db, folder, versions):
"""Downloads (if necessary) UniProt FASTA, taxon files to "folder".
Written by Phil Wilmarth, OHSU, 2009.
"""
# check if files are already downloaded, if not fetch them from uniprot site
socket.setdefaulttimeout(120.)
print('...downloading databases and taxonomy files...')
db_name = 'uniprot_%s_%s.fasta.gz' % (db, versions[db],)
base_address = 'ftp://ftp.expasy.org/databases/uniprot/current_release/knowledgebase/complete/'
db_address = 'uniprot_%s.fasta.gz' % (db,)
db_address = base_address + db_address
files_addresses = [ (os.path.join(folder, db_name), db_address),
(os.path.join(folder, 'taxdump.tar.gz'),
'ftp://ftp.ncbi.nih.gov/pub/taxonomy/taxdump.tar.gz'),
(os.path.join(folder, 'speclist.txt'),
'ftp://ftp.ebi.ac.uk/pub/databases/uniprot/current_release/knowledgebase/complete/docs/speclist.txt')]
files_addresses.reverse()
for (file_name, address) in files_addresses:
if os.path.exists(file_name):
pass
else:
print('...downloading', file_name)
x = reporter()
try:
urllib.request.urlretrieve(address, file_name, reporthook=x.report)
except:
print('...WARNING: download may have hung at EOF or other error')
finally:
urllib.request.urlcleanup()
return
def download_ncbi(nr_folder):
"""Downloads (if necessary) the ncbi FASTA and taxon files to "nr_folder".
Written by Phil Wilmarth, OHSU, 2009.
"""
# check if files are already downloaded, if not fetch them from ncbi site
nr_name = os.path.split(nr_folder)[1] + '.gz'
if os.path.exists(os.path.join(nr_folder, 'nr.gz')):
os.rename(os.path.join(nr_folder, 'nr.gz'), \
os.path.join(nr_folder, nr_name))
files_addresses = [ (os.path.join(nr_folder, nr_name),
'ftp://ftp.ncbi.nih.gov/blast/db/FASTA/nr.gz'),
(os.path.join(nr_folder, 'prot.accession2taxid.gz'),
'ftp://ftp.ncbi.nih.gov/pub/taxonomy/accession2taxid/prot.accession2taxid.gz'),
(os.path.join(nr_folder, 'taxdump.tar.gz'),
'ftp://ftp.ncbi.nih.gov/pub/taxonomy/taxdump.tar.gz') ]
files_addresses.reverse()
socket.setdefaulttimeout(240.)
for (file_name, address) in files_addresses:
if os.path.exists(file_name):
pass
else:
print('...downloading', file_name)
x = reporter()
try:
urllib.request.urlretrieve(address, file_name, reporthook=x.report)
except:
print('...WARNING: download may have hung at EOF or other error')
finally:
urllib.request.urlcleanup()
return
class reporter():
"""Prints download progress to console.
"""
def __init__(self):
self.packets = 0
self.size = 0
self.buff = 8192
def report(self, packets, buff, size):
# this monitors the download progress
if packets % 4096 == 0:
sub_total = packets * buff
print('......%s of %s bytes (%.2f%%)' %
("{0:,d}".format(sub_total), "{0:,d}".format(size), float(100*sub_total)/size))
return
# end class
def expand_species(folder, db, taxon_dict, min_sequence_count, min_seq_per_species,
REF_SEQ_ONLY=False):
"""Expands any taxon nodes numbers into all member taxon numbers.
Written by Phil Wilmarth, OHSU, 2009.
"""
VERBOSE = False
# open taxonomy nodes file
print('...making taxonomy nodes dictionary...')
archive_name = os.path.join(folder, 'taxdump.tar.gz')
archive = tarfile.open(archive_name)
nodes = archive.extractfile('nodes.dmp')
# read nodes file and save child to parent taxon mappings
child_to_parent = {}
while True:
line = nodes.readline()
line = line.decode('utf-8')
line = line.rstrip()
if not line:
break
else:
line = line.rstrip()
item = line.split('\t|\t')
child_to_parent[int(item[0])] = int(item[1])
nodes.close()
# open the fasta_analysis.txt file
species_counts = {}
if db == 'nr':
analysis_file = os.path.join(folder, 'nr_fasta_analyze.txt')
if REF_SEQ_ONLY:
index = 4
else:
index = 3
elif db == 'sprot':
analysis_file = os.path.join(folder, 'sprot_fasta_analyze.txt')
index = 4
elif db == 'trembl':
analysis_file = os.path.join(folder, 'trembl_fasta_analyze.txt')
index = 4
else:
analysis_file = os.path.join(folder, 'uniprot_fasta_analyze.txt')
index = 6
fasta_analyze = open(analysis_file, 'r')
# save species taxons and sequence counts
line = fasta_analyze.readline().rstrip() # skip header line
while True:
line = fasta_analyze.readline()
if not line:
break
else:
line = line.rstrip()
temp = line.split('\t')
taxon = temp[1]
count = temp[index]
try:
taxon = int(taxon)
count = int(count)
except:
continue
species_counts[taxon] = count
fasta_analyze.close()
# see if we have any group taxon numbers
group_expand = {}
for alltax in [x for x in species_counts.keys() if species_counts[x] >= min_seq_per_species]:
tree = []
tree.append(alltax)
parent = alltax