-
Notifications
You must be signed in to change notification settings - Fork 9
/
121d_SlidingWindow.py
225 lines (188 loc) · 8.18 KB
/
121d_SlidingWindow.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
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
#-----------------------------------------------------------+
# |
# 121_SlidingWindow.py - plot data with sliding window on multiple chromosome |
# |
#-----------------------------------------------------------+
# |
# AUTHOR: Vikas Gupta |
# CONTACT: [email protected] |
# STARTED: 09/06/2013 |
# UPDATED: 09/06/2013 |
# |
# DESCRIPTION: |
# Short script to convert and copy the wheat BACs |
# Run this in the parent dir that the HEX* dirs exist |
# |
# LICENSE: |
# GNU General Public License, Version 3 |
# http://www.gnu.org/licenses/gpl.html |
# |
#-----------------------------------------------------------+
# Example:
# python ~/script/python/100b_fasta2flat.py -i 02_Stegodyphous_cdna.refined.fa.orf.tr_longest_frame
### import modules
import os,sys,getopt, re, classVCF
### global variables
global ifile, window_size, step_size, window_count, step_count, callable_file
### make a logfile
import datetime
now = datetime.datetime.now()
o = open(str(now.strftime("%Y-%m-%d_%H%M."))+'logfile','w')
### write logfile
def logfile(infile):
o.write("Program used: \t\t%s" % "100b_fasta2flat.py"+'\n')
o.write("Program was run at: \t%s" % str(now.strftime("%Y-%m-%d_%H%M"))+'\n')
o.write("Infile used: \t\t%s" % infile+'\n')
def help():
print '''
python 100b_fasta2flat.py -i <ifile>
-j <callable_file>
-w <window_size_pos> ### Position based
-c <window_size_count> ### count based
File must follow the format:
<chromosome> <Position> <DataPoints>
'''
sys.exit(2)
### main argument to
def options(argv):
global ifile, window_size, step_size, window_count, step_count, callable_file
ifile = ''
callable_file = ''
window_size = 1000
step_size = 100
window_count = 1000
step_count = 100
try:
opts, args = getopt.getopt(argv,"hi:j:w:c:s:d:",["ifile=","callable_file=","window_size_pos=","step_size_pos=","window_size_count=","step_size_count=" ])
except getopt.GetoptError:
help()
for opt, arg in opts:
if opt == '-h':
help()
elif opt in ("-i", "--ifile"):
ifile = arg
elif opt in ("-j", "--callable_file"):
callable_file = arg
elif opt in ("-w", "--window_size_pos"):
window_size = int(arg)
elif opt in ("-c", "--window_size_count"):
window_count = float(arg)
elif opt in ("-s", "--step_size_pos"):
step_size = int(arg)
elif opt in ("-d", "--window_size_count"):
step_count = float(arg)
logfile(ifile)
def get_size(file):
count = 0
chroHash = {}
for line in open(file,'r'):
count += 1
### print the lines processed
if count%100000 == 0:
print 'Lines processed: ', '{:9,.0f}'.format(count)
line = line.strip()
if len(line)>1 and not line.startswith('#'):
token = line.split('\t')
if token[0] not in chroHash:
chroHash[token[0]] = ''
return chroHash
def parse_call(chromosome,callable_file):
hash = {}
count = 0
for line in open(callable_file,'r'):
if len(line)>1 and not line.startswith('#'):
#obj = classVCF.VCF(line)
count += 1
### print the lines processed
if count%100000 == 0:
print 'Lines processed in callable fraction file: ', chromosome, '{:9,.0f}'.format(count)
line = line.strip()
tokens = line.split('\t')
if tokens[0]==chromosome:
pos = int(tokens[1])
hash[pos] = ''
return hash
def parse(chromosome, o):
count = 0
data_hash = {}
for line in open(ifile, 'r'):
if len(line)>1 and not line.startswith('#'):
count += 1
### print the lines processed
if count%100000 == 0:
print 'Lines processed in the variant file: ', chromosome, '{:9,.0f}'.format(count)
line = line.strip()
tokens = line.split('\t')
if tokens[0]==chromosome:
pos = int(tokens[1])
data_hash[pos] = float(tokens[2]), float(tokens[3]), float(tokens[4]), float(tokens[5]), float(tokens[6]), float(tokens[7]) ,\
float(tokens[8]), float(tokens[9])
return data_hash
def window(chromosome, o, data_hash, call_hash):
### run the window position based analysis
if len(data_hash)>0:
start = min(data_hash)
end = max(data_hash)
for i in range(start, end+1, step_size):
print 'Lines processed printing out: ', chromosome, '{:9,.0f}'.format(i)
pos_sum = 0
snp_sum = 0
depth_sum = 0
genotypeCalls_sum = 0
genotypeCallsHete_sum = 0
genotypeCallsHomo_sum = 0
InbreedingCoeffs_sum = 0
HaplotypeScores_sum = 0
temp_count = 0
for j in range(i, i+window_size):
pos_sum += 1
if j in call_hash:
temp_count += 1
depth_sum += 0
if j in data_hash:
call, snp, depth, genotypeCalls, genotypeCallsHete, genotypeCallsHomo, InbreedingCoeffs, HaplotypeScores = data_hash[j]
snp_sum += snp
genotypeCalls_sum += genotypeCalls
genotypeCallsHete_sum += genotypeCallsHete
genotypeCallsHomo_sum += genotypeCallsHomo
InbreedingCoeffs_sum += InbreedingCoeffs
HaplotypeScores_sum += HaplotypeScores
if temp_count > 0:
if snp_sum > 0:
o.write(chromosome+'\t'+ \
str(i+window_size/2) + '\t' + \
str(temp_count/float(pos_sum))+ '\t' + \
str(snp_sum/float(temp_count))+ '\t' + \
str(depth_sum/float(temp_count))+ '\t'+ \
str(genotypeCalls_sum/float(snp_sum))+ '\t' + \
str(genotypeCallsHete_sum/float(snp_sum))+ '\t'+ \
str(genotypeCallsHomo_sum/float(snp_sum))+ '\t'+ \
str(InbreedingCoeffs_sum/float(snp_sum))+ '\t'+ \
str(HaplotypeScores_sum/float(snp_sum))+ '\n')
else:
o.write(chromosome+'\t'+ \
str(i+window_size/2) + '\t' + \
str(temp_count/float(pos_sum))+ '\t' + \
str(snp_sum/float(temp_count))+ '\t' + \
str(depth_sum/float(temp_count))+ '\t'+ \
str(0)+ '\t' + \
str(0)+ '\t'+ \
str(0)+ '\t'+ \
str(0)+ '\t'+ \
str(0)+ '\n')
if __name__ == "__main__":
options(sys.argv[1:])
HEADER = '#Chromosome\tPosition\tCallable\tSNP\tDepth\tgenotypeCalls\tgenotypeCallsHete\tgenotypeCallsHomo\tInbreedingCoeffs\tHaplotypeScores'
print 'Hashing the chromosomes name'
chroHash = get_size(ifile)
o = open(ifile+'.w'+str(window_size)+'.s'+str(step_size), 'w')
o.write(HEADER+'\n')
for chromosome in sorted(chroHash):
if chromosome != 'chr0':
### hash the callable fraction
call_hash = parse_call(chromosome,callable_file)
### parse file
data_hash = parse(chromosome, o)
window(chromosome, o, data_hash, call_hash)
### close the logfile
o.close()