-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathidentify_variants.py
More file actions
83 lines (66 loc) · 4.51 KB
/
identify_variants.py
File metadata and controls
83 lines (66 loc) · 4.51 KB
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
'''
The MIT License (MIT)
Copyright 2020 John Chen
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
'''
from libraries.id_variants import id_single_variants
from libraries.helper import *
import pandas as pd
def id_from_variants( ):
vim_wt_dna = "".join(['ATGGGATTCAAACTTTTGAGTAAGTTATTGGTCTATTTGACCGCGTCTATCATGGCTATTGCGAGCCCGCTCGCTTTTTCCGTAGATTCTAGCGGAGAATATCCGACAGTCAGCGAA',
'ATTCCGGTCGGGGAGGTCCGGCTTTACCAGATTGCCGATGGTGTTTGGTCGCATATCGCAACGCAGTCGTTTGATGGCGCAGTCTACCCGTCCAATGGTCTCATTGTCCGTGATGGT',
'GATGAGTTGCTTTTGATTGATACAGCGTGGGGTGCGAAAAACACAGCGGCACTTCTCGCGGAGATTGAGAAGCAAATTGGACTTCCTGTAACGCGTGCAGTCTCCACGCACTTTCAT',
'GACGACCGCGTCGGCGGCGTTGATGTCCTTCGGGCGGCTGGGGTGGCAACGTACGCATCACCGTCGACACGCCGGCTAGCCGAGGTAGAGGGGAACGAGATTCCCACGCACTCTCTT',
'GAAGGACTTTCATCGAGCGGGGACGCAGTGCGCTTCGGTCCAGTAGAACTCTTCTATCCTGGTGCTGCGCATTCGACCGACAACTTAATTGTGTACGTCCCGTCTGCGAGTGTGCTC',
'TATGGTGGTTGTGCGATTTATGAGTTGTCACGCACGTCTGCGGGGAACGTGGCCGATGCCGATCTGGCTGAATGGCCCACCTCCATTGAGCGGATTCAACAACACTACCCGGAAGCA',
'CAGTTCGTCATTCCGGGGCACGGCCTGCCGGGCGGTCTTGACTTGCTCAAGCACACAACGAATGTTGTAAAAGCGCACACAAATCGCTCAGTCGTTGAGTAA'])
# paths for input and output
path = 'merged_reads'
outpath = 'processed_counts'
make_sure_path_exists(outpath)
# load list of files
files = pd.read_csv('merged_reads/fastq_merge_report.txt', sep='\t')
files = files[~(files['name'].str.startswith('wt'))]['name'] # get all non-wt files
offset_info = pd.read_excel('sample_reference.xlsx', sheet_name='trim_info').set_index('group')
out = {}
for f in files:
sep = f.split('_')
label = '_'.join(sep[:2])
group = sep[2][-1]
rep = sep[3][-1]
offset = offset_info.at[int(group),'offset'] # dna offset
length = offset_info.at[int(group),'length'] # dna length after offset
df = pd.read_csv(f"{path}/{f}/{f}.txt", sep='\t', index_col=0)
wt = vim_wt_dna[offset:offset+length]
results = id_single_variants(df, wt, offset)
# extract results and add extra info
single_codon_variants = results['single_codon_variants']
single_aa_variants = results['single_aa_variants']
single_codon_variants['group'] = group
single_aa_variants['group'] = group
single_codon_variants['rep'] = rep
single_aa_variants['rep'] = rep
codon_mut_counts = results['codon_mut_counts']
codon_mut_counts['group'] = group
codon_mut_counts['rep'] = rep
aa_mut_counts = results['aa_mut_counts']
aa_mut_counts['group'] = group
aa_mut_counts['rep'] = rep
if out.get(label):
out[label]['codon_var'].append(single_codon_variants)
out[label]['aa_var'].append(single_aa_variants)
out[label]['codon_mut_counts'].append(codon_mut_counts)
out[label]['aa_mut_counts'].append(aa_mut_counts)
else:
out[label] = {'codon_var':[single_codon_variants],'aa_var':[single_aa_variants],'codon_mut_counts':[codon_mut_counts],'aa_mut_counts':[aa_mut_counts]}
# aggregate and save the data from each label (condition)
for label, data in out.items():
prepath = f"{outpath}/{label}"
make_sure_path_exists(prepath)
for name, data_group in data.items():
d = pd.concat(data_group)
d.to_csv(f'{prepath}/{label}_{name}.txt', sep='\t')
if __name__=='__main__':
id_from_variants()