-
Notifications
You must be signed in to change notification settings - Fork 0
/
pick_random_snps.py
131 lines (85 loc) · 3.39 KB
/
pick_random_snps.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
import sys
import random
from optparse import OptionParser, OptionGroup
import gzip
import numpy
import timeit
start = timeit.default_timer()
parser = OptionParser()
parser.add_option("--input",dest="input",help="A file containing the dgrp haplotypes (MimicrEE input)")
parser.add_option("--output",dest="output",help="A file containing the randomly selected SNPs")
parser.add_option("--snp-number",dest="snps",type=int, help="Number of selected SNPs")
(options, args) = parser.parse_args()
rndm=None
gamma_list=[]
line_to_parse_freqval_2L=[]
line_to_parse_freqval_3R=[]
count_ancestral=0
count_derived=0
rndm2=None
writefile=open(options.output, 'w')
##keep one line after every ....
for line in open(options.input, 'r'):
if line.startswith("2L") or line.startswith("2R") or line.startswith("3L") or line.startswith("3R") :
rndm2=random.random()
if rndm2<=float(0.01):
filter_line=line
#print filter_line
k=filter_line.split("\t")
chrm=k[0]
pos=k[1]
ancestral,derived=k[3].split("/")
alleles=k[4].split(" ")
for j in range(0, len(alleles)):
allele_to_count=alleles[j]
if allele_to_count.rstrip()==str(ancestral*2):
count_ancestral=count_ancestral+1
elif allele_to_count.rstrip()==str(derived*2):
count_derived=count_derived+1
freq_ancestral=float(count_ancestral) / len(alleles)
freq_derived=float(count_derived) / len(alleles)
if chrm=="2L" and ( freq_ancestral>0.07 and freq_ancestral<0.3 or freq_derived>0.07 and freq_derived<0.3):
# pass
#else:
line_to_parse_freqval_2L.append(line)
elif chrm=="3R" and ( freq_ancestral>0.07 and freq_ancestral<0.3 or freq_derived>0.07 and freq_derived<0.3):
line_to_parse_freqval_3R.append(line)
count_ancestral, count_derived= 0,0
##keep random lines(snps)
if len(line_to_parse_freqval_2L and line_to_parse_freqval_3R)>= int(options.snps/2):
random_lines_2L=random.sample(line_to_parse_freqval_2L, int(options.snps/2))
random_lines_3R=random.sample(line_to_parse_freqval_3R, int(options.snps/2))
else:
raise ValueError("Number of random snps greater than the total number of alleles")
def make_file(random_lines, snps):
count_ancestral, count_derived= 0,0
for j in range(0, snps):
line_to_parse2=random_lines[j]
k2=line_to_parse2.split("\t")
chrm2=k2[0]
pos2=k2[1]
ancestral2,derived2=k2[3].split("/")
alleles2=k2[4].split(" ")
for j in range(0, len(alleles2)):
allele_to_count=alleles2[j]
if allele_to_count.rstrip()==str(ancestral*2):
count_ancestral=count_ancestral+1
elif allele_to_count.rstrip()==str(derived*2):
count_derived=count_derived+1
freq_ancestral=float(count_ancestral) / len(alleles)
freq_derived=float(count_derived) / len(alleles)
print(freq_ancestral,freq_derived)
if freq_ancestral>=0.5:
major=ancestral2
minor=derived2
else:
major=derived2
minor=ancestral
effect=1
writefile.writelines(chrm2+"\t"+ pos2 +"\t"+minor+"/"+major+"\t"+"%f" %effect+"\t"+"%d" %0+"\n")
count_ancestral, count_derived= 0,0
make_file(random_lines_2L, 5)
make_file(random_lines_3R, 5)
writefile.close()
stop = timeit.default_timer()
print("Elapsed time (sec):", (stop - start))