-
Notifications
You must be signed in to change notification settings - Fork 52
Expand file tree
/
Copy pathread_seq.py
More file actions
executable file
·37 lines (32 loc) · 1.21 KB
/
read_seq.py
File metadata and controls
executable file
·37 lines (32 loc) · 1.21 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
#!/usr/bin/env python
from argparse import ArgumentParser
from Bio import SeqIO, SeqUtils
from collections import namedtuple
SeqStats = namedtuple('SeqStats', ['length', 'gc', 'weight'])
def compute_stats(seq):
stats = SeqStats
stats.length = len(seq)
stats.gc = SeqUtils.GC(seq)
try:
stats.weight = SeqUtils.molecular_weight(seq)
except ValueError:
stats.weight = None
return stats
if __name__ == '__main__':
arg_parser = ArgumentParser(description='Read sequence file and '
'compute some statistics')
arg_parser.add_argument('file', help='sequence file to parse')
arg_parser.add_argument('--format', default='fasta', help='file format')
options = arg_parser.parse_args()
seqs = {
seq_record.id: seq_record.seq
for seq_record in SeqIO.parse(options.file, options.format)
}
fmt_str = ('id: {id}\n\t'
'length: {stats.length}\n\t'
'gc: {stats.gc}\n\t'
'molecular weight: {stats.weight}')
for id, seq in seqs.items():
seq_stats = compute_stats(seq)
print(fmt_str.format(id=id, stats=seq_stats))
print('{0:d} sequences'.format(len(seqs)))