-
Notifications
You must be signed in to change notification settings - Fork 14
/
Gene.pm
287 lines (214 loc) · 6.25 KB
/
Gene.pm
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
#Copyright (C) 2011 Morgan G.I. Langille
#Author contact: [email protected]
#This file is part of MicrobeDB.
#MicrobeDB 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.
#MicrobeDB 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 MicrobeDB. If not, see <http://www.gnu.org/licenses/>.
package MicrobeDB::Gene;
#Gene contains features that are associated with a single gene.
#perldoc Gene - for more information (or see end of this file)
#inherit common methods and fields from the MicroDB class
use base ("MicrobeDB::MicrobeDB");
use strict;
use warnings;
use Carp;
use Bio::SeqIO;
use Bio::Perl;
my @FIELDS;
my @gene;
my @_db_fields;
my @_tables;
my %_field_hash;
my @version;
BEGIN {
#All fields in the following arrays correspond to the fields in the database
#Each array represents one table in the database
#Duplicate field names *should* all represent the same piece of data (usually just a foreign key);
#therefore, only a single copy for that field will be stored in the object and all others will be clobbered.
@gene = qw(
gene_id
rpv_id
version_id
gpv_id
gid
pid
protein_accnum
gene_type
gene_start
gene_end
gene_length
gene_strand
gene_name
locus_tag
gene_product
);
@version = qw(
version_id
dl_directory
version_date
used_by
);
#puts all fields relavant to the database in a single array and removes duplicates
@_db_fields = (@gene, @version);
my %temp;
@temp{@_db_fields} =();
@_db_fields = keys %temp;
#store the db tablenames that are used in this object
@_tables = qw(
gene
version
);
$_field_hash{gene} = \@gene;
$_field_hash{version} = \@version;
my @_other = qw(
replicon
genomeproject
seqobj
gene_seq
protein_seq
);
@FIELDS = ( @_db_fields, @_other );
}
use fields @FIELDS;
sub new {
my ( $class, %arg ) = @_;
#bless and restrict the object
my $self = fields::new($class);
#Set each attribute that is given as an arguement
foreach ( keys(%arg) ) {
# Skip the type of gene_seq & protein_seq, we never store these
next if($_ eq 'gene_seq');
next if($_ eq 'protein_seq');
$self->$_( $arg{$_} );
}
return $self;
}
#returns an array of fields
#all fields are returned if a table name is not given
sub field_names{
my ($self, $table_name)=@_;
unless(defined($table_name)){
return @FIELDS
}
if($table_name eq 'gene'){ return @gene}
#special case
if($table_name eq 'db'){return @_db_fields}
return;
}
#returns the genomeproject associated with this gene
sub genomeproject{
my ($self, $new_genomeproject) =@_;
$self->{genomeproject} = $new_genomeproject if defined($new_genomeproject);
if(defined($self->{genomeproject})) {
return $self->{genomeproject};
} else {
my $so = new MicrobeDB::Search();
my ($genomeproject) = $so->object_search( new MicrobeDB::GenomeProject(gpv_id => $self->gpv_id()));
$self->{genomeproject} = $genomeproject;
return $genomeproject;
}
}
#returns the replicon associated with this gene
sub replicon{
my ($self, $new_replicon) =@_;
$self->{replicon} = $new_replicon if defined($new_replicon);
if(defined($self->{replicon})) {
return $self->{replicon};
} else {
my $so = new MicrobeDB::Search();
my ($replicon) = $so->object_search( new MicrobeDB::Replicon(rpv_id => $self->rpv_id()));
$self->{replicon} = $replicon;
return $replicon;
}
}
# Override to pull the sequences from the flat
# files ratherthan the database
sub gene_seq() {
my ($self) = @_;
if(! defined($self->{seqobj})) {
my $replicon = $self->replicon();
my $filename = $replicon->get_filename('fna');
if(!defined($filename)) {
$filename = $replicon->get_filename('gbk');
}
if(!defined($filename)) {
croak "Error, fna or gbk file needed for retrieving sequences";
}
# Grab the fna file via bioperl
my $in = new Bio::SeqIO(-file => $filename);
my $seqobj = $in->next_seq();
return (($self->gene_strand() eq '-1' || $self->gene_strand() eq '-') ?
reverse_complement_as_string($seqobj->subseq($self->gene_start(), $self->gene_end())) :
$seqobj->subseq($self->gene_start(), $self->gene_end()));
# Holding all the seqobj in memory was taking to much memory
# and crashing the scripts...
# Iterates through each genomic dna sequence in the file,
# returns a BioSeq object representing it
# $self->{seqobj} = $in->next_seq();
} else {
return (($self->gene_strand() eq '-1' || $self->gene_strand() eq '-') ?
reverse_complement_as_string($self->{seqobj}->subseq($self->gene_start(), $self->gene_end())) :
$self->{seqobj}->subseq($self->gene_start(), $self->gene_end()));
}
return "ABCDF";
}
sub protein_seq() {
my ($self) = @_;
unless($self->gene_type() eq 'CDS') {
return '';
}
return Bio::Seq->new(-seq => $self->gene_seq(),
-alphabet => 'dna')
->translate(-codontable_id => 11, -complete => 1)
->seq();
}
# If we're cycling through a large number of genomes, we need
# to clean up after ourselves since there are circular references
# between the Replicon and Gene objects
sub cleanup() {
my ($self) = @_;
if($self->{replicon}) {
undef $self->{replicon};
}
}
sub table_names {
my ( $self, $field_name ) = @_;
#return all table names if no field name is given
unless ( defined($field_name) ) {
return @_tables;
}
my $table_name;
#look up what table the field name is in
foreach my $curr_table ( keys(%_field_hash) ) {
#stop looking if we already found it
unless ( defined($table_name) ) {
#look at each field name in the current table
foreach ( @{ $_field_hash{$curr_table} } ) {
if ( $field_name eq $_ ) {
#set the found table name
$table_name = $curr_table;
#exit the inner loop
last;
}
}
}
}
return $table_name;
}
1;
__END__
=head1 NAME
Gene: contains features that are associated with a single gene.
=head1 Synopsis
=head1 AUTHOR
Morgan Langille
=head1 Date Created
June 5th, 2006
=cut