-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathplot_mask.py
More file actions
34 lines (31 loc) · 1005 Bytes
/
Copy pathplot_mask.py
File metadata and controls
34 lines (31 loc) · 1005 Bytes
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
import numpy
import argparse
import sys
import csv
from Bio import SeqIO
def count(args):
win_out = []
counts = []
window = args.win
mask = list(SeqIO.parse(open(args.mask, 'r'), 'fasta'))[0]
len_record = len(mask)
variant_positions = numpy.zeros(len_record+1, dtype=bool)
for i, base in enumerate(mask.seq):
if base not in ['A', 'C', 'G', 'T']:
variant_positions[i] = True
for n in range(0, len_record, window):
count = (n, numpy.count_nonzero(variant_positions[n:n+window]))
counts.append(count)
with open("variant_density.csv", "w") as fh_out:
fh_out.write(f"position, density\n")
for line in counts:
fh_out.write(f"{line[0]}, {line[1]}\n")
if __name__ == '__main__':
parser = argparse.ArgumentParser(description='Count variant density')
parser.add_argument('mask', help='Mask file')
parser.add_argument('--win', dest='win', default='1000', type=int, help='Window size')
if len(sys.argv)==1:
parser.print_help()
sys.exit()
args = parser.parse_args()
count(args)