-
Notifications
You must be signed in to change notification settings - Fork 1
/
nanoforenstr.py
executable file
·196 lines (134 loc) · 7.03 KB
/
nanoforenstr.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
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
"""
- ForenRepeat ver0.0.1
- Written by Ren Zilin; zilin.ren@outlook.com
- For reads from the forenseq kit and nanopore platform
"""
##
import os
import argparse
import pathlib
import pandas as pd
from tqdm import tqdm
## local pack
# import sys
# sys.path.insert(0, "myLibs")
from myLibs import myBasicCount
from myLibs.myLocalDPCount import func_reads_covering_str_locus, func_get_repeat_allele, func_str_genotyper
from myLibs import myPatternReader
# import myplot
## parameters
# parser = argparse.ArgumentParser(description='Repeat quantification for reads from forenseq kit. Still working on')
# parser.add_argument('--BAM', help='input1: *.bam file', default = None)
# parser.add_argument('--PAT', help='input2: repeat pattern file', default = None)
# parser.add_argument('--ID', help='output file name', default = None)
# args = parser.parse_args()
# bam_file_path = args.BAM
# pattern_file_path = args.PAT
# sample = args.ID
def quick(args):
bam_file_path = args.BAM
pattern_file_path = args.PAT
sample = args.ID
## create dir
path1 = 'nanoforenstr_output/%s' % sample
path2 = 'nanoforenstr_output/%s/figs' % sample
pathlib.Path(path2).mkdir(parents=True, exist_ok=True)
## pattern file
pattern_dict = myPatternReader.func_read_in_pattern_file(pattern_file_path)
result_lst = []
for str_name in pattern_dict:
chrom = pattern_dict[str_name][0]
start = int(pattern_dict[str_name][1])
end = int(pattern_dict[str_name][2])
pattern = pattern_dict[str_name][3]
count_lst = myBasicCount.func_extract_reads_in_bam(bam_file_path, chrom, start, end, pattern)
genotype, valid_alleles_num, total_cov, infos = myBasicCount.func_str_genotyper(count_lst)
## plot hist
# fig = myplot.func_save_plot(count_lst, str_name, path2)
## output allele
if valid_alleles_num < 2:
result_lst.append([str_name, chrom, pattern, genotype[1], genotype[0], valid_alleles_num, total_cov, infos])
else :
result_lst.append([str_name, chrom, pattern, sorted(genotype)[0], sorted(genotype)[1], valid_alleles_num, total_cov, infos])
pd.DataFrame(result_lst,
columns=["# STR", "CHR", "pattern",
"allele1", "allele2", "valid_alleles_num",
"total_cov", "infos"]).to_csv('%s/forenRep.csv' % path1, index=None, na_rep="NA")
return
###################### LA Local Align
def LA(args):
def fetch_flanking_seq(chrom, start, end, flanking_len, reference_genome, samtools):
pre_flanking_coor = "%s:%s-%s" % (chrom, int(start) - flanking_len, int(start)-1)
suf_flanking_coor = "%s:%s-%s" % (chrom, int(end) + 1, int(end) + flanking_len)
# print("## USE samtools to extract the flanking seq;\n## the command: %s faidx %s %s" % (samtools, reference_genome, pre_flanking_coor))
faidx_output = os.popen("%s faidx %s %s" % (samtools, reference_genome, pre_flanking_coor)).read()
pre_flanking = faidx_output.split('\n')[1]
# print("## USE samtools to extract the flanking seq;\n## the command: %s faidx %s %s" % (samtools, reference_genome, suf_flanking_coor))
faidx_output = os.popen("%s faidx %s %s" % (samtools, reference_genome, suf_flanking_coor)).read()
sup_flanking = faidx_output.split('\n')[1]
return pre_flanking, sup_flanking
bam_file_path = args.BAM
pattern_file_path = args.PAT
sample = args.ID
reference_genome = args.ref
samtools = args.samtools
## create dir
path1 = 'nanoforenstr_output/%s' % sample
pathlib.Path(path1).mkdir(parents=True, exist_ok=True)
# path2 = 'nanoforenstr_output/%s/figs' % sample
# pathlib.Path(path2).mkdir(parents=True, exist_ok=True)
## pattern file
pattern_dict = myPatternReader.func_read_in_pattern_file(pattern_file_path)
result_lst = []
pbar = tqdm(pattern_dict)
for str_ind, str_name in enumerate(pbar):
pbar.set_description('## The locus %s is under inference {%d/%d}' % (str_name, str_ind + 1, len(pattern_dict)))
# for str_ind, str_name in enumerate(pattern_dict):
# print('## The locus %s is under inference {%d/%d}' % (str_name, str_ind + 1, len(pattern_dict)), flush = True)
chrom = pattern_dict[str_name][0]
start = int(pattern_dict[str_name][1])
end = int(pattern_dict[str_name][2])
pattern = pattern_dict[str_name][3]
## reads
flanking_len = 20
seq_prefix, seq_suffix = fetch_flanking_seq(chrom, start, end, flanking_len, reference_genome, samtools)
read_lst = func_reads_covering_str_locus(chrom, start, end, bam_file_path, flanking_len, seq_prefix, seq_suffix)
## copy number list
copy_number_lst = func_get_repeat_allele(read_lst, pattern)
## Get genotype
genotype, valid_alleles_num, total_cov, infos = func_str_genotyper(copy_number_lst, .56)
## plot hist
# fig = myplot.func_save_plot(copy_number_lst, str_name, path2)
## output allele
result_lst.append([str_name, chrom, pattern, genotype[0], genotype[1], valid_alleles_num, total_cov, infos])
pd.DataFrame(result_lst,
columns=["# STR", "CHR", "pattern",
"allele1", "allele2", "valid_alleles_num",
"total_cov", "infos"]).to_csv('%s/%s.csv' % (path1, sample), index=None, na_rep="NA")
return
if __name__ == '__main__':
parser = argparse.ArgumentParser(description='Genotyping forensic STR',
usage='''python NanoForenRepeat.py <options> [<args>]
Available options are:
quick Genotyping forensic STR by quick mode
LA Genotyping forensic STR by local align (recommend)
''')
subparsers = parser.add_subparsers(help='sub-command help')
subparsers.required = True
## quick
qucik_parser = subparsers.add_parser('quick', help='Genotyping forensic STR by quick mode')
qucik_parser.add_argument('--BAM', help='input1: *.bam file', default = None)
qucik_parser.add_argument('--PAT', help='input2: repeat pattern file', default = None)
qucik_parser.add_argument('--ID', help='output file name', default = None)
qucik_parser.set_defaults(func=quick)
## localalign
window_parser = subparsers.add_parser('LA', help='Genotyping forensic STR by local align')
window_parser.add_argument('--BAM', help='input1: *.bam file', default = None)
window_parser.add_argument('--PAT', help='input2: repeat pattern file', default = None)
window_parser.add_argument('--ID', help='output file name', default = None)
window_parser.add_argument('--ref', help='reference genome', default = None)
window_parser.add_argument('--samtools', help='samtools', default = None)
window_parser.set_defaults(func=LA)
## run
args = parser.parse_args()
args.func(args)