-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathscale_gt.py
executable file
·176 lines (139 loc) · 5.82 KB
/
scale_gt.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
#!/usr/bin/env python
import argparse, sys
# import math, time, re
import numpy as np
from scipy import stats
from argparse import RawTextHelpFormatter
__author__ = "Colby Chiang (cchiang@genome.wustl.edu)"
__version__ = "$Revision: 0.0.1 $"
__date__ = "$Date: 2015-10-22 11:25 $"
# --------------------------------------
# define functions
def get_args():
parser = argparse.ArgumentParser(formatter_class=RawTextHelpFormatter, description="\
scale_gt.py\n\
author: " + __author__ + "\n\
version: " + __version__ + "\n\
description: continuous ld")
parser.add_argument('-i', '--input', metavar='VCF', dest='vcf_in', type=argparse.FileType('r'), default=None, help='VCF input [stdin]')
parser.add_argument('-v', '--variants', metavar='FILE', dest='variants_file', type=argparse.FileType('r'), default=None, required=False, help='list of variants to include')
parser.add_argument('-s', '--samples', metavar='FILE', dest='samples_file', type=argparse.FileType('r'), default=None, required=False, help='list of samples to include')
parser.add_argument('-f', '--field', metavar='STR', dest='field', default='DS', help='specify genotyping format field [DS]')
parser.add_argument('-r', '--range', metavar='STR', dest='range', default='0,2', help='range to scale over [0,2]')
# parser.add_argument('-c', '--covar', metavar='FILE', dest='covar', type=argparse.FileType('r'), default=None, required=True, help='tab delimited file of covariates')
# parser.add_argument('-v', '--max_var', metavar='FLOAT', dest='max_var', type=float, default=0.1, help='maximum genotype variance explained by covariates for variant to PASS filtering [0.1]')
# parse the arguments
args = parser.parse_args()
# if no input, check if part of pipe and if so, read stdin.
if args.vcf_in == None:
if sys.stdin.isatty():
parser.print_help()
exit(1)
else:
args.vcf_in = sys.stdin
# send back the user input
return args
# primary function
def scale_gt(vcf_in, var_list, samp_set, field, scale_range):
# X = {} # dict of genotypes for each sample, key is variant id
# var_ids = []
samp_cols = []
for line in vcf_in:
if line[:2] == '##':
print line.rstrip()
continue
v = line.rstrip().split('\t')
if line[0] == "#":
for i in xrange(9,len(v)):
if v[i] in samp_set or len(samp_set) == 0:
samp_cols.append(i)
print line.rstrip()
continue
if v[2] not in var_list and len(var_list):
continue
var_id = v[2]
# print v[:6]
# read the genotypes
fmt = v[8].split(':')
field_idx = -1
for i in xrange(len(fmt)):
if fmt[i] == field:
field_idx = i
break
if field_idx == -1:
sys.stderr.write("Format field '%s' not found for variant %s\n" % (field, v[2]))
exit(1)
fmt_list = []
for i in samp_cols:
fmt_str = v[i].split(':')[field_idx]
fmt_list.append(fmt_str)
# ensure that there are informative calls in dataset
if set(fmt_list) != set(['.']):
min_fmt = np.min([float(x) for x in fmt_list if x != "."])
max_fmt = np.max([float(x) for x in fmt_list if x != "."])
# print 'min_fmt', min_fmt
# print 'max_fmt', max_fmt
scalar = (max_fmt - min_fmt) / (scale_range[1] - scale_range[0])
shift = min_fmt - scale_range[0]
# print scalar, shift
# if scalar == 0:
# print line.rstrip()
# print fmt_list.rstrip()
# print 'fmt', fmt_list
# np.seterr(all='raise')
# print "scalar", scalar, min_fmt, max_fmt, v[:5], set(fmt_list)
fmt_scaled = []
for x in fmt_list:
if x == "." or scalar == 0:
fmt_scaled.append(".")
else:
fmt_scaled.append((float(x) - shift) / scalar)
# fmt_scaled = [(float(x) - shift) / scalar for x in fmt_list]
# print 'scaled', fmt_scaled
# print 'min_scaled', np.min([float(x) for x in fmt_scaled if x != "."])
# print 'max_scaled', np.max([float(x) for x in fmt_scaled if x != "."])
for i in samp_cols:
# print 'before', v[i]
fmt_split = v[i].split(':')
scaled_value = fmt_scaled[i - 9]
if scaled_value == ".":
fmt_split[field_idx] = scaled_value
else:
fmt_split[field_idx] = "%0.6f" % scaled_value
v[i] = ':'.join(fmt_split)
print '\t'.join(v)
# X[var_id] = fmt_list
# if len(var_list) != len(X):
# sys.stderr.write("Warning, missing variants\n")
# exit(1)
return
# --------------------------------------
# main function
def main():
# parse the command line args
args = get_args()
# get list of variants to examine
var_list = []
if args.variants_file is not None:
for line in args.variants_file:
var_list.append(line.rstrip())
args.variants_file.close()
# get list of samples to examine
samp_set = set()
if args.samples_file is not None:
for line in args.samples_file:
v = line.rstrip().split('\t')
samp_set.add(v[0])
args.samples_file.close()
scale_range = map(int, args.range.split(','))
# call primary function
scale_gt(args.vcf_in, var_list, samp_set, args.field, scale_range)
# close the files
args.vcf_in.close()
# initialize the script
if __name__ == '__main__':
try:
sys.exit(main())
except IOError, e:
if e.errno != 32: # ignore SIGPIPE
raise