forked from mlangill/MicrobeDB
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathVersions.pm
198 lines (144 loc) · 5.32 KB
/
Versions.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
#Copyright (C) 2011 Morgan G.I. Langille
#Author contact: morgan.g.i.langille@gmail.com
#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::Versions;
# Version contains search functionality for available versions of MicrobeDB
# along with routines to find replicon differences between versions.
use strict;
use warnings;
#inherit common methods and fields from the MicroDB class
use base ("MicrobeDB::MicrobeDB");
use MicrobeDB::Search;
use MicrobeDB::Version;
use Carp;
sub new {
my ( $class, %arg ) = @_;
#Bless an anonymous empty hash
my $self = bless {}, $class;
# Even as the database grows over the years the version table
# should remain quite small, so slurp it down.
my $so = new MicrobeDB::Search();
my @versions = sort {$a->version_id cmp $b->version_id } $so->object_search( new MicrobeDB::Version());
$self->{versions} = \@versions;
# Save the mappings of actual version numbers to where they are
# in the index
for(my $i = 0; $i <= $#versions; $i++) {
$self->{index_map}->{$versions[$i]->version_id} = $i;
}
$self->index(0);
return $self;
}
sub next_version {
my ($self) = @_;
# get the current index
my $index = $self->{version_index};
return undef if($index >= scalar(@{$self->{versions}}));
my $ver = $self->{versions}->[$index];
$self->{version_index}++;
return $ver;
}
sub get_versions {
my ($self) = @_;
my @keys = sort keys (%{$self->{index_map}});
return @keys;
}
sub get_version {
my ($self, $ver) = @_;
# If a version is defined get that version
return $self->{versions}[$self->{index_map}->{$ver}]
if(defined($ver) && defined($self->{index_map}->{$ver}));
# Otherwise get the current version based on the index
return $self->{versions}[$self->{version_index}] if($self->{version_index} <= $#{$self->{versions}});
return undef;
}
# Compare the $updatedver against $basever and show what has changed
# compared to the $basever. If no parameters are given it will default
# to $updatedver being the newest version and $basever being the
# immediately prior version
sub version_changes {
my ($self, $updatedver, $basever) = @_;
unless(defined($updatedver) && defined($basever)) {
# Get newest two versions
my @vers = $self->get_versions();
$updatedver = pop @vers;
$basever = pop @vers;
} else {
return undef unless($self->get_version($updatedver) &&
$self->get_version($basever));
}
my $base_hash = $self->_get_version_hash($basever);
my $updated_hash = $self->_get_version_hash($updatedver);
my $newrec; my $changed; my $deleted;
foreach my $acc (keys %{$updated_hash}) {
if(defined $base_hash->{$acc}) {
# We have an intersection, has it changed?
# Yes it has if the following is true
$changed->{$acc} = $updated_hash->{$acc}
if($updated_hash->{$acc}->{ver} ne $base_hash->{$acc}->{ver});
} else {
# Ok, it's new!
$newrec->{$acc} = $updated_hash->{$acc};
}
}
# Now unfortunately we have to loop backwards to find all the deleted
foreach my $acc (keys %{$base_hash}) {
$deleted->{$acc} = $base_hash->{$acc}
unless(defined $updated_hash->{$acc});
}
return ($newrec, $changed, $deleted);
}
# Return the MicrobeDB version number of the
# newest version loaded
sub newest_version {
my ($self) = @_;
my @vers = $self->get_versions();
return pop @vers;
}
# Check if a particular version is valid still
# Give a MicrobeDB version number and check the result
sub isvalid {
my ($self, $ver) = @_;
my @vers = $self->get_versions();
foreach my $v (@vers) {
return 1 if($ver eq $v);
}
return 0;
}
sub _get_version_hash {
my ($self, $ver) = @_;
my $dbh = $self->_db_connect();
my $sth = $dbh->prepare("SELECT replicon.rep_accnum, replicon.rpv_id, replicon.gpv_id, genomeproject.taxon_id, genomeproject.gp_id FROM replicon, genomeproject WHERE genomeproject.gpv_id = replicon.gpv_id AND genomeproject.version_id = ? AND replicon.version_id = ?");
$sth->execute($ver, $ver);
my $ver_hash;
while(my @row = $sth->fetchrow_array) {
my ($accnum, $ver) = split '\.', $row[0];
$ver_hash->{$accnum}->{ver} = $ver;
$ver_hash->{$accnum}->{rpv_id} = $row[1];
$ver_hash->{$accnum}->{gpv_id} = $row[2];
$ver_hash->{$accnum}->{taxon_id} = $row[3];
$ver_hash->{$accnum}->{gp_id} = $row[4];
}
return $ver_hash;
}
sub index {
my ($self, $newvalue) = @_;
return -1 if(defined($newvalue) && $newvalue >= scalar(@{$self->{versions}}));
return -1 if($self->{version_index} >= scalar(@{$self->{versions}}));
$self->{version_index} = $newvalue if(defined($newvalue));
return $self->{version_index};
}
sub max_index {
my ($self) = @_;
return $#{$self->{versions}};
}
1;