-
Notifications
You must be signed in to change notification settings - Fork 14
/
Copy pathGenomeProject.pm
490 lines (386 loc) · 11.7 KB
/
GenomeProject.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
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
#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::GenomeProject;
#GenomeProject contains all features that are associated with a single genome project,
#including details about the organism that was sequenced
#perldoc GenomeProject - for more information (or see end of this file)
use strict;
use warnings;
#inherit common methods and fields from the MicroDB class
use base ("MicrobeDB::MicrobeDB");
use Carp;
require MicrobeDB::Replicon;
require MicrobeDB::Search;
use Log::Log4perl qw(get_logger :nowarn);
my $logger = Log::Log4perl->get_logger();
my @FIELDS;
my @_db_fields;
my %_field_hash;
my @_tables;
my @genomeproject;
my @taxonomy;
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.
#The array names must be kept the same as the database names (otherwise alter field_names())
@genomeproject = qw(
gpv_id
gp_id
version_id
taxon_id
org_name
gram_stain
genome_gc
patho_status
disease
genome_size
chromosome_num
contig_num
plasmid_num
pathogenic_in
temp_range
habitat
shape
arrangement
endospore
motility
salinity
oxygen_req
release_date
centre
gpv_directory
);
@taxonomy = qw(
taxon_id
superkingdom
phylum
class
order
family
genus
species
other
synonyms
);
@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 = ( @genomeproject, @taxonomy, @version );
my %temp;
@temp{@_db_fields} = ();
@_db_fields = keys %temp;
#store the db tablenames that are used in this object
@_tables = qw(
genomeproject
taxonomy
version
);
#fields not directly related to the database
my @_other = qw(
replicons
rep_index
);
$_field_hash{genomeproject} = \@genomeproject;
$_field_hash{taxonomy} = \@taxonomy;
$_field_hash{version} = \@version;
@FIELDS = ( @_db_fields, @_other );
}
use fields @FIELDS;
sub new {
my ( $class, %arg ) = @_;
#bless and restrict the object
my $self = fields::new($class);
#set the replicon index to the first of the array
$self->rep_index(0);
#Set each attribute that is given as an arguement (and handle special cases)
foreach my $attr ( keys(%arg) ) {
#Need special case when setting the replicons attribute
#(note: we want to store this as a reference of an array of Replicon objects but,
#this check will handle if a plain hash is given (ie. the replicon hashes are not blessed as Replicons))
if ( $attr eq 'replicons' ) {
#check each of the replicons to see if it is an actuall Replicon object
for ( my $i = 0 ; $i < scalar( @{ $arg{$attr} } ) ; $i++ ) {
unless ( ref( $arg{$attr}->[$i] ) eq 'MicrobeDB::Replicon' ) {
#overwrite the normal hash reference with the new Replicon object reference
$arg{$attr}->[$i] = new MicrobeDB::Replicon( %{ $arg{$attr}->[$i] } );
}
}
} elsif ($attr eq 'genome_size') {
# We have to test genome_size and genome_gc to see if they're
# null, otherwise when it tries to retreive the replicons
# to calculate it and gpv_id isn't yet set bad things happen.
# (infinite loop)
next unless$arg{$attr};
} elsif ($attr eq 'genome_gc') {
next unless$arg{$attr};
}
#do the same for references
# if ( $attr eq 'references' ) {
#check each of the references to see if it is an actual Reference object
# for ( my $i = 0 ; $i < scalar( @{ $arg{$attr} } ) ; $i++ ) {
# unless ( ref( $arg{$attr}->[$i] ) eq 'Reference' ) {
#overwrite the normal hash reference with the new Replicon object reference
# $arg{$attr}->[$i] = new Reference( %{ $arg{$attr}->[$i] } );
# }
#}
#}
#set the attribute in the object
$self->$attr( $arg{$attr} );
}
return $self;
}
#adds a replicon object (or a hash that can be converted to a replicon object) to the array of replicons for this genome project
sub add_replicon {
my ( $self, $rep ) = @_;
my $rep_obj;
if ( ref($rep) eq 'MicrobeDB::Replicon' ) {
$rep_obj = $rep;
} elsif ( ref($rep) eq 'HASH' ) {
$rep_obj = new MicrobeDB::Replicon(%$rep);
} else {
$logger->logcroak("Only a MicrobeDB::Replicon object or hash can be used to add a Replicon");
}
push( @{ $self->{replicons} }, $rep_obj );
}
#retrieves the next replicon for this genome project
sub next_replicon {
my ($self) = @_;
#get the replicon index
my $rep_index = $self->rep_index();
#get the array of all replicon objects
my @replicons = @{ $self->replicons() };
#return the replicon object if the rep index is still within bounds
my $ret_rep;
if ( $rep_index < scalar(@replicons) ) {
$ret_rep = $replicons[$rep_index];
$self->rep_index( ++$rep_index );
}
return $ret_rep;
}
#retrieves all replicons for this genome project
sub _retrieve_replicons{
my ($self) =@_;
# In case a genome didn't load properly and we don't
# get back a proper object, we DON'T want to search
# with no gpv_id, ugh, not good.
return () unless( $self->{gpv_id});
my $rep = new MicrobeDB::Replicon(gpv_id => $self->gpv_id());
my $so = new MicrobeDB::Search();
my @reps = $so->object_search($rep);
return \@reps;
}
#returns, sets, and finds replicons associated with this genome project
sub replicons{
my ($self,$new_reps) = @_;
#Set the new value for the attribute if available (stored as a reference to an array)
$self->{replicons} = $new_reps if defined($new_reps);
unless(defined($self->{replicons})){
$self->replicons($self->_retrieve_replicons());
}
#return the current content
return $self->{replicons};
}
sub delete{
my($self)=@_;
my $dbh=$self->_db_connect();
my $gpv_id=$self->gpv_id();
if(defined($gpv_id)){
#list of tables to delete records from
my @tables_to_delete = qw/genomeproject replicon gene/;
#delete records corresponding to gpv_id (use QUICK since there are millions of genes)
foreach my $curr_table (@tables_to_delete) {
$dbh->do("DELETE QUICK FROM $curr_table WHERE gpv_id = $gpv_id ");
}
}
}
sub plasmid_num{
my ($self,$value)=@_;
$self->{plasmid_num} = $value if defined($value);
unless(defined($self->{plasmid_num})){
$self->plasmid_num($self->_count_plasmid_num());
}
return $self->{plasmid_num};
}
sub _count_plasmid_num{
my ($self)=@_;
my $plasmid_num=0;
foreach my $rep (@{$self->replicons()}){
if($rep->rep_type() eq 'plasmid'){
$plasmid_num++;
}
}
return $plasmid_num;
}
sub contig_num{
my ($self,$value)=@_;
$self->{contig_num} = $value if defined($value);
unless(defined($self->{contig_num})){
$self->contig_num($self->_count_contig_num());
}
return $self->{contig_num};
}
sub _count_contig_num{
my ($self)=@_;
my $contig_num=0;
foreach my $rep (@{$self->replicons()}){
if($rep->rep_type() eq 'contig'){
$contig_num++;
}
}
return $contig_num;
}
sub chromosome_num{
my ($self,$value)=@_;
$self->{chromosome_num} = $value if defined($value);
unless(defined($self->{chromosome_num})){
$self->chromosome_num($self->_count_chromosome_num());
}
return $self->{chromosome_num};
}
sub _count_chromosome_num{
my ($self)=@_;
my $chromosome_num=0;
foreach my $rep (@{$self->replicons()}){
if($rep->rep_type() eq 'chromosome'){
$chromosome_num++;
}
}
return $chromosome_num;
}
#returns, sets, and finds genome size associated with this genome project
sub genome_size{
my ($self,$genome_size)=@_;
$self->{genome_size} = $genome_size if defined($genome_size);
unless(defined($self->{genome_size})){
$self->genome_size($self->_calc_genome_size());
}
return $self->{genome_size};
}
#calculates the genome size by adding up all the rep sizes
sub _calc_genome_size{
my ($self)=@_;
my $genome_size;
foreach my $rep (@{$self->replicons()}){
$genome_size+=$rep->rep_size();
}
#format the size to be in Mb
$genome_size = sprintf("%.2f",$genome_size/1000000);
return $genome_size;
}
#returns, sets, and finds genome gc associated with this genome project
sub genome_gc{
my ($self,$genome_gc)=@_;
$self->{genome_gc} = $genome_gc if defined($genome_gc);
unless(defined($self->{genome_gc})){
$self->genome_gc($self->_calc_genome_gc());
}
return $self->{genome_gc};
}
#calculates the genome size by adding up all the rep sizes
sub _calc_genome_gc{
my ($self)=@_;
my $genome_gc;
my $g_count=0;
my $c_count=0;
my $genome_size=0;
foreach my $rep (@{$self->replicons()}){
if(defined($rep->rep_seq())){
$g_count += ($rep->{rep_seq} =~ tr/g//);
$g_count += ($rep->{rep_seq} =~ tr/G//);
$c_count += ($rep->{rep_seq} =~ tr/c//);
$c_count += ($rep->{rep_seq} =~ tr/C//);
$genome_size += $rep->rep_size();
}
}
$genome_gc = ($g_count+$c_count)/($genome_size);
#format the number
$genome_gc = sprintf("%.2f",$genome_gc*100);
return $genome_gc;
}
#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;
}
#special case
if($table_name eq 'db'){return @_db_fields}
return @{$_field_hash{$table_name}};
#if($table_name eq 'genomeproject'){ return @genomeproject}
#if($table_name eq 'version'){return @version}
#if($table_name eq 'taxonomy'){return @taxonomy}
#return;
}
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;
}
# 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->{replicons}) {
for my $replicon (@{ $self->{replicons}}) {
$replicon->cleanup();
}
}
undef $self->{replicons};
}
# 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 DESTROY {
my $self = shift;
return if ${^GLOBAL_PHASE} eq 'DESTRUCT';
$self->cleanup();
}
1;
__END__
=head1 NAME
GenomeProject: contains all features that are associated with a single genome project, including details about the organism that was sequenced
=head1 Synopsis
=head1 AUTHOR
Morgan Langille
=head1 Date Created
June 5th, 2006
=cut