-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathvcfHeterozygosity.py
More file actions
96 lines (74 loc) · 2.99 KB
/
vcfHeterozygosity.py
File metadata and controls
96 lines (74 loc) · 2.99 KB
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
import os
import sys
import time
from os.path import exists
# get starting time of program
program_start_time = time.time()
# checks if everything is as it should be
if not len(sys.argv) == 2:
print("Please provide the path to the .vcf file")
exit()
if exists("new_VCF_filtered_het.vcf"):
print("A filtered .vcf file already exists, move it so it doesn't get overwritten")
exit()
# .vcf file path
file_path = sys.argv[1]
# sys.argv[0] is the command itself
# get number of lines
print("Determining number of lines\n")
numberOfLines = 0
#while current_line_for_length:
# numberOfLines = numberOfLines + 1
# current_line_for_length = data_file_lines.readline()
before = time.time()
numberOfLines = sum(1 for line in open(file_path, 'r'))
print("Found " + str(numberOfLines) + " lines. It took " + str(time.time() - before) + " to determine that")
# i'm dividing by numberOfLines later on, so make sure it's not equal to 0. Also, if it's 0, that's not good too, so exit there
if numberOfLines < 1:
print("number of lines equal to 0. Something went wrong")
exit()
# we now have to extract the neccesary information from the .vcf file
# we need all the 0/1 and 1/2's in the bwa.sorted.bam column, but not the 0/0 and 1/1's
print("Extracting neccesary information from .vcf ...\n")
data_file = open(file_path, 'r')
# new file that we will save all relevant data into
results_file = open("new_VCF_filtered_het.vcf", 'w')
current_line = data_file.readline()
# search for beginning of data. Also, save lines into new file, we need those lines in there (i think)
while current_line:
if current_line == "#CHROM POS ID REF ALT QUAL FILTER INFO FORMAT bwa.sorted.bam\n":
print("Found beginning of data")
results_file.write(current_line)
# read next line, which is first actual data line
current_line = data_file.readline()
break;
results_file.write(current_line)
current_line = data_file.readline()
numberOfZeroOnes = 0
numberOfOneTwos = 0
counter = 1
next_time = time.time() + 60
print("Extracting 0/1 and 1/2 chromosomes...")
# check if current line is relevant. If that is the case, save it into another file
while current_line:
# progress report
counter = counter + 1
if time.time() > next_time:
next_time = next_time + 60
print("Went over " + str(counter/numberOfLines) + "% of lines")
# we are only interested in the characters before the last ':'
split_line = current_line.split(':')
interesting_part = split_line[len(split_line) - 2]
interesting_part = interesting_part[-3:]
if interesting_part == "0/1":
results_file.write(current_line)
numberOfZeroOnes = numberOfZeroOnes + 1
elif interesting_part == "1/2":
results_file.write(current_line)
numberOfOneTwos = numberOfOneTwos + 1
current_line = data_file.readline()
data_file.close()
results_file.close()
print("There were " + str(numberOfZeroOnes) + " 0/1's and " + str(numberOfOneTwos) + " 1/2's")
run_time = time.time() - program_start_time
print("\nFinished! It took " + str(run_time) + " seconds to complete. (" + str(run_time / 60) + " minutes")