forked from chasewnelson/SNPGenie
-
Notifications
You must be signed in to change notification settings - Fork 0
/
snpgenie_gbk2gtf.pl
executable file
·511 lines (463 loc) · 14 KB
/
snpgenie_gbk2gtf.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
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
#! /usr/bin/perl
# Takes a Genbank file as its argument, and creates a GTF file for its products.
use strict;
#use warnings;
use IO::Handle;
my $genbank_file_name = $ARGV[0];
# Generate new file name prefix
my $new_file_prefix;
if($genbank_file_name =~/\.gbk/) {
$new_file_prefix = $`;
} else {
$new_file_prefix = "inFile";
}
my $new_file_name = $new_file_prefix . ".gtf";
my $curr_ORF = '';
my $curr_start = '';
my $curr_stop = '';
my $curr_start2 = '';
my $curr_stop2 = '';
my %ORF_info_hh;
my @ORF_arr;
my $seen_label = 0;
open(GBK_FILE, $genbank_file_name) or die "Could not open file $genbank_file_name\n";
while(<GBK_FILE>) {
#if($_ =~ /^\s*ORF\s+(\d+)\.\.(\d+)/) {
if($_ =~ /^\s*CDS\s+(\d+)\.\.(\d+)/) {
$curr_start = $1;
$curr_stop = $2;
$seen_label = 0;
#} elsif($_ =~ /^\s*ORF\s+<(\d+)\.\.(\d+)/) {
} elsif($_ =~ /^\s*CDS\s+<(\d+)\.\.(\d+)/) {
$curr_start = $1;
$curr_stop = $2;
$seen_label = 0;
#} elsif($_ =~ /^\s*ORF\s+join\((\d+)\.\.(\d+),(\d+)\.\.(\d+)\)/) {
} elsif($_ =~ /^\s*CDS\s+join\((\d+)\.\.(\d+),(\d+)\.\.(\d+)\)/) {
$curr_start = $1;
$curr_stop = $2;
$curr_start2 = $3;
$curr_stop2 = $4;
$seen_label = 0;
}
if($_ =~ /^\s*ORF\s+(\d+)\.\.(\d+)/) {
#if($_ =~ /^\s*CDS\s+(\d+)\.\.(\d+)/) {
$curr_start = $1;
$curr_stop = $2;
$seen_label = 0;
} elsif($_ =~ /^\s*ORF\s+<(\d+)\.\.(\d+)/) {
#} elsif($_ =~ /^\s*CDS\s+<(\d+)\.\.(\d+)/) {
$curr_start = $1;
$curr_stop = $2;
$seen_label = 0;
} elsif($_ =~ /^\s*ORF\s+join\((\d+)\.\.(\d+),(\d+)\.\.(\d+)\)/) {
#} elsif($_ =~ /^\s*CDS\s+join\((\d+)\.\.(\d+),(\d+)\.\.(\d+)\)/) {
$curr_start = $1;
$curr_stop = $2;
$curr_start2 = $3;
$curr_stop2 = $4;
$seen_label = 0;
}
if($_ =~ /^\s*misc_feature\s+(\d+)\.\.(\d+)/) {
$curr_start = $1;
$curr_stop = $2;
$seen_label = 0;
} elsif($_ =~ /^\s*misc_feature\s+<(\d+)\.\.(\d+)/) {
$curr_start = $1;
$curr_stop = $2;
$seen_label = 0;
} elsif($_ =~ /^\s*misc_feature\s+join\((\d+)\.\.(\d+),(\d+)\.\.(\d+)\)/) {
$curr_start = $1;
$curr_stop = $2;
$curr_start2 = $3;
$curr_stop2 = $4;
$seen_label = 0;
}
#if($_ =~ /\s*\/label=(\w+)/) { # This was for the SIV and SHFVkrc1 and SHFVkrc2
if($seen_label == 0) {
if($_ =~ /\s*\/label="([\w\s']+ [\w\s']+)"/) { # try to for all non-whitespace names
if($curr_start2 eq '') {
$curr_ORF = $1;
push(@ORF_arr,$curr_ORF);
$ORF_info_hh{$curr_ORF}->{start} = $curr_start;
$ORF_info_hh{$curr_ORF}->{stop} = $curr_stop;
$seen_label = 1;
} else {
$curr_ORF = $1;
push(@ORF_arr,$curr_ORF);
$ORF_info_hh{$curr_ORF}->{start} = $curr_start;
$ORF_info_hh{$curr_ORF}->{stop} = $curr_stop;
$ORF_info_hh{$curr_ORF}->{start2} = $curr_start2;
$ORF_info_hh{$curr_ORF}->{stop2} = $curr_stop2;
$curr_ORF = '';
$curr_start = '';
$curr_stop = '';
$curr_start2 = '';
$curr_stop2 = '';
$seen_label = 1;
}
} elsif($_ =~ /\s*\/label="([\w\s']+)"/) { # JUST ADDED THIS FOR MANESS DATA
if($curr_start2 eq '') {
$curr_ORF = $1;
push(@ORF_arr,$curr_ORF);
$ORF_info_hh{$curr_ORF}->{start} = $curr_start;
$ORF_info_hh{$curr_ORF}->{stop} = $curr_stop;
$seen_label = 1;
} else {
$curr_ORF = $1;
push(@ORF_arr,$curr_ORF);
$ORF_info_hh{$curr_ORF}->{start} = $curr_start;
$ORF_info_hh{$curr_ORF}->{stop} = $curr_stop;
$ORF_info_hh{$curr_ORF}->{start2} = $curr_start2;
$ORF_info_hh{$curr_ORF}->{stop2} = $curr_stop2;
$curr_ORF = '';
$curr_start = '';
$curr_stop = '';
$curr_start2 = '';
$curr_stop2 = '';
$seen_label = 1;
}
} elsif($_ =~ /\s*\/label=([\w\s']+)/) {
if($curr_start2 eq '') {
$curr_ORF = $1;
#print "Here is \$curr_ORF: $curr_ORF";
chomp($curr_ORF);
push(@ORF_arr,$curr_ORF);
$ORF_info_hh{$curr_ORF}->{start} = $curr_start;
$ORF_info_hh{$curr_ORF}->{stop} = $curr_stop;
$seen_label = 1;
} else {
$curr_ORF = $1;
#print "Here is \$curr_ORF: $curr_ORF";
chomp($curr_ORF);
push(@ORF_arr,$curr_ORF);
$ORF_info_hh{$curr_ORF}->{start} = $curr_start;
$ORF_info_hh{$curr_ORF}->{stop} = $curr_stop;
$ORF_info_hh{$curr_ORF}->{start2} = $curr_start2;
$ORF_info_hh{$curr_ORF}->{stop2} = $curr_stop2;
$curr_ORF = '';
$curr_start = '';
$curr_stop = '';
$curr_start2 = '';
$curr_stop2 = '';
$seen_label = 1;
}
} elsif($_ =~ /\s*\/product="([\w\s']+ [\w\s']+)"/) { # try to for all non-whitespace names
if($curr_start2 eq '') {
$curr_ORF = $1;
push(@ORF_arr,$curr_ORF);
$ORF_info_hh{$curr_ORF}->{start} = $curr_start;
$ORF_info_hh{$curr_ORF}->{stop} = $curr_stop;
$seen_label = 1;
} else {
$curr_ORF = $1;
push(@ORF_arr,$curr_ORF);
$ORF_info_hh{$curr_ORF}->{start} = $curr_start;
$ORF_info_hh{$curr_ORF}->{stop} = $curr_stop;
$ORF_info_hh{$curr_ORF}->{start2} = $curr_start2;
$ORF_info_hh{$curr_ORF}->{stop2} = $curr_stop2;
$curr_ORF = '';
$curr_start = '';
$curr_stop = '';
$curr_start2 = '';
$curr_stop2 = '';
$seen_label = 1;
}
} elsif($_ =~ /\s*\/product="([\w\s']+)"/) {
if($curr_start2 eq '') {
$curr_ORF = $1;
push(@ORF_arr,$curr_ORF);
$ORF_info_hh{$curr_ORF}->{start} = $curr_start;
$ORF_info_hh{$curr_ORF}->{stop} = $curr_stop;
$seen_label = 1;
} else {
$curr_ORF = $1;
push(@ORF_arr,$curr_ORF);
$ORF_info_hh{$curr_ORF}->{start} = $curr_start;
$ORF_info_hh{$curr_ORF}->{stop} = $curr_stop;
$ORF_info_hh{$curr_ORF}->{start2} = $curr_start2;
$ORF_info_hh{$curr_ORF}->{stop2} = $curr_stop2;
$curr_ORF = '';
$curr_start = '';
$curr_stop = '';
$curr_start2 = '';
$curr_stop2 = '';
$seen_label = 1;
}
} elsif($_ =~ /\s*\/product=([\w\s']+)/) {
if($curr_start2 eq '') {
$curr_ORF = $1;
#print "Here is \$curr_ORF: $curr_ORF";
chomp($curr_ORF);
push(@ORF_arr,$curr_ORF);
$ORF_info_hh{$curr_ORF}->{start} = $curr_start;
$ORF_info_hh{$curr_ORF}->{stop} = $curr_stop;
$seen_label = 1;
} else {
$curr_ORF = $1;
#print "Here is \$curr_ORF: $curr_ORF";
chomp($curr_ORF);
push(@ORF_arr,$curr_ORF);
$ORF_info_hh{$curr_ORF}->{start} = $curr_start;
$ORF_info_hh{$curr_ORF}->{stop} = $curr_stop;
$ORF_info_hh{$curr_ORF}->{start2} = $curr_start2;
$ORF_info_hh{$curr_ORF}->{stop2} = $curr_stop2;
$curr_ORF = '';
$curr_start = '';
$curr_stop = '';
$curr_start2 = '';
$curr_stop2 = '';
$seen_label = 1;
}
}
}
}
close GBK_FILE;
my @ORF_arr_sorted = sort (@ORF_arr);
open(OUTFILE,">>$new_file_name");
foreach my $curr_product (@ORF_arr_sorted) {
#print "$curr_product\n";
my $this_start = $ORF_info_hh{$curr_product}->{start};
my $this_stop = $ORF_info_hh{$curr_product}->{stop};
print OUTFILE "$genbank_file_name\tCLC\tCDS\t$this_start\t$this_stop\t\.\t\+\t0\tgene_id \"$curr_product\";\n";
if (exists $ORF_info_hh{$curr_product}->{start2}) {
my $this_start2 = $ORF_info_hh{$curr_product}->{start2};
my $this_stop2 = $ORF_info_hh{$curr_product}->{stop2};
print OUTFILE "$genbank_file_name\tCLC\tCDS\t$this_start2\t$this_stop2\t\.\t\+\t0\tgene_id \"$curr_product\";\n";
}
}
close OUTFILE;
#print "\n\nProducts are:";
#foreach (@ORF_arr) {
# print "\n\n" . $_ . "\nStart: " . $ORF_info_hh{$_}->{start} . "\nStop: " . $ORF_info_hh{$_}->{stop};
# if (exists $ORF_info_hh{$_}->{start2}) {
# print "\nStart2: " . $ORF_info_hh{$_}->{start2} . "\nStop2: " . $ORF_info_hh{$_}->{stop2};
# }
#}
# open (INFILE, $curr_snp_report_name);
# while (<INFILE>) {
# if($line == 0) {
# $line++;
# } else {
# chomp;
#
# my @line_arr = split(/\t/,$_,-1);
# my $type = $line_arr[$index_type];
# my $min = $line_arr[$index_min];
# my $max = $line_arr[$index_max];
# my $product_name = $line_arr[$index_product];
#
# if($line_arr[$index_min] =~ /\<(\d+)/) {
# $min = $1;
# }
#
# # Store the CDS coordinates by the product name
# if($type eq 'CDS' && $product_name ne '') { # &
# $gtf_info_hh{$product_name}->{start} = $min;
# $gtf_info_hh{$product_name}->{stop} = $max;
# }
#
# }
# }
# close INFILE;
#if($line =~ /^\/\/\n/) { # If $line is the end of record, //\n
# last;
#}
#STDOUT->autoflush(1);
#
#my @snp_report_file_names_arr = &get_snp_report_file_names;
#
#foreach my $curr_snp_report_name (@snp_report_file_names_arr) {
# #print "The current report is: $curr_snp_report_name\n";
#
# # Generate new file name prefix
# my $new_file_prefix;
# if($curr_snp_report_name =~/\.txt/) {
# $new_file_prefix = $`;
# } else {
# $new_file_prefix = "inFile";
# }
#
# my @header_names_arr = &get_header_names($curr_snp_report_name);
#
# my $index_min;
# my $index_max;
# my $index_type;
# my $index_poly_type;
# my $index_cds_position;
# my $index_change;
# my $index_percent;
# my $index_cov;
# my $index_product;
#
# # Determine the index of each column
# for (my $i=0; $i<scalar(@header_names_arr); $i++) {
# if ($header_names_arr[$i] eq 'Minimum') { # MUST MAKE SURE MIN = MAX
# $index_min = $i;
# } elsif ($header_names_arr[$i] eq 'Maximum') { # Will check the value BEGINS with 'SNP'
# $index_max = $i;
# } elsif ($header_names_arr[$i] eq 'Type') { # Will check the value BEGINS with 'SNP'
# $index_type = $i;
# } elsif ($header_names_arr[$i] eq 'Polymorphism Type') { # Will check the value BEGINS with 'SNP'
# $index_poly_type = $i;
# } elsif ($header_names_arr[$i] eq 'CDS Position') {
# $index_cds_position = $i;
# } elsif ($header_names_arr[$i] eq 'Change') {
# $index_change = $i;
# } elsif ($header_names_arr[$i] eq 'Variant Frequency') {
# $index_percent = $i;
# } elsif ($header_names_arr[$i] eq 'Coverage') {
# $index_cov = $i;
# } elsif ($header_names_arr[$i] eq 'product') {
# $index_product = $i;
# }
# }
#
# my @product_names_arr = &get_product_names_geneious($curr_snp_report_name,$index_product,$index_type);
# # N.B.: product names comes from both type eq "CDS" and type eq "Polymorphism", so
# # we may need to add a check and print a warning if a product is known but does not
# # have any CDS information listed
#
# my %gtf_info_hh;
#
# my $line = 0;
# open (INFILE, $curr_snp_report_name);
# while (<INFILE>) {
# if($line == 0) {
# $line++;
# } else {
# chomp;
#
# my @line_arr = split(/\t/,$_,-1);
# my $type = $line_arr[$index_type];
# my $min = $line_arr[$index_min];
# my $max = $line_arr[$index_max];
# my $product_name = $line_arr[$index_product];
#
# if($line_arr[$index_min] =~ /\<(\d+)/) {
# $min = $1;
# }
#
# # Store the CDS coordinates by the product name
# if($type eq 'CDS' && $product_name ne '') { # &
# $gtf_info_hh{$product_name}->{start} = $min;
# $gtf_info_hh{$product_name}->{stop} = $max;
# }
#
# }
# }
# close INFILE;
#
# my $out_gtf_file_name;
#
# print "$curr_snp_report_name\n";
#
# if($curr_snp_report_name =~/\.txt/) {
# $out_gtf_file_name = $` . '.gtf'; # $` is for the string BEFORE the match
# } else {
# $out_gtf_file_name = 'inFile.gtf';
# }
#
# print "$out_gtf_file_name\n";
#
# open(OUTFILE,">>$out_gtf_file_name");
#
# my @current_products_arr = sort (keys %gtf_info_hh);
#
# foreach my $curr_product (@current_products_arr) {
# print "$curr_product\n";
#
# my $this_min = $gtf_info_hh{$curr_product}->{start};
# my $this_max = $gtf_info_hh{$curr_product}->{stop};
# print OUTFILE "$curr_snp_report_name\tGeneious\tCDS\t$this_min\t$this_max\t\.\t\+\t0\tgene_id \"$curr_product\";\n";
# }
# close OUTFILE;
#
# print "\n";
## PRINT, tab-delimited
## REPORT NAME
## CLC OR GENEIOUS
## CDS
## MIN
## MAX
## .
## +
## 0
## gene_id "";
#
#}
#
#
##########################################################################################
##########################################################################################
####################################### SUBROUTINES ######################################
##########################################################################################
##########################################################################################
#
#
##########################################################################################
## Obtains all file names in current directory ending in .txt
#sub get_snp_report_file_names {
# my @snp_report_file_names = glob "*.txt";
# if (scalar (@snp_report_file_names) == 0) {
# die "\n\n## WARNING:\n## There are no .txt SNP Reports.\n\n";
# }
# return @snp_report_file_names;
#}
#
##########################################################################################
#sub get_header_names {
# my ($curr_snp_report_filename) = @_;
# #print "\n$curr_snp_report_filename\n";
# my $line = 0;
# open (CURRINFILE, $curr_snp_report_filename);
# while (<CURRINFILE>) {
# if($line == 0) {
# if(!($_ =~/\t/)) {
# chdir('SNPGenie_Results');
# open(ERROR_FILE,">>SNPGenie\_WARNINGS\.txt");
# # FILE | PRODUCT | SITE | CODON | WARNING
# print ERROR_FILE "$curr_snp_report_filename\tN/A\tN/A\t".
# "File not TAB-delimited (\\t) or there is only one column. SNPGenie terminated\n";
# close ERROR_FILE;
# chdir('..');
#
# die "\n\n## WARNING:\n## The SNP Report $curr_snp_report_filename is ".
# "not TAB-delimited (\\t), or there is only one column.\n\n";
# }
# chomp;
# $line++;
# my @line_arr = split(/\t/,$_);
# return @line_arr;
# }
# }
# close CURRINFILE;
#}
#
##########################################################################################
#sub get_product_names_geneious {
# my ($curr_snp_report_filename,$index_product,$index_type) = @_;
# my $line = 0;
# my %products_hash;
# open (CURRINFILE, $curr_snp_report_filename);
# while (<CURRINFILE>) {
# if ($line == 0) {
# $line++;
# } else {
# chomp;
# my @line_arr = split(/\t/,$_);
# my $product = $line_arr[$index_product];
# my $type = $line_arr[$index_type];
#
# if($product ne '') {
# if ( ! exists $products_hash{$product} &&
# (($type eq 'Polymorphism') || ($type eq 'CDS')) ) {
# $products_hash{$product} = 1;
# }
# }
# }
# }
# close CURRINFILE;
# my @product_names = keys %products_hash;
# return @product_names;
#}