-
Notifications
You must be signed in to change notification settings - Fork 22
/
Copy pathblock.py
executable file
·199 lines (159 loc) · 5.55 KB
/
block.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
#!/usr/bin/env python
import argparse, sys
from argparse import RawTextHelpFormatter
import numpy as np
import operator
__author__ = "Author (email@site.com)"
__version__ = "$Revision: 0.0.1 $"
__date__ = "$Date: 2013-05-09 14:31 $"
# --------------------------------------
# define functions
def get_args():
parser = argparse.ArgumentParser(formatter_class=RawTextHelpFormatter, description="\
block.py\n\
author: " + __author__ + "\n\
version: " + __version__ + "\n\
description: Basic python script template")
# parser.add_argument('-a', '--argA', metavar='argA', type=str, required=True, help='description of argument')
#parser.add_argument('-b', '--argB', metavar='argB', required=False, help='description of argument B')
#parser.add_argument('-c', '--flagC', required=False, action='store_true', help='sets flagC to true')
parser.add_argument('-d', '--maxDiff', type=int, required=False, default=10, help='max deviation from expected to include')
parser.add_argument('input', nargs='?', type=argparse.FileType('r'), default=None, help='file to read. If \'-\' or absent then defaults to stdin.')
# parse the arguments
args = parser.parse_args()
# if no input, check if part of pipe and if so, read stdin.
if args.input == None:
if sys.stdin.isatty():
parser.print_help()
exit(1)
else:
args.input = sys.stdin
# send back the user input
return args
# primary function
def read(file):
frame = list()
for line in file:
v = line.rstrip().split('\t')
v[4:7] = map(float,v[4:7])
v[11:14] = map(float,v[11:14])
frame.append(v)
return frame
# switches alleles so that 0 is maj and 2 is min
def majMin(myFrame):
for i in range(len(myFrame)):
pair = myFrame[i]
if pair[4] < pair[6]:
#print pair
gt = pair[14][0]
minFreq = pair[4]
majFreq = pair[6]
newGt = str
if gt == '0':
newGt = '2'
elif gt == '2':
newGt = '0'
else: newGt = gt
pair[14] = newGt + pair[14][1]
pair[4] = majFreq
pair[6] = minFreq
if pair[13] > pair[11]:
gt = pair[14][1]
minFreq = pair[11]
majFreq = pair[13]
newGt = str
if gt == '0':
newGt = '2'
elif gt == '2':
newGt = '0'
else: newGt = gt
pair [14] = pair[14][0] + newGt
pair[11] = majFreq
pair[13] = minFreq
myFrame[i] = pair
myFrame = sorted(myFrame, key=operator.itemgetter(14))
return myFrame
def block(file, maxDiff):
frame = read(file)
frame = majMin(frame)
prevGt = -1
obs = list()
exp = list()
diffs = list()
ref1 = list()
alt1 = list()
ref2 = list()
alt2 = list()
# print header
print '\t'.join(("# gt", "mean", "stdev", "numPairs", "meanObs", "meanExp", "meanRef1", "meanRef2"))
for v in frame:
gt = v[14]
if gt != prevGt and prevGt != -1:
for d in diffs:
if abs(d - np.mean(diffs) > maxDiff):
index = diffs.index(d)
diffs.remove(d)
obs.pop(index)
exp.pop(index)
ref1.pop(index)
alt1.pop(index)
ref2.pop(index)
alt2.pop(index)
refMaj1 = list()
refMaj2 = list()
for i in range(len(ref1)):
refMaj1.append( ref1[i] - alt1[i] )
refMaj2.append( ref2[i] - alt2[i] )
# omitArr = diffs[0:i] + diffs[i+1:len(diffs)]
# print(np.std(omitArr))
print '%s\t%.2f\t%.2f\t%s\t%.2f\t%.2f\t%.2f\t%.2f' % (prevGt,np.mean(diffs),np.std(diffs),len(diffs),np.mean(obs),np.mean(exp),np.mean(refMaj1),np.mean(refMaj2))
diffs = list()
obs = list()
exp = list()
ref1 = list()
alt1 = list()
ref2 = list()
alt2 = list()
# print line.rstrip()
counts = v[15].split('|')
# difference between observed and expected
diffs.append(float(counts[2]))
obs.append(float(counts[0]))
exp.append(float(counts[1]))
ref1.append(float(v[4]))
alt1.append(float(v[6]))
ref2.append(float(v[11]))
alt2.append(float(v[13]))
prevGt = gt
for d in diffs:
if abs(d - np.mean(diffs) > maxDiff):
index = diffs.index(d)
diffs.remove(d)
obs.pop(index)
exp.pop(index)
ref1.pop(index)
alt1.pop(index)
ref2.pop(index)
alt2.pop(index)
for i in range(len(ref1)):
refMaj1.append( ref1[i] - alt1[i] )
refMaj2.append( ref2[i] - alt2[i] )
# omitArr = diffs[0:i] + diffs[i+1:len(diffs)]
# print(np.std(omitArr))
print '%s\t%.2f\t%.2f\t%s\t%.2f\t%.2f\t%.2f\t%.2f' % (prevGt,np.mean(diffs),np.std(diffs),len(diffs),np.mean(obs),np.mean(exp),np.mean(refMaj1),np.mean(refMaj2))
return
# --------------------------------------
# main function
def main():
# parse the command line args
args = get_args()
# store into global values
file = args.input
maxDiff = args.maxDiff
# call primary function
block(file, maxDiff)
# close the input file
file.close()
# initialize the script
if __name__ == '__main__':
sys.exit(main())