-
Notifications
You must be signed in to change notification settings - Fork 7
/
count_fasta.py
118 lines (96 loc) · 4.59 KB
/
count_fasta.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
"""'count_fasta.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 compatibility -PW 7/4/2017
import os
import sys
import time
import fasta_lib
def fasta_counter(fasta_file):
"""Counts entries in a FASTA protein database.
Call with FASTA filename.
Checks for duplicate accessions and (optional) valid characters.
"""
# create a log file to mirror screen output
_folder = os.path.split(fasta_file)[0]
log_obj = open(os.path.join(_folder, 'fasta_utilities.log'), 'a')
write = [None, log_obj]
fasta_lib.time_stamp_logfile('\n>>> starting: count_fasta.py', log_obj)
# create instances of reader object and protein object, initialize counters
f = fasta_lib.FastaReader(fasta_file)
p = fasta_lib.Protein()
prot = 0
head = 0
conflict = {}
# read proteins until EOF; NOTE: checking for errors slows program by factor of 3-4
while f.readNextProtein(p, check_for_errs=False):
# count protein sequences
prot += 1
if (prot % 500000) == 0:
print('......(%s proteins read...)' % ("{0:,d}".format(prot),))
## # check for duplicate accession
## dup = conflict.get(p.accession, False)
## if dup:
## for obj in write:
## print('\n...WARNING: %s is already in FASTA database!\n' % (p.accession,), file=obj)
## if p.molwtProtein(show_errs=False) == conflict[p.accession]:
## print('......possible duplicated sequence...', file=obj)
## else:
## conflict[p.accession] = p.molwtProtein(show_errs=False)
# count number of header elements
control_A = p.description.count(chr(1))
head = head + control_A + 1
# print results and return
for obj in write:
print('...there are %s proteins in %s' % ("{0:,d}".format(prot), os.path.split(fasta_file)[1]), file=obj)
if head > prot:
print('...there were %s header lines' % ("{0:,d}".format(head),), file=obj)
fasta_lib.time_stamp_logfile('>>> ending: count_fasta.py', log_obj)
log_obj.close()
return
# setup stuff: check for command line args, etc.
if __name__ == '__main__':
# check if database name(s) passed on command line
if len(sys.argv) > 1:
fasta_files = sys.argv[1:]
# if not, browse to database file
else:
database = r'C:\Xcalibur\database' # set a default to speed up browsing
if not os.path.exists(database):
database = os.getcwd()
fasta_files = fasta_lib.get_files(database,
[('FASTA files', '*.fasta'), ('Zipped FASTA files', '*.gz'), ('All files', '*.*')],
'Select a FASTA database')
if not fasta_files: sys.exit() # cancel button repsonse
# print version info, etc. (here because of the loop)
print('===============================================================')
print(' count_fasta.py, v 1.1.0, written by Phil Wilmarth, OHSU, 2017 ')
print('===============================================================')
# call counter function for each fasta file
print('start', time.ctime())
for fasta_file in fasta_files:
try:
fasta_counter(fasta_file)
except FileNotFoundError: # FastaReader class raises exception if file not found
pass
print('end', time.ctime())
# end