-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgbExtractFeatures.py
More file actions
182 lines (175 loc) · 10.2 KB
/
gbExtractFeatures.py
File metadata and controls
182 lines (175 loc) · 10.2 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
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
#!/usr/bin/python
# ***************************************************************
# Name: gbExtractFeatures.py
# Purpose: After SNP calling is done using lofreq, the program can take multiple VCF files
# provided in a CSV format and
# a) generate a tab delimited list to show which genes are affected by SNPs
# b) annotate a genbank file with SNPs and produce secondary genbank file(s) (either single or separate) with annotated SNPs
#
# At the bare minimal, you can first explore the SNPs using -f 0 switch:
#
# $ ./gbExtractFeatures.py -g ../Reference/MDS42reference.gb -v vcf_files_MDS42.csv -f 0
# SAMPLE=JC7, CHROM=NC_020518.1, POS=61609, REF=A, ALT=T, INFO={'SB': 12, 'DP4': [2337, 2323, 34, 53], 'DP': 4765, 'AF': 0.018258}
# SAMPLE=JC7, CHROM=NC_020518.1, POS=61621, REF=T, ALT=C, INFO={'SB': 4, 'DP4': [2369, 2375, 36, 45], 'DP': 4834, 'AF': 0.016756}
# SAMPLE=JC7, CHROM=NC_020518.1, POS=120642, REF=A, ALT=G, INFO={'SB': 56, 'DP4': [2018, 2272, 84, 38], 'DP': 4414, 'AF': 0.027639} ,
# TYPE=gene, PRODUCT=None, LOCUS_TAG=ECMDS42_RS00555, PROTEIN_ID=None, TRANSLATION=None , TYPE=CDS,
# PRODUCT=['dihydrolipoyllysine-residue acetyltransferase component of pyruvate dehydrogenase complex'],
# LOCUS_TAG=ECMDS42_RS00555, PROTEIN_ID=['WP_000963518.1'], TRANSLATION=['MAIEIKVPDIGADEVEITEILVKVGDKV
# EAEQSLITVEGDKASMEVPSPQAGIVKEIKVSVGDKTQTGALIMIFDSADGAADAAPAQAEEKKEAAPAAAPAAAAAKDVNVPDIGSDEVEVTEILVKVGDK
# VEAEQSLITVEGDKASMEVPAPFAGTVKEIKVNVGDKVSTGSLIMVFEVAGEAGAAAPAAKQEAAPAAAPAPAAGVKEVNVPDIGGDEVEVTEVMVKVGDKV
# AAEQSLITVEGDKASMEVPAPFAGVVKELKVNVGDKVKTGSLIMIFEVEGAAPAAAPAKQEAAAPAPAAKAEAPAAAPAAKAEGKSEFAENDAYVHATPLIR
# RLAREFGVNLAKVKGTGRKGRILREDVQAYVKEAIKRAEAAPAATGGGIPGMLPWPKVDFSKFGEIEEVELGRIQKISGANLSRNWVMIPHVTHFDKTDITE
# LEAFRKQQNEEAAKRKLDVKITPVVFIMKAVAAALEQMPRFNSSLSEDGQRLTLKKYINIGVAVDTPNGLVVPVFKDVNKKGIIELSRELMTISKKARDGKL
# TAGEMQGGCFTISSIGGLGTTHFAPIVNAPEVAILGVSKSAMEPVWNGKEFVPRLMLPISLSFDHRVIDGADGARFITIINNTLSDIRRLVM']
# SAMPLE=JC7, CHROM=NC_020518.1, POS=120645, REF=C, ALT=T, INFO={'SB': 24, 'DP4': [2027, 2248, 64, 39], 'DP': 4392, 'AF': 0.023452000000000001} ,
# TYPE=gene, PRODUCT=None, LOCUS_TAG=ECMDS42_RS00555, PROTEIN_ID=None, TRANSLATION=None , TYPE=CDS,
# PRODUCT=['dihydrolipoyllysine-residue acetyltransferase component of pyruvate dehydrogenase complex'],
# LOCUS_TAG=ECMDS42_RS00555, PROTEIN_ID=['WP_000963518.1'], TRANSLATION=['MAIEIKVPDIGADEVEITEILVKVGDKV
# EAEQSLITVEGDKASMEVPSPQAGIVKEIKVSVGDKTQTGALIMIFDSADGAADAAPAQAEEKKEAAPAAAPAAAAAKDVNVPDIGSDEVEVTEILVKVGDK
# VEAEQSLITVEGDKASMEVPAPFAGTVKEIKVNVGDKVSTGSLIMVFEVAGEAGAAAPAAKQEAAPAAAPAPAAGVKEVNVPDIGGDEVEVTEVMVKVGDKV
# AAEQSLITVEGDKASMEVPAPFAGVVKELKVNVGDKVKTGSLIMIFEVEGAAPAAAPAKQEAAAPAPAAKAEAPAAAPAAKAEGKSEFAENDAYVHATPLIR
# RLAREFGVNLAKVKGTGRKGRILREDVQAYVKEAIKRAEAAPAATGGGIPGMLPWPKVDFSKFGEIEEVELGRIQKISGANLSRNWVMIPHVTHFDKTDITE
# LEAFRKQQNEEAAKRKLDVKITPVVFIMKAVAAALEQMPRFNSSLSEDGQRLTLKKYINIGVAVDTPNGLVVPVFKDVNKKGIIELSRELMTISKKARDGKL
# TAGEMQGGCFTISSIGGLGTTHFAPIVNAPEVAILGVSKSAMEPVWNGKEFVPRLMLPISLSFDHRVIDGADGARFITIINNTLSDIRRLVM']
#
# The program accepts the genbank files listed in a CSV files as:
# [SAMPLE_NAME],[PATH_TO_VCF_FILE]
#
# for example,
#
# $ cat vcf_files_MDS42.csv
# JC7,../MDS42/Sample_1-JC7/Sample_1-JC7_lofreq.vcf
# JC8,../MDS42/Sample_2-JC8/Sample_2-JC8_lofreq.vcf
# JC9,../MDS42/Sample_3-JC9/Sample_3-JC9_lofreq.vcf
# JC10,../MDS42/Sample_4-JC10/Sample_4-JC10_lofreq.vcf
# JC11,../MDS42/Sample_5-JC11/Sample_5-JC11_lofreq.vcf
# JC12,../MDS42/Sample_6-JC12/Sample_6-JC12_lofreq.vcf
#
# Dependencies:
# Biopython is installed
# PyVCF https://github.com/jamescasbon/PyVCF is installed
# Tested on Python 2.6.6
# Version: 0.1 (2018-10-06)
# Authors: Umer Zeeshan Ijaz (Umer.Ijaz@glasgow.ac.uk)
# http://userweb.eng.gla.ac.uk/umer.ijaz
# Created: 2018-10-06
# License: Copyright (c) 2018 Environmental'Omics Lab, University of Glasgow, UK
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.
# **************************************************************/
import sys, getopt
from Bio import SeqIO
from Bio.SeqFeature import SeqFeature
from Bio.SeqFeature import FeatureLocation
from Bio.SeqFeature import ExactPosition
import vcf
def usage():
print './gbExtractFeatures.py -g genbank_file.gb -v vcf_files_table.csv -f FLAG'
print '-f 0(default) print features'
print '-f 1 annotate and save genebank files separately'
print '-f 2 annotate and save compound genbank file with unique features'
print '-f 3 annotate and save compound genbank file with all features'
def annotate_genbank_file_with_features(genbank_file,vcf_dict):
for i in vcf_dict.keys():
vcf_reader=vcf.Reader(filename=vcf_dict[i])
r=SeqIO.read(genbank_file,"genbank")
for record in vcf_reader:
my_feature_location=FeatureLocation(record.POS,record.POS,strand=1)
my_feature_type="SNP"
my_feature_qualifiers={"ALT":''.join(str(e) for e in record.ALT),"AF":str(record.INFO['AF'])}
my_feature=SeqFeature(my_feature_location,type=my_feature_type,qualifiers=my_feature_qualifiers)
r.features.append(my_feature)
SeqIO.write(r,i+".gb","genbank")
def annotate_compound_genbank_file_unique_features(genbank_file,vcf_dict):
r=SeqIO.read(genbank_file,"genbank")
unique_positions=[]
for i in vcf_dict.keys():
vcf_reader=vcf.Reader(filename=vcf_dict[i])
for record in vcf_reader:
my_feature_location=FeatureLocation(record.POS,record.POS,strand=1)
my_feature_type="SNP"
my_feature_qualifiers={"ALT":''.join(str(e) for e in record.ALT),"AF":str(record.INFO['AF'])}
my_feature=SeqFeature(my_feature_location,type=my_feature_type,qualifiers=my_feature_qualifiers)
if record.POS not in unique_positions:
r.features.append(my_feature)
unique_positions.append(record.POS)
SeqIO.write(r,"compound_unique.gb","genbank")
def annotate_compound_genbank_file_all_features(genbank_file,vcf_dict):
r=SeqIO.read(genbank_file,"genbank")
for i in vcf_dict.keys():
vcf_reader=vcf.Reader(filename=vcf_dict[i])
for record in vcf_reader:
my_feature_location=FeatureLocation(record.POS,record.POS,strand=1)
my_feature_type="SNP"+"_"+i
my_feature_qualifiers={"ALT":''.join(str(e) for e in record.ALT),"AF":str(record.INFO['AF'])}
my_feature=SeqFeature(my_feature_location,type=my_feature_type,qualifiers=my_feature_qualifiers)
r.features.append(my_feature)
SeqIO.write(r,"compound_all.gb","genbank")
def print_features(genbank_file,vcf_dict):
for r in SeqIO.parse(genbank_file,"genbank"):
for i in vcf_dict.keys():
vcf_reader=vcf.Reader(filename=vcf_dict[i])
for record in vcf_reader:
print "SAMPLE="+i+", ",
print "CHROM="+record.CHROM+", ",
print "POS="+str(record.POS)+", ",
print "REF="+''.join(record.REF)+", ",
print "ALT="+''.join(str(e) for e in record.ALT)+", ",
print "INFO="+str(record.INFO),
for feature in r.features:
if record.POS in feature:
if feature.type in ("CDS","gene"):
print ", ",
print "TYPE="+str(feature.type)+", ",
print "PRODUCT="+str(feature.qualifiers.get("product"))+", ",
print "LOCUS_TAG="+str("".join(feature.qualifiers.get("locus_tag")))+", ",
print "PROTEIN_ID="+str(feature.qualifiers.get("protein_id"))+", ",
print "TRANSLATION="+str(feature.qualifiers.get("translation")),
print "\n",
def main(argv):
genbank_file=''
vcf_files_table=''
flag=0
try:
opts, args=getopt.getopt(argv,"hg:v:f:",["genbank","vcf_files","flag"])
except getopt.GetoptError:
usage()
sys.exit(2)
for opt, arg in opts:
if opt == '-h':
usage()
sys.exit(2)
elif opt in ("-g", "--genbank"):
genbank_file=arg
elif opt in ("-v", "--vcf_files"):
vcf_files_table=arg
elif opt in ("-f", "--flag"):
flag=int(arg)
vcf_dict={}
#Load the sample names and VCF files into dictionary
if vcf_files_table!='':
with open(vcf_files_table) as vf:
for line in vf:
rec=line.rstrip().split(',')
vcf_dict[rec[0]]=rec[1]
if flag==0:
print_features(genbank_file,vcf_dict)
elif flag==1:
annotate_genbank_file_with_features(genbank_file,vcf_dict)
elif flag==2:
annotate_compound_genbank_file_unique_features(genbank_file,vcf_dict)
elif flag==3:
annotate_compound_genbank_file_all_features(genbank_file,vcf_dict)
if __name__ == "__main__":
main(sys.argv[1:])