-
Notifications
You must be signed in to change notification settings - Fork 0
/
symvcf.py
56 lines (46 loc) · 1.4 KB
/
symvcf.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
import sys
import os
import subprocess
import csv
import optparse
def read_file(path):
try:
with open(path,'r') as csvfile:
reader = csv.reader(csvfile, delimiter='\t')
rows = [row for row in reader]
return rows
except Exception as e:
print("Read from: %s, ERROR: %s" %(path, e))
def write_file(path, data):
try:
with open(path, 'w') as csvfile:
writer = csv.writer(csvfile, delimiter='\t')
for row in data:
writer.writerow(row)
except Exception as e:
print ("Write to: %s, ERROR: %s" %(path, e))
if __name__ == '__main__':
# read sample list
parser = optparse.OptionParser()
parser.add_option('-i', '--ifile', dest='input')
(options, args) = parser.parse_args()
sample_list = options.input
# read .txt
txt = read_file(sample_list)
# read .vcf
orig_vcf = read_file(sample_list[0:-4]+'.vcf')
if len(orig_vcf) == 0:
os._exit()
# symchromize .vcf
valid_pos = []
for i in iter(txt):
if i[0][0] != '#':
valid_pos.append(i[0]+' '+i[1])
sym_vcf = []
sym_vcf.append(orig_vcf[0])
for i in iter(orig_vcf):
if i[0][0] != '#':
if i[0]+' '+i[1] in valid_pos:
sym_vcf.append(i))
# output
write_file(sample_list[0:-4]+'.vcf', sym_vcf)