-
Notifications
You must be signed in to change notification settings - Fork 23
/
Copy pathalnFilterSeqSize.py
executable file
·59 lines (46 loc) · 2.43 KB
/
alnFilterSeqSize.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
#!/usr/bin/env python
"""
A basic script to filter a fasta file of sequences by size - a useful step to remove partial sequences or sequences that would potentially introduce a large number of gaps in the alignment. This script reads in the alignment, computes the average sequence length, and outputs a new alignment that keeps sequences of length mean +/- tolerance (tolerance default = 50)
:Arguments:
Input_MSA.fasta (the alignment to be processed)
:Keyword Arguments:
--tolerance, -t allowable sequence length variation (in number of amino acids), default: 50
--output output file name, default: FilteredAln.fa
:By: Kim Reynolds
:On: 6.5.2015
Copyright (C) 2015 Olivier Rivoire, Rama Ranganathan, Kimberly Reynolds
This program is free software distributed under the BSD 3-clause
license, please see the file LICENSE for details.
"""
import scaTools as sca
import numpy as np
import argparse
if __name__ =='__main__':
#parse inputs
parser = argparse.ArgumentParser()
parser.add_argument("alignment", help='Input Sequence Alignment')
parser.add_argument("-t","--tolerance", dest = "tol", type = int, default = 50, help="allowable sequence length variation in number of amino acids (alignment will be trimmed to mean +/- tolerance, default = 50)")
parser.add_argument("--output", dest="outputfile", default = 'FilteredAln.fa', help="specify an outputfile name")
options = parser.parse_args()
headers, seqs = sca.readAlg(options.alignment)
seqLen = np.zeros((len(seqs),1)).astype(int)
for i,k in enumerate(seqs):
seqLen[i] = len(k)
avgLen = seqLen.mean()
print ("Average sequence length: %i" % avgLen)
print ("Min: %i, Max %i" % (seqLen.min(), seqLen.max()))
minsz = avgLen - options.tol;
maxsz = avgLen + options.tol;
print ("Keeping sequences in the range: %i - %i" % (minsz, maxsz))
keepSeqs = list()
keepHeaders = list()
for i,k in enumerate(seqLen):
if (k > minsz) & (k < maxsz):
keepSeqs.append(seqs[i])
keepHeaders.append(headers[i])
print ("Keeping %i of %i total sequences" % (len(keepSeqs), len(seqLen)))
f = open(options.outputfile, 'w')
for i,k in enumerate(keepSeqs):
f.write('>%s\n' % keepHeaders[i])
f.write('%s\n' % keepSeqs[i])
f.close()