EnsEMBL/Map/DBSQL/DitagAdaptor.pm untested

filter: Statementstmt 0.0% 0 / 115 Branchbran 0.0% 0 / 24 Conditioncond 0.0% 0 / 9 Subroutinesub 0.0% 0 / 16 Podpod 0.0% 0 / 11 totaltotal 0.0% 0 / 175 CC 13 SCAR 31.5
CC
13
Cov
0%
store
72.0
fetch_all_by_name
6.0
fetch_by_dbID
6.0
CRAP 23.4
1 =head1 LICENSE
2  
3 See the NOTICE file distributed with this work for additional information
4 regarding copyright ownership.
5  
6 Licensed under the Apache License, Version 2.0 (the "License");
7 you may not use this file except in compliance with the License.
8 You may obtain a copy of the License at
9  
10 http://www.apache.org/licenses/LICENSE-2.0
11  
12 Unless required by applicable law or agreed to in writing, software
13 distributed under the License is distributed on an "AS IS" BASIS,
14 WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15 See the License for the specific language governing permissions and
16 limitations under the License.
17  
18 =cut
19  
20  
21 =head1 CONTACT
22  
23 Please email comments or questions to the public Ensembl
24 developers list at <http://lists.ensembl.org/mailman/listinfo/dev>.
25  
26 Questions may also be sent to the Ensembl help desk at
27 <http://www.ensembl.org/Help/Contact>.
28  
29 =cut
30  
31 =head1 NAME
32  
33 Bio::EnsEMBL::Map::DBSQL::DitagAdaptor
34  
35 =head1 SYNOPSIS
36  
37 my $ditagadaptor = $db->get_DitagAdaptor();
38 my @ditags = @{ $ditagadaptor->fetch_by_type("ZZ11") };
39  
40 =head1 DESCRIPTION
41  
42 Provides database interaction for the Bio::EnsEMBL::Map::Ditag object
43  
44 =head1 METHODS
45  
46 =cut
47  
48 package Bio::EnsEMBL::Map::DBSQL::DitagAdaptor;
49 $Bio::EnsEMBL::Map::DBSQL::DitagAdaptor::VERSION = '114.0.0_50'; # TRIAL
50 $Bio::EnsEMBL::Map::DBSQL::DitagAdaptor::VERSION = '114.0.050';
51 use strict;
52 use vars ('@ISA');
53  
54 use Bio::EnsEMBL::Map::Ditag;
55 use Bio::EnsEMBL::DBSQL::BaseAdaptor;
56 use Bio::EnsEMBL::Utils::Exception qw( throw warning );
57  
58 @ISA = qw(Bio::EnsEMBL::DBSQL::BaseAdaptor);
59  
60  
61 =head2 fetch_all_by_name
62  
63 Arg [1] : ditag name
64 Example : $tag = $ditag_adaptor->fetch_by_name("U3");
65 Description: Retrieves ditags from the database using the name
66 Returntype : listref of Bio::EnsEMBL::Map::Ditag
67 Caller : general
68  
69 =cut
70  
71 sub fetch_all_by_name {
72 my ($self, $tagname) = @_;
73  
74 if(!$tagname){
75 throw "must be called with a name of a ditag.";
76 }
77 my $sth = $self->prepare("SELECT d.ditag_id, d.name, d.type, d.tag_count, d.sequence
78 FROM ditag d
79 WHERE d.name = ?");
80 $sth->execute($tagname);
81 my $result = $self->_fetch($sth);
82  
83 return $result;
84  
85 }
86  
87  
88 =head2 fetch_by_dbID
89  
90 Arg [1] : ditag type
91 Example : @all_tags = @{$ditag_adaptor->fetch_by_dbID(1003)};
92 Description: Retrieve ditags with a certain id from the database
93 Returntype : Bio::EnsEMBL::Map::Ditag
94 Caller : general
95  
96 =cut
97  
98 sub fetch_by_dbID {
99 my ($self, $tagid) = @_;
100  
101 if(!$tagid){
102 throw "must be called with the type of a ditag.";
103 }
104 my $sth = $self->prepare("SELECT d.ditag_id, d.name, d.type, d.tag_count, d.sequence
105 FROM ditag d
106 WHERE d.ditag_id = ?");
107 $sth->execute($tagid);
108 my $result = $self->_fetch($sth);
109  
110 return $result->[0];
111 }
112  
113  
114 =head2 fetch_all_by_type
115  
116 Arg [1] : ditag type
117 Example : @all_tags = @{$ditag_adaptor->fetch_by_type("SME005")};
118 Description: Retrieves all ditags of a certain type from the database
119 Returntype : listref of Bio::EnsEMBL::Map::Ditag
120 Caller : general
121  
122 =cut
123  
124 sub fetch_all_by_type {
125 my ($self, $tagtype) = @_;
126  
127 if(!$tagtype){
128 throw "must be called with the type of a ditag.";
129 }
130 my $sth = $self->prepare("SELECT d.ditag_id, d.name, d.type, d.tag_count, d.sequence
131 FROM ditag d
132 WHERE d.type = ?");
133 $sth->execute($tagtype);
134 my $result = $self->_fetch($sth);
135  
136 return $result;
137 }
138  
139  
140 =head2 fetch_by_name_and_type
141  
142 Arg [1] : ditag name
143 Arg [2] : ditag type
144 Example : $tag = $ditag_adaptor->fetch_by_name_and_type("U3", "SME005");
145 Description: Retrieves ditags from the database using name/type combination
146 which should be non-ambiguous
147 Returntype : Bio::EnsEMBL::Map::Ditag
148 Caller : general
149  
150 =cut
151  
152 sub fetch_by_name_and_type {
153 my ($self, $tagname, $tagtype) = @_;
154  
155 if(!$tagname or !$tagtype){
156 throw "must be called with a name and type of a ditag.";
157 }
158 my $sth = $self->prepare("SELECT d.ditag_id, d.name, d.type, d.tag_count, d.sequence
159 FROM ditag d
160 WHERE d.name = ? and d.type = ?");
161 $sth->execute($tagname, $tagtype);
162 my $result = $self->_fetch($sth);
163  
164 return $result->[0];
165 }
166  
167  
168 =head2 fetch_all
169  
170 Args : none
171 Example : @all_tags = @{$ditag_adaptor->fetch_all};
172 Description: Retrieves all ditags from the database
173 Returntype : listref of Bio::EnsEMBL::Map::Ditag
174 Caller : general
175  
176 =cut
177  
178 sub fetch_all {
179 my ($self) = @_;
180  
181 my $sth = $self->prepare("SELECT d.ditag_id, d.name, d.type, d.tag_count, d.sequence
182 FROM ditag d");
183 $sth->execute;
184 my $result = $self->_fetch($sth);
185  
186 return $result;
187 }
188  
189  
190 =head2 fetch_all_with_limit
191  
192 Arg [1] : ditag type
193 Arg [2] : row limit
194 Arg [3] : row offset
195 Description: fetch_by_type with row limit and offset
196 Returntype : listref of Bio::EnsEMBL::Map::Ditag
197 Caller : general
198  
199 =cut
200  
201 sub fetch_all_with_limit {
202 my ($self, $tagtype, $limit, $offset) = @_;
203  
204 my @ditags = ();
205 my $sql = "SELECT d.ditag_id, d.name, d.type, d.tag_count, d.sequence ".
206 "FROM ditag d ".
207 "WHERE d.type = ? LIMIT ? OFFSET ?;";
208 my $sth = $self->db->dbc->prepare($sql);
209 $sth->execute($tagtype, $limit, $offset);
210 my $result = $self->_fetch($sth);
211  
212 return $result;
213 }
214  
215  
216 =head2 _fetch
217  
218 Arg [1] : statement handler object
219 Description: generic sql-fetch function for the ditag fetch methods
220 Returntype : listref of Bio::EnsEMBL::Map::Ditag
221 Caller : private
222  
223 =cut
224  
225 sub _fetch {
226 my ($self, $sth) = @_;
227  
228 my($tag_id, $name, $type, $count, $sequence);
229 my @tags;
230  
231 $sth->bind_columns(\$tag_id, \$name, \$type, \$count, \$sequence);
232 while($sth->fetch) {
233 push @tags, Bio::EnsEMBL::Map::Ditag->new (
234 -dbID => $tag_id,
235 -name => $name,
236 -type => $type,
237 -tag_count => $count,
238 -sequence => $sequence,
239 -adaptor => $self,
240 );
241 }
242  
243 return \@tags;
244 }
245  
246  
247 =head2 store
248  
249 Arg [1] : Bio::EnsEMBL::Map::Ditag
250 Example : $ditag_adaptor->store(\@ditags);
251 Description: Stores a single ditag or
252 a list of ditags in this database.
253 Returntype : none
254 Caller : general
255  
256 =cut
257  
258 sub store {
259 my ($self, $ditags) = @_;
260  
261 if(ref $ditags eq 'ARRAY'){
262 if(scalar(@$ditags) == 0){
263 throw("Must call store with ditag or list ref of ditags");
264 }
265 }
266 elsif($ditags){
267 my @ditags;
268 push @ditags, $ditags;
269 $ditags = \@ditags;
270 }
271 else{
272 throw("Must call store with ditag or list ref of ditags not ".$ditags);
273 }
274  
275 my $db = $self->db() or throw "Couldn t get database connection.";
276  
277 TAG:
278 foreach my $ditag (@$ditags) {
279  
280 if ( !ref $ditag || !$ditag->isa("Bio::EnsEMBL::Map::Ditag") ) {
281 throw( "Object must be an Ensembl Ditag, " . "not a [" . ref($ditag) . "]" );
282 }
283  
284 if ( $ditag->is_stored($db) ) {
285 warning( "Ditag [" . $ditag->dbID . "] is already stored in this database." );
286 next TAG;
287 }
288  
289 #check if tag with same name/type already exists
290 my $sth = $self->prepare( "SELECT COUNT(*) FROM ditag
291 WHERE name = ? AND type = ?" );
292 $sth->execute($ditag->name, $ditag->type);
293 if($sth->fetchrow() > 0){
294 warning( "Ditag with name/type ".$ditag->name." / ".$ditag->type.
295 " is already stored in this database.\n".
296 "Use update_ditag() instead.");
297 next TAG;
298 }
299  
300 if ( $ditag->dbID ) {
301 my $sth = $self->prepare( "INSERT INTO ditag( ditag_id , name, type, tag_count, sequence ) ".
302 "VALUES( ?,?,?,?,? )" );
303 $sth->bind_param(1,$ditag->dbID,SQL_INTEGER);
304 $sth->bind_param(2,$ditag->name,SQL_VARCHAR);
305 $sth->bind_param(3,$ditag->type,SQL_VARCHAR);
306 $sth->bind_param(4,$ditag->tag_count,SQL_VARCHAR);
307 $sth->bind_param(5,$ditag->sequence,SQL_VARCHAR);
308 $sth->execute();
309 } else {
310 my $sth = $self->prepare( "INSERT INTO ditag( name, type, tag_count, sequence ) ".
311 "VALUES( ?,?,?,? )" );
312 $sth->bind_param(1,$ditag->name,SQL_VARCHAR);
313 $sth->bind_param(2,$ditag->type,SQL_VARCHAR);
314 $sth->bind_param(3,$ditag->tag_count,SQL_VARCHAR);
315 $sth->bind_param(4,$ditag->sequence,SQL_VARCHAR);
316 $sth->execute();
317 my $dbID = $self->last_insert_id('ditag_id', undef, 'ditag');
318 $ditag->dbID($dbID);
319 $ditag->adaptor($self);
320 }
321 }
322  
323 return 1;
324 }
325  
326  
327 =head2 print_creation
328  
329 Arg [1] : ditag probe name
330 Arg [2] : ditag type
331 Arg [3] : ditag count
332 Arg [4] : ditag sequence
333 Arg [5] : (optional) ditag dbID
334 Description: convenience method to produce SQL insert statements
335 to speed up the creation of new ditags
336 Returntype : string
337  
338 =cut
339  
340 sub print_creation {
341 my ($self, $probe_name, $type, $count, $sequence, $dbid) = @_;
342 my $string;
343 if($dbid){
344 $string = "INSERT INTO ditag( ditag_id, name, type, tag_count, sequence ) ".
345 "VALUES($dbid, '".$probe_name."', '".$type."', ".$count."'".$sequence."');\n";
346 }
347 else {
348 $string = "INSERT INTO ditag( name, type, tag_count, sequence ) ".
349 "VALUES('".$probe_name."', '".$type."', ".$count.", '".$sequence."');\n";
350 }
351  
352 return $string;
353 }
354  
355  
356 =head2 update_ditag
357  
358 Arg [1] : ditag to update
359 Description: update an existing ditag with new values
360 Returntype : true on success
361  
362 =cut
363  
364 sub update_ditag {
365 my ($self, $ditag) = @_;
366  
367 my $sth = $self->prepare( "UPDATE ditag SET name=?, type=?, tag_count=?, sequence=? where ditag_id=?;" );
368 my $result =$sth->execute(
369 $ditag->name,
370 $ditag->type,
371 $ditag->tag_count,
372 $ditag->sequence,
373 $ditag->dbID,
374 );
375  
376 return $result;
377 }
378  
379  
380 =head2 list_dbIDs
381  
382 Args : None
383 Example : my @feature_ids = @{$da->list_dbIDs()};
384 Description: Gets an array of internal IDs for all Ditag objects in
385 the current database.
386 Returntype : List of ints
387 Exceptions : None
388  
389 =cut
390  
391 sub list_dbIDs {
392 my ($self, $ordered) = @_;
393
394 return $self->_list_dbIDs('ditag');
395 }
396  
397 1;