blib/lib/Bio/Phylo/Forest/DBTree.pm untested

filter: Statementstmt 0.0% 0 / 108 Branchbran 0.0% 0 / 10 Conditioncond 0.0% 0 / 4 MC/DCmcdc n/a Subroutinesub 0.0% 0 / 20 Podpod 0.0% 0 / 11 totaltotal 0.0% 0 / 153 SCAR 37.4
CC
6
Cov
0%
persist
20.0
connect
12.0
create
2.0
CRAP 42.0
1 package Bio::Phylo::Forest::DBTree;
2 use strict;
3 use warnings;
4 use DBI;
5 use Bio::Phylo::Factory;
6 use Bio::Phylo::Util::Exceptions 'throw';
7 use base 'DBIx::Class::Schema';
8 use base 'Bio::Phylo::Forest::Tree';
9  
10 __PACKAGE__->load_namespaces;
11  
12 my $SINGLETON;
13 my $DBH;
14 my $fac = Bio::Phylo::Factory->new;
15 use version 0.77; our $VERSION = qv("v0.1.2");
16  
17 =head1 NAME
18  
19 Bio::Phylo::Forest::DBTree - Phylogenetic database as a tree object
20  
21 =head1 SYNOPSIS
22  
23 use Bio::Phylo::Forest::DBTree;
24
25 # connect to the Green Genes tree
26 my $file = 'gg_13_5_otus_99_annotated.db';
27 my $dbtree = Bio::Phylo::Forest::DBTree->connect($file);
28  
29 # $dbtree can be used as a Bio::Phylo::Forest::Tree object,
30 # and the node objects that are returned can be used as
31 # Bio::Phylo::Forest::Node objects
32 my $root = $dbtree->get_root;
33  
34 =head1 DESCRIPTION
35  
36 This package provides the functionality to handle very large phylogenies (examples: the
37 NCBI taxonomy, the Green Genes tree) as if they are L<Bio::Phylo> tree objects, with all
38 the possibilities for traversal, computation, serialization, and visualization, but stored
39 in a SQLite database. These databases are single files, so that they can be easily shared.
40 Some useful database files are available here:
41 https://figshare.com/account/home#/projects/18808
42  
43 To make new tree databases, a number of scripts are provided with the distribution of this
44 package:
45  
46 =over
47  
48 =item * C<megatree-loader> Loads a very large Newick tree into a database.
49  
50 =item * C<megatree-ncbi-loader> Loads the NCBI taxonomy dump into a database.
51  
52 =item * C<megatree-phylotree-loader> Loads a tree in the format of L<http://phylotree.org>
53 into a database.
54  
55 =back
56  
57 As an example of interacting with a database tree, the script C<megatree-pruner> can be
58 used to extract subtrees from a database.
59  
60 =head1 DATABASE METHODS
61  
62 The following methods deal with the database as a whole: creating a new database,
63 connecting to an existing one, persisting a tree in a database and extracting one as a
64 mutable, in-memory object.
65  
66 =head2 create()
67  
68 Creates a SQLite database file in the provided location. Usage:
69  
70 use Bio::Phylo::Forest::DBTree;
71
72 # second argument is optional
73 Bio::Phylo::Forest::DBTree->create( $file, '/opt/local/bin/sqlite3' );
74  
75 The first argument is the location where the database file is going to be created. The
76 second argument is optional, and provides the location of the C<sqlite3> executable that
77 is used to create the database. By default, the C<sqlite3> is simply found on the
78 C<$PATH>, but if it is installed in a non-standard location that location can be provided
79 here. The database schema that is created corresponds to the following SQL statements:
80  
81 create table node(
82 id int not null,
83 parent int,
84 left int,
85 right int,
86 name varchar(20),
87 length float,
88 height float,
89 primary key(id)
90 );
91 create index parent_idx on node(parent);
92 create index left_idx on node(left);
93 create index right_idx on node(right);
94 create index name_idx on node(name);
95  
96 =cut
97  
98 sub create {
99 my $class = shift;
100 my $file = shift;
101 my $sqlite3 = shift || 'sqlite3';
102 my $command = do { local $/; <DATA> };
103 system("echo '$command' | sqlite3 '$file'") == 0 or die 'Create failed!';
104 }
105  
106 =head2 connect()
107  
108 Connects to a SQLite database file, returns the connection as a
109 C<Bio::Phylo::Forest::DBTree> object. Usage:
110  
111 use Bio::Phylo::Forest::DBTree;
112 my $dbtree = Bio::Phylo::Forest::DBTree->connect($file);
113  
114 The argument is a file name. If the file exists, a L<DBD::SQLite> database handle to that
115 file is returned. If the file does not exist, a new database is created in that location,
116 and subsequently the handle to that newly created database is returned. The creation of
117 the database is handled by the C<create()> method (see below).
118  
119 =cut
120  
121 sub connect {
122 my $class = shift;
123 my $file = shift;
124 if ( not $SINGLETON ) {
125
126 # create if not exist
127 if ( not -e $file ) {
128 $class->create($file);
129 }
130
131 # fuck it, let's just hardcode it here - Yeehaw!
132 my $dsn = "dbi:SQLite:dbname=$file";
133 $DBH = DBI->connect($dsn,'','');
134 $DBH->{'RaiseError'} = 1;
135 $SINGLETON = $class->SUPER::connect( sub { $DBH } );
136 }
137 return $SINGLETON;
138 }
139  
140 =head2 persist()
141  
142 Persist a phylogenetic tree object (a subclass of L<Bio::Phylo::Forest::Tree>) into a
143 newly created database file. Usage:
144  
145 use Bio::Phylo::Forest::DBTree;
146 my $dbtree = Bio::Phylo::Forest::DBTree->persist(
147 -file => $file,
148 -tree => $tree,
149 );
150  
151 This method first create a database at the location specified by C<$file> by making a call
152 to the C<create()> method. Subsequently, the C<$tree> object is traversed from root to
153 tips and inserted in the newly created database. Finally, the handle to this database is
154 returned, i.e. a C<Bio::Phylo::Forest::DBTree> object.
155  
156 =cut
157  
158 sub persist {
159 my ( $class, %args ) = @_;
160
161 # need a file argument to write to
162 if ( not $args{'-file'} ) {
163 throw 'BadArgs' => "Need -file argument!";
164 }
165
166 # need a tree argument to persis
167 if ( not $args{'-tree'} ) {
168 throw 'BadArgs' => "Need -tree argument!";
169 }
170
171 # create a new database, prepare statement handler
172 $class->create( $args{'-file'} );
173 my $dsn = 'dbi:SQLite:dbname=' . $args{'-file'};
174 my $dbh = DBI->connect($dsn,'','');
175 $dbh->{'RaiseError'} = 1;
176 my $db = $class->SUPER::connect( sub { $dbh } );
177 my $sth = $dbh->prepare("insert into node values(?,?,?,?)");
178
179 # start traversing
180 my $counter = 2;
181 my %idmap;
182 $args{'-tree'}->visit_depth_first(
183 '-pre' => sub {
184 my $node = shift;
185 my $id = $node->get_id;
186 $idmap{$id} = $counter++;
187
188 # get the parent id, or "1" if root
189 my $parent_id;
190 if ( my $parent = $node->get_parent ) {
191 my $pid = $parent->get_id;
192 $parent_id = $idmap{$pid};
193 }
194 else {
195 $parent_id = 1;
196 }
197
198 # do the insertion
199 $sth->execute(
200 $idmap{$id}, # primary key
201 $parent_id, # self-joining foreign key
202 undef, # not indexed yet
203 undef, # not indexed yet
204 $node->get_internal_name, # node label or taxon name
205 $node->get_branch_length, # branch length
206 undef # not computed yet
207 );
208 }
209 );
210 my $i = 0;
211 $db->get_root->_index(\$i,0);
212 return $db;
213 }
214  
215 =head2 extract()
216  
217 Extracts a tree from a database. The returned tree is an in-memory object. Hence, this is
218 an expensive operation that is best avoided as much as possible. Usage:
219  
220 my $tree = $dbtree->extract;
221  
222 =cut
223  
224 sub extract {
225 my $self = shift;
226 my $tree = $fac->create_tree;
227 my $root = $self->get_root;
228 _clone_mutable(
229 $fac->create_node(
230 '-name' => $root->get_name,
231 '-branch_length' => $root->get_branch_length,
232 ),
233 $root,
234 $tree
235 );
236 return $tree;
237 }
238  
239 {
240 no warnings 'recursion';
241 sub _clone_mutable {
242 my ( $parent, $template, $tree ) = @_;
243 $tree->insert($parent);
244 for my $child ( @{ $template->get_children } ) {
245 _clone_mutable(
246 $fac->create_node(
247 '-name' => $child->get_name,
248 '-branch_length' => $child->get_branch_length,
249 '-parent' => $parent,
250 ),
251 $child,
252 $tree
253 );
254 }
255 }
256 }
257  
258 =head2 dbh()
259  
260 Returns the underlying handle through which SQL statements can be executed directly on the
261 database. This is a L<DBD::SQLite> object. Usage:
262  
263 my $dbh = $dbtree->dbh;
264  
265 =cut
266  
267 sub dbh { $DBH }
268  
269 =head1 TREE METHODS
270  
271 The following methods are implemented here to override methods of the same name in the
272 L<Bio::Phylo> hierarchy so that the tree database is accessed more efficiently than
273 otherwise would be the case.
274  
275 =head2 get_root()
276  
277 Returns the root of the tree, i.e. a L<Bio::Phylo::Forest::DBTree::Result::Node> object,
278 which is a subclass of L<Bio::Phylo::Forest::Node>. Usage:
279  
280 my $root = $dbtree->get_root;
281  
282 =cut
283  
284 sub get_root {
285 shift->_rs->search(
286 { 'parent' => 1 },
287 {
288 'order_by' => 'id',
289 'rows' => 1,
290 }
291 )->single
292 }
293  
294 =head2 get_id()
295  
296 Returns a dummy ID, an integer. Usage:
297  
298 my $id = $dbtree->get_id;
299  
300 =cut
301  
302 sub get_id { 0 }
303  
304 =head2 get_by_name()
305  
306 Returns the first node object that has the provided name. Usage:
307  
308 my $node = $dbtree->get_by_name( 'Homo sapiens' );
309  
310 =cut
311  
312 sub get_by_name {
313 my ( $self, $name ) = @_;
314 return $self->_rs->search({ 'name' => $name })->single;
315 }
316  
317 =head2 visit()
318  
319 Given a code reference, visits all the nodes in the tree and executes the code on the
320 focal node. Usage:
321  
322 $dbtree->visit(sub{
323 my $node = shift;
324 print $node->name, "\n";
325 });
326  
327 =cut
328  
329 sub visit {
330 my ( $self, $code ) = @_;
331 my $rs = $self->_rs;
332 while( my $node = $rs->next ) {
333 $code->($node);
334 }
335 return $self;
336 }
337  
338 sub _rs { shift->resultset('Node') }
339  
340 1;
341  
342 __DATA__
343 create table node(id int not null,parent int,left int,right int,name varchar(20),length float,height float,primary key(id));
344 create index parent_idx on node(parent);
345 create unique index left_idx on node(left asc);
346 create unique index right_idx on node(right asc);
347 create index name_idx on node(name);