-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathseq_stats.py
executable file
·91 lines (73 loc) · 2.94 KB
/
seq_stats.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
#!/usr/bin/env python3
#################
# Author: B.M. Anderson
# Date: 27 Apr 2020
# Modified: May 2023 (add alignment length), Mar 2023 (add gaps),
# Nov 2020 (changed from proportion to percentage Ns), Oct 2022 (changed screen output format)
# Aug 2024 (update deprecated GC function)
# Description: output basic sequence statistics for a set of input fasta files
#################
import sys
from Bio import SeqIO
from Bio.SeqUtils import gc_fraction
from Bio.Seq import Seq
def help():
print('A script to output basic sequence statistics for a set of input fasta files')
print('')
print('Usage: ' + str(sys.argv[0]) + ' fasta1 fasta2 fasta3 ...')
print('')
# print help if the script is called without arguments
if len(sys.argv[1:]) == 0:
sys.exit(help())
# reconstruct a GC function (see https://github.com/nextgenusfs/funannotate/issues/1000#issue-2125508955)
def GC(sequence):
return 100 * gc_fraction(sequence, ambiguous = 'ignore')
# for each of the input files, read the sequence(s) then calculate and output stats
cum_seqs = []
index = 1
print('\t'.join(['File', 'ID', 'Length', 'GC', '% N', '% Gaps', '% Missing']))
align_lengths = []
for fasta in sys.argv[1:]:
fsa = SeqIO.parse(open(fasta, 'r'), 'fasta')
entries = []
for entry in fsa:
entries.append(entry)
seqs = []
lengths = []
for entry in entries:
seqs.append(entry.seq)
n_count = entry.seq.count('N') + entry.seq.count('n')
gap_count = entry.seq.count('-')
lengths.append(len(entry.seq))
print('\t'.join([fasta, entry.id, str(len(entry.seq)), '%.2f' % GC(entry.seq),
'%.2f' % (float(100*n_count)/len(entry.seq)),
'%.2f' % (float(100*gap_count)/len(entry.seq)),
'%.2f' % (float(100*(n_count + gap_count)/len(entry.seq)))]))
cum_seq = Seq('').join(seqs)
cum_seqs.append(cum_seq)
n_count = cum_seq.count('N') + cum_seq.count('n')
gap_count = cum_seq.count('-')
if len(seqs) > 1:
print('\t'.join([fasta, 'Total (' + str(len(seqs)) + ')', str(len(cum_seq)),
'%.2f' % GC(cum_seq), '%.2f' % (float(100*n_count)/len(cum_seq)),
'%.2f' % (float(100*gap_count)/len(cum_seq)),
'%.2f' % (float(100*(n_count + gap_count)/len(cum_seq)))]))
# check if the lengths are all the same (looking at alignments)
if len(set(lengths)) == 1:
align_lengths.append(lengths[0])
index = index + 1
# check whether reporting alignment lengths is appropriate
if len(align_lengths) == (index - 1):
align_length = sum(align_lengths)
else:
align_length = 'n/a'
overall_seq = Seq('').join(cum_seqs)
n_count = overall_seq.count('N') + overall_seq.count('n')
gap_count = overall_seq.count('-')
if len(cum_seqs) > 1:
print('')
print('\t'.join(['Summary', 'Length', 'Align_length', 'GC', '% N', '% Gaps', '% Missing']))
print('\t'.join([str(index - 1) + ' files', str(len(overall_seq)), str(align_length),
'%.2f' % GC(overall_seq), '%.2f' % (float(100*n_count)/len(overall_seq)),
'%.2f' % (float(100*gap_count)/len(overall_seq)),
'%.2f' % (float(100*(n_count + gap_count)/len(overall_seq)))]))