-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathextractmutationsfromgbk.pl
178 lines (141 loc) · 5.66 KB
/
extractmutationsfromgbk.pl
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
# first, bring in the SeqIO module
use Bio::SeqIO;
use Bio::Seq;
use List::MoreUtils qw(any);
use Getopt::Long;
use Bio::Tools::CodonTable;
my $startdir = '.';
my $refsource = '.';#'/groups/kishony/Reference_Genomes/';
my $outdir = '.';
my $mutfile = "newtest5variants.txt";
my $outfile = 'mutations.txt';
my $promoterlength=150;
GetOptions ('s|startdir=s' => \$startdir, #directory with reads split into folders
'r|g|refsource=s' => \$refsource, #where is the reference genome?
'i|mutfile=s' => \$mutfile, #where is the mutations vcf
'o|outfile=s' => \$outfile, #to what directory should analyses be outputted?
'runtag=s' => \$runtag, #unique tag for these processes? (to manage bsub dependencies)
'unixcsv' => \$unixcsv, #are samples.csv and alignment_params.csv unix or windows?
'postprocess' => \$postprocess, #are samples.csv and alignment_params.csv unix or windows?
'debug' => \$debug); #debug mode
# Bring in the file and format, or die with a nice
# usage statement if one or both arguments are missing.
#my $usage = "perl testparse.pl [file] [format]\n";
#my $file = shift or die $usage;
#my $format = shift or die $usage;
open VCFFILE, $mutfile || die "can't open VCF file $mutfile\n";;
while ($vcfline = <VCFFILE>) {
next if ($vcfline =~ m/$\#/);
@linesplitarray = split(" ",$vcfline);
#print $vcfline."\n";
#print $mutpos."\n";
$mutpos = $linesplitarray[1];
$refbases{$mutpos} = $linesplitarray[3];
push @mutlocs, $mutpos;
}
close VCFFILE;
#print "<$refsource/genome.fasta";
open GENOME, "<$refsource/genome.fasta" || warn "can't open gemome";
$genometopline = <GENOME>;
$genometopline =~ m/\|.+\|.+\|(.+)\|/;
$gbkfile = $refsource."/".$1.".gb";
close GENOME;
print "Reading genbank file $gbkfile ....\n";
my $inseq = Bio::SeqIO->new(
-file => "<$gbkfile",
-format => 'GenBank',
);
$myCodonTable = Bio::Tools::CodonTable->new();
$myCodonTable->id(11);
print "Parsing genbank...\n";
my $seq = $inseq->next_seq;
for my $feat_object ($seq->get_SeqFeatures) {
my $primtag = $feat_object->primary_tag;
#print "primary tag: ", $primtag , "\n";
if ($feat_object->primary_tag eq "CDS") {
$startpos = $feat_object->start;
$endpos = $feat_object->end;
$mutdata{$_}{"strand"}=$feat_object->strand;
#if (any { ($startpos < $_ && $endpos>$_) } @mutlocs ) {
foreach (@mutlocs) {
if ($startpos < $_ && $endpos>$_) {
$mutdata{$_}{"strand"}=$feat_object->strand;
$mutdata{$_}{"start"}=$feat_object->start;
$mutdata{$_}{"end"}=$feat_object->end;
$mutdata{$_}{"promotor"}=0;
#print $feat_object->start,"...";
#print $feat_object->end," ";
#print (($feat_object->strand eq 1)?"F":"R")."\n";
for my $tag ($feat_object->get_all_tags) {
#print " tag: ", $tag, "\n";
for my $value ($feat_object->get_tag_values($tag)) {
#print " value: ", $value, "\n";
$mutdata{$_}{$tag}=$value;
}
}
my $sequence_string = $feat_object->seq()->seq;
$mutdata{$_}{"nt_sequence"}=$sequence_string;
$mutpos=(($feat_object->strand == 1)? ( $_ -$startpos):($endpos-$_)); #0-indexed position
$mutdata{$_}{"nt_pos"}=$mutpos+1;
$mutcodonstart = $mutpos-($mutpos % 3);
$mutdata{$_}{"aa_pos"}=int($mutcodonstart/3)+1;
#print $mutcodonstart."x y".length($sequence_string)."z a";
$mutcodon = substr($sequence_string,$mutcodonstart,3);
$mutdata{$_}{"codon"}=$mutcodon;
#print "$mutcodon ";
$altcodon = $mutcodon;
$altcodings = "";
foreach $base ("A","T","C","G") {
#print "($altcodon) ".($mutpos % 3)." ";
$z = substr $altcodon,($mutpos % 3), 1,$base;
$altcodings = $altcodings.($myCodonTable->translate($altcodon));
$altcodings =~ s/(.)(.)(.)(.)/$2$1$4$3/ if ($feat_object->strand == -1);
}
$mutdata{$_}{"AAs"}=$altcodings;
#
} else { #INTERGENIC
if ( ( (($startpos-$promoterlength) < $_) && (($_ < $startpos) && ($feat_object->strand==1) ) ) ||
( (($endpos+$promoterlength) > $_) && ( ($_ > $endpos) && ($feat_object->strand==-1) )) ){
$mutdata{$_}{"strand"}=$feat_object->strand;
$mutdata{$_}{"start"}=$feat_object->start;
$mutdata{$_}{"end"}=$feat_object->end;
$mutdata{$_}{"promotor"}=1;
for my $tag ($feat_object->get_all_tags) {
#print " tag: ", $tag, "\n";
for my $value ($feat_object->get_tag_values($tag)) {
#print " value: ", $value, "\n";
$mutdata{$_}{$tag}=$value;
}
}
}
}
}
}
}
open OUTFILE, ">$outfile";
foreach (@mutlocs) {
print OUTFILE $_."\t";
if ($mutdata{$_}{"gene"}) {
print OUTFILE $mutdata{$_}{"gene"};
} else {
print OUTFILE "yyyY";
}
print OUTFILE "\t";
print OUTFILE $mutdata{$_}{"start"}."\t";
print OUTFILE $mutdata{$_}{"end"}."\t";
print OUTFILE $mutdata{$_}{"strand"}."\t";
print OUTFILE $mutdata{$_}{"promotor"}."\t";
print OUTFILE $refbases{$_}."\t";
print OUTFILE $mutdata{$_}{"product"}."\t";
print OUTFILE $mutdata{$_}{"protein_id"}."\t";
print OUTFILE $mutdata{$_}{"nt_pos"}."\t";
print OUTFILE $mutdata{$_}{"codon"}."\t";
print OUTFILE $mutdata{$_}{"locus_tag"}."\t";
print OUTFILE $mutdata{$_}{"aa_pos"}."\t";
print OUTFILE $mutdata{$_}{"AAs"}."\t";
print OUTFILE $mutdata{$_}{"note"};
#print OUTFILE " ".$mutdata{$_}{"function"}."\n";
#print OUTFILE " ".$mutdata{$_}{"note"}."\n";
#print OUTFILE " ".$mutdata{$_}{"translation"}."\n";
print OUTFILE "\n";
}