-
Notifications
You must be signed in to change notification settings - Fork 11
/
Copy pathgbk2fna.txt
executable file
·106 lines (90 loc) · 2.16 KB
/
gbk2fna.txt
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
#!/usr/bin/perl
print "\n**********************************************************";
print "\n* gbk2fna.pl , CopyRight @ cail, Fudan University. *";
print "\n* Convert a file in current dir please enter 1, *";
print "\n* Convert all files in current dir please enter 2. *";
print "\n**********************************************************";
print "\nPlease enter your choice (1 or 2) : ";
my $choice=<STDIN>;
print "\nGOOD LUCK!\n\n";
chop($choice);
if($choice==1){
print "Please input the ACCESSION ID of the files : ";
my $input=<STDIN>;
chop($input);
my $filename=$input.".gbk";
my $output='';
$output=$input.".fna";
gbk2fna($filename,$output);
}
elsif($choice==2){
my $dir = ".";
opendir(DIR0, $dir) or die "Can't open $dir: $!\n";
while (defined(my $filename = readdir(DIR0))) {
next unless $filename =~ /\.gbk$/;
my $output=substr($filename,0,9).".fna";
print "$filename -> $output\n";
gbk2fna($filename,$output);
}
}
else{
print "The program does not support this request. SORRY.";
}
exit;
sub gbk2fna{
my ($data,$output)=@_;
my @GenBankFile = ( );
my $o=''; #organism
my $d=''; #total sequence
my $d1=''; #version+accession
my $d4=''; #nucleic acid sequence
my $d4t=''; #temp of d4
my $true=0;
@GenBankFile=get_file_data($data);
foreach my $line (@GenBankFile) {
if($line=~/^\/\/\n/){
last;
}
elsif($line=~/^DEFINITION/){
$line=~s/^DEFINITION (.+)\.\n$/$1/g;
$o=$line;
}
elsif($line=~/^VERSION/){
$line=~s/^VERSION\s+(.+)\s+GI:(.+)\n$/$2|man|$1/g;
$d1=$line;
chop($d1);
}
elsif($line=~/^ORIGIN/){
$true=1;
}
elsif($true==1){
$d4t.=$line;
}
}
$d4t=~s/[\s0-9]//g;
$d4=uc($d4t);
$d=">cl|".$d1."|".$o."\n";
my $i=0;
while($i<length($d4)) {
$d.=substr($d4,$i,70)."\n";
$i+=70;
}
open(FNA,">$output");
print FNA $d;
close FNA;
return 1;
}
sub get_file_data{
my ($file)=@_;
use strict;
use warnings;
# Initialize variables
my @filedata=();
unless(open(GET_FILE_DATA,$file)) {
print STDERR "Cannot open file \"$file\"\n\n";
exit;
}
@filedata=<GET_FILE_DATA>;
close GET_FILE_DATA;
return @filedata;
}