-
Notifications
You must be signed in to change notification settings - Fork 7
/
sprot_get_analyze.py
114 lines (83 loc) · 4.55 KB
/
sprot_get_analyze.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
"""'sprot_get_analyze.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.
"""
# updated for Python 3 -PW 7/6/2017
import os
import sys
import fasta_lib
# minimum sequence count cutoff for output table
min_sequence_count = 0
def main(db, folder, versions):
"""Fetches and analyzes the species names in Sprot database.
Arguments:
"folder" is full path name to UniProt DBs. "versions" is
a dictionary of version numbers for file naming. No return values.
Saves a summary text file that can be loaded into EXCEL.
"""
print('====================================================================')
print(' sprot_get_analyze.py, v1.1.0, written by Phil Wilmarth, OHSU, 2017 ')
print('====================================================================')
# create a log file to mirror screen output
log_obj = open(os.path.join(folder, 'fasta_utilities.log'), 'a')
write = [None, log_obj]
fasta_lib.time_stamp_logfile('\n>>> starting: sprot_get_analyze.py', log_obj)
# make sure the files are present or download if not
fasta_lib.download_uniprot(db, folder, versions)
# make dictionaries of species names (or accession IDs) to taxon IDs
(sci_to_taxon, id_to_taxon) = fasta_lib.make_uniprot_to_taxon(folder)
# get more complete list of names to taxon number from ncbi data
name_to_taxon = fasta_lib.make_all_names_to_taxon(folder)
# make species frequency dictionary
fname = 'uniprot_sprot_%s.fasta.gz' % (versions['sprot'],)
(name_freq, name_to_id, prot_count) = fasta_lib.uniprot_species_frequency(os.path.join(folder, fname))
# sort the species names and write to file
fasta_lib.save_species_info(db, folder, name_freq, name_to_taxon, sci_to_taxon, id_to_taxon,
name_to_id, minimum=min_sequence_count)
# print out some stats and exit
new_db = 'uniprot_%s_%s.fasta.gz' % (db, versions[db],)
for obj in write:
print('...%s contained %s entries...' % (new_db, prot_count), file=obj)
print('...there were', len(name_freq), 'species names...', file=obj)
fasta_lib.time_stamp_logfile('>>> ending: sprot_get_analyze.py', log_obj)
log_obj.close()
return
if __name__ == '__main__':
# get path to uniprot databases and call main function to download, etc.
# check if folder path is passed on command line
versions = fasta_lib.get_uniprot_version()
if len(sys.argv) > 1 and os.path.exists(sys.argv[1]):
container = sys.argv[1]
else:
# browse to container folder for uniprot downloads, subfolder will be created with name "uniprot_version"
database = r'C:\Xcalibur\database' # default for BioWorks, XP
if not os.path.exists(database):
database = os.getcwd()
container = fasta_lib.get_folder(database, 'Select folder for uniprot downloads')
if container == '': sys.exit() # cancel button repsonse
folder = os.path.split(container)[1].lower() # ignore case
if folder.startswith('uniprot_'): # if subfolder, up one level
container = os.path.split(container)[0]
folder = os.path.join(container, 'uniprot_' + versions['uniprot'])
if not os.path.exists(folder): # make folder if necessary
os.mkdir(folder)
main('sprot', folder, versions)
# end