-
Notifications
You must be signed in to change notification settings - Fork 3
/
masker.py
executable file
·79 lines (66 loc) · 2.12 KB
/
masker.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
#!/usr/bin/env python3
# !/bin/sh
from collections import defaultdict
import re
import os
import textwrap
import argparse
import sys
def fasta(fasta_file):
seq = ''
header = ''
Dict = defaultdict(lambda: defaultdict(lambda: 'EMPTY'))
for i in fasta_file:
i = i.rstrip()
if re.match(r'^>', i):
if len(seq) > 0:
Dict[header] = seq
header = i[1:]
seq = ''
else:
header = i[1:]
seq = ''
else:
seq += i
Dict[header] = seq
return Dict
parser = argparse.ArgumentParser(
prog="masker.py",
formatter_class=argparse.RawDescriptionHelpFormatter,
description=textwrap.dedent('''
Script for masking FASTA sequences - removes positions were there are too many gaps
(how many is too many is decided by the user)
Developed by Arkadiy Garber: [email protected]
'''))
parser.add_argument('-i', help='alignment file to mask (in FASTA format)', default="NA")
parser.add_argument('-o', help="output file (also in FASTA format)", default="NA")
parser.add_argument('-m', help="maximum proportion of sequences that can contain a gap for the position to be kept in the alignment. Should be a number between 0 and 1 (default = 0.2)", default=0.2)
if len(sys.argv) == 1:
parser.print_help(sys.stderr)
sys.exit(0)
args = parser.parse_known_args()[0]
infile = open(args.i)
infile = fasta(infile)
gapDict = defaultdict(lambda: defaultdict(lambda: 'EMPTY'))
firstHeader = (list(infile.keys())[0])
firstSeq = infile[firstHeader]
for i in range(0, len(firstSeq)):
count = 0
for j in infile.keys():
char = (infile[j][i])
if char == "-":
count += 1
gapDict[i] = count/len(infile.keys())
count = 0
out = open(args.o, "w")
infile = open(args.i)
infile = fasta(infile)
for i in infile.keys():
out.write(">" + i + "\n")
for j in gapDict.keys():
if gapDict[j] < float(args.m):
out.write(infile[i][j])
else:
count += 1
out.write("\n")
print(str(int(count/len(infile.keys()))) + " positions were removed")