-
Notifications
You must be signed in to change notification settings - Fork 0
/
embl2gff.pl
178 lines (133 loc) · 3.64 KB
/
embl2gff.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
#!/usr/bin/perl
use Getopt::Long;
use warnings;
GetOptions (\%opt,"embl:s","project:s","help");
my $help=<<USAGE;
EMBL to gff
perl $0 --embl test.embl --project moc1
USAGE
if ($opt{help} or keys %opt < 1){
print "$help\n";
exit();
}
# Declare and initialize variables
my $fh; # variable to store filehandle
my $record;
my $dna;
my $annotation;
my $offset;
my $library = $opt{embl};
# Perform some standard subroutines for test
$fh = open_file($library);
while( $record = get_next_record($fh) ) {
next if (length $record < 2);
($annotation, $dna) = get_annotation_and_dna($record);
my $gene=writegff($annotation,$dna);
#print "Annotation:\n$annotation\n";
#print "DNA:\n$dna\n";
}
exit;
################################################################################
# Subroutines
################################################################################
# open_file
#
# - given filename, set filehandle
sub writegff
{
my ($anno,$dna)=@_;
my $count=0;
open OUT, ">$opt{project}.gff" or die "$!";
while($anno=~/FT CDS (.*?)[\)\/]/gs){
my $pos=$1;
my $strand="+";
$count++;
my $name=$opt{project}.".".$count;
$pos=~s/[\n\s]//g;
$strand= "-" if ($pos=~/complement/);
$pos=~s/[a-z]//g;
$pos=~s/[\(\>]//g;
my @exon=split(",",$pos);
my @exonp;
foreach my $e (@exon){
my @unit;
if ($e=~/(\d+)\.\.(\d+)/){
$unit[0]=$1;
$unit[1]=$2;
}
push (@exonp,[$unit[0],$unit[1]]);
}
my $mrnas=$exonp[0][0];
my $mrnae=$exonp[$#exonp][1];
print OUT "$opt{project}\tEMBL\tmRNA\t$mrnas\t$mrnae\t\.\t$strand\t\.\tID=$name;\n";
foreach my $ep (@exonp){
print OUT "$opt{project}\tEMBL\tCDS\t$ep->[0]\t$ep->[1]\t\.\t$strand\t\.\tParent=$name;\n";
}
print "$strand\t$pos\n";
}
close OUT;
open OUT, ">$opt{project}.fasta" or die "$!";
print OUT ">$opt{project}\n$dna\n";
close OUT;
}
sub open_file {
my($filename) = @_;
my $fh;
unless(open($fh, $filename)) {
print "Cannot open file $filename\n";
exit;
}
return $fh;
}
# get_next_record
#
# - given GenBank record, get annotation and DNA
sub get_next_record {
my($fh) = @_;
my($offset);
my($record) = '';
my($save_input_separator) = $/;
$/ = "//\n";
$record = <$fh>;
$/ = $save_input_separator;
return $record;
}
# get_annotation_and_dna
#
# - given GenBank record, get annotation and DNA
sub get_annotation_and_dna {
my($record) = @_;
my($annotation) = '';
my($dna) = '';
# Now separate the annotation from the sequence data
#($annotation, $dna) = ($record =~ /^(LOCUS.*ORIGIN\s*\n)(.*)\/\/\n/s);
($annotation, $dna) = ($record =~ /^(FH\s+Key.*SQ\s+Sequence.*?BP;\s*\n)(.*)\/\/\n/s);
# clean the sequence of any whitespace or / characters
# (the / has to be written \/ in the character class, because
# / is a metacharacter, so it must be "escaped" with \)
$dna =~ s/[\s\/\d]//g;
return($annotation, $dna)
}
# search_sequence
#
# - search sequence with regular expression
sub search_sequence {
my($sequence, $regularexpression) = @_;
my(@locations) = ( );
while( $sequence =~ /$regularexpression/ig ) {
push( @locations, pos );
}
return (@locations);
}
# search_annotation
#
# - search annotation with regular expression
sub search_annotation {
my($annotation, $regularexpression) = @_;
my(@locations) = ( );
# note the /s modifier--. matches any character including newline
while( $annotation =~ /$regularexpression/isg ) {
push( @locations, pos );
}
return (@locations);
}