-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtrunk_match.py
executable file
·166 lines (143 loc) · 6.2 KB
/
trunk_match.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
#! /usr/bin/env python3
# ##################################################
# Title : analysis.py
# Author : Lim Ming Sun (Abner)
# Date : Mar 15 2018 15:05:31
# Description : post analysis of WGS run
# ##################################################
import matplotlib.pyplot as plt
from matplotlib.ticker import FormatStrFormatter
import numpy as np
import os
import re
import argparse
parser = argparse.ArgumentParser()
parser.add_argument('--trunk', help = 'Generated trunk file')
parser.add_argument('--phy', help = 'either the snv/cnv file generated by phylovar')
parser.add_argument('-o', help = 'output directory for plots')
args=vars(parser.parse_args())
phy_file = args['phy']
trunk_file = args['trunk']
out_dir = args['o']
# check the number of variants in trunk var that is carried by CSiTE
class variants:
def __init__(self):
self.chr = []
self.hap = []
self.start = []
self.end =[]
self.var = []
self.frequency = []
def check_var():
trunk = variants()
snvs = variants()
with open(phy_file,'r') as handle:
for line in handle:
if '#' not in line:
elements = line.split()
snvs.chr.append(elements[0])
snvs.start.append(elements[2])
snvs.frequency.append(elements[4])
handle.close()
with open(trunk_file, 'r') as handle:
for line in handle:
if '#' not in line:
elements = line.split()
trunk.chr.append(elements[0])
trunk.start.append(elements[2])
trunk.var.append(elements[4])
handle.close()
results = variants()
for j in range(len(trunk.chr)):
for i in range(len(snvs.chr)):
if snvs.chr[i] == trunk.chr[j] and snvs.start[i] == trunk.start[j]:
results.chr.append(snvs.chr[i])
results.start.append(snvs.start[i])
results.frequency.append(snvs.frequency[i])
print('trunk has {} variants'.format(len(trunk.chr)))
print('number of matches = {}'.format(len(results.chr)))
print([i for i in results.frequency])
freq,bins,patches=plt.hist(results.frequency,bins=int(round(np.sqrt(len(results.chr)))))
plt.xticks(np.linspace(0,max(results.frequency)+0.1, bins))
plt.xlabel('frequency')
plt.ylabel('counts')
plt.savefig(os.path.join(out_dir,'detected.png'))
def check_cnv():
trunk = variants()
cnv = variants()
with open(phy_file,'r') as handle:
for line in handle:
if "#" not in line:
elements = line.split()
cnv.chr.append(elements[0])
cnv.start.append(elements[1])
cnv.end.append(elements[2])
cnv.var.append(elements[3])
handle.close()
with open(trunk_file, 'r') as handle:
for line in handle:
if '#' not in line:
elements = line.split()
trunk.chr.append(elements[0])
trunk.start.append(elements[2])
trunk.end.append(elements[3])
trunk.hap.append(elements[1])
trunk.var.append(elements[4])
handle.close()
results = variants()
major = variants()
minor = variants()
for i in range(len(trunk.chr)):
for j in range(len(cnv.chr)):
if trunk.var[i]==cnv.var[j] and trunk.chr[i]==cnv.chr[j] and trunk.start[i]==cnv.start[j] and trunk.end[i] == cnv.end[j]:
results.chr.append(trunk.chr[i])
results.start.append(trunk.start[i])
results.end.append(trunk.end[i])
results.var.append(int(trunk.var[i]))
if int(trunk.hap[i]) == 0:
major.var.append(int(trunk.var[i]))
else:
minor.var.append(int(trunk.var[i]))
continue
print('There are {} variants'.format(len(trunk.chr)))
print('{} variants were inherited from trunk file'.format(len(results.chr)))
f=open('results.txt', 'w')
f.write('chr\tstart\tend\tvar\n')
for i in range(len(results.chr)):
f.write('{}\t{}\t{}\t{}\n'.format(results.chr[i],results.start[i],results.end[i],results.var[i]))
f.close()
plt.figure(1)
freq,bins,patches = plt.hist(results.var, bins=21)
plt.xticks(np.arange(-1,19,1))
plt.xlabel('copy number')
plt.ylabel('counts')
plt.savefig(os.path.join(out_dir,'detected_cnv.png'))
plt.figure(2)
freq,bins,patches = plt.hist(major.var, bins =21)
plt.xticks(np.arange(-1,20,1))
plt.xlabel('copy number')
plt.ylabel('count')
plt.savefig(os.path.join(out_dir,'major_cnv.png'))
plt.figure(3)
freq,bins,patches = plt.hist(minor.var, bins =21)
plt.xticks(np.arange(-1,20,1))
plt.xlabel('copy number')
plt.ylabel('count')
plt.savefig(os.path.join(out_dir,'minor_cnv.png'))
plt.figure(4)
freq,bins,patches = plt.hist(([int(i) for i in cnv.var]), bins = 21)
plt.xticks(np.arange(-1,20,1))
plt.xlabel('Copy Number')
plt.ylabel('count')
plt.savefig(os.path.join(out_dir,'CN_distribution.png'))
plt.figure(5)
freq,bins,patches = plt.hist([int(i) for i in trunk.var if re.match(r'[+-]',i)],bins = 21)
plt.xticks(np.arange(-1,20,1))
plt.xlabel('Copy Number')
plt.ylabel('count')
plt.savefig('CN_trunkvar.png')
def check_mutect_trunk():
mutect = variants()
trunk = variants()
#check_var()
check_cnv()