ensembl-hive  2.8.1
DitagFeatureAdaptor.pm
Go to the documentation of this file.
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 
34 
35 =head1 SYNOPSIS
36 
37  my $dfa = $db->get_DitagFeatureAdaptor;
38  my $ditagFeatures = $dfa->fetch_all_by_Slice( $slice, "SME005" );
39 
40  foreach my $ditagFeature (@$ditagFeatures) {
41  print $ditagFeature->ditag_id . " "
42  . $ditagFeature->slice . " "
43  . $ditagFeature->start . "-"
44  . $ditagFeature->end . " "
45  . $ditagFeature->strand;
46  }
47 
48 =head1 DESCRIPTION
49 
50 Provides database interaction for the Bio::EnsEMBL::Map::DitagFeature
51 object
52 
53 =head1 METHODS
54 
55 =cut
56 
57 package Bio::EnsEMBL::Map::DBSQL::DitagFeatureAdaptor;
58 
59 use strict;
60 use vars ('@ISA');
61 
65 use Bio::EnsEMBL::Utils::Exception qw( throw warning );
66 
68 
69 
70 =head2 fetch_all
71 
72  Arg [1] : none
73  Example : @all_tags = @{$ditagfeature_adaptor->fetch_all};
74  Description: Retrieves all ditagFeatures from the database;
75  Usually not a good idea, use fetch_all_by_Slice instead.
76  Returntype : listref of Bio::EnsEMBL::Map::DitagFeature
77  Caller : general
78  Status : At Risk
79 
80 =cut
81 
82 sub fetch_all {
83  my $self = shift;
84 
85  my $sth = $self->prepare("SELECT df.ditag_feature_id, df.ditag_id, df.seq_region_id,
86  df.seq_region_start, df.seq_region_end, df.seq_region_strand,
87  df.analysis_id, df.hit_start, df.hit_end, df.hit_strand,
88  df.cigar_line, df.ditag_side, df.ditag_pair_id, d.tag_count
89  FROM ditag_feature df, ditag d
90  WHERE df.ditag_id=d.ditag_id" );
91  $sth->execute;
92 
93  my $result = $self->_fetch($sth);
94 
95  return $result;
96 }
97 
98 
99 =head2 fetch_by_dbID
100 
101  Arg [1] : ditagFeature dbID
102  Example : @my_tags = @{$ditagfeature_adaptor->fetch_by_dbID($my_id)};
103  Description: Retrieves a ditagFeature from the database.
105  Caller : general
106  Status : At Risk
107 
108 =cut
109 
110 sub fetch_by_dbID {
111  my ($self, $dbid) = @_;
112 
113  my $sth = $self->prepare("SELECT df.ditag_feature_id, df.ditag_id, df.seq_region_id,
114  df.seq_region_start, df.seq_region_end, df.seq_region_strand,
115  df.analysis_id, df.hit_start, df.hit_end, df.hit_strand,
116  df.cigar_line, df.ditag_side, df.ditag_pair_id, d.tag_count
117  FROM ditag_feature df, ditag d
118  WHERE df.ditag_id=d.ditag_id AND df.ditag_feature_id = ?" );
119  $sth->execute($dbid);
120 
121  my $result = $self->_fetch($sth);
122 
123  return $result->[0];
124 }
125 
126 
127 =head2 fetch_all_by_ditagID
128 
129  Arg [1] : ditag dbID
130  Arg [2] : (optional) ditag-pair dbID
131  Arg [3] : (optional) analysis ID
132  Example : @my_tags = @{$ditagfeature_adaptor->fetch_all_by_ditag_id($my_id)};
133  Description: Retrieves all ditagFeatures from the database linking to a specific ditag-id
134  Returntype : listref of Bio::EnsEMBL::Map::DitagFeature
135  Caller : general
136  Status : At Risk
137 
138 =cut
139 
140 sub fetch_all_by_ditagID {
141  my ($self, $ditag_id, $ditag_pair_id, $analysis_id) = @_;
142 
143  my $arg = $ditag_id;
144  my $sql = "SELECT df.ditag_feature_id, df.ditag_id, df.seq_region_id,
145  df.seq_region_start, df.seq_region_end, df.seq_region_strand,
146  df.analysis_id, df.hit_start, df.hit_end, df.hit_strand,
147  df.cigar_line, df.ditag_side, df.ditag_pair_id, d.tag_count
148  FROM ditag_feature df, ditag d
149  WHERE df.ditag_id=d.ditag_id AND df.ditag_id = ? ";
150  if($ditag_pair_id){
151  $sql .= "AND df.ditag_pair_id = ? ";
152  $arg .= ", $ditag_pair_id";
153  }
154  if($analysis_id){
155  $sql .= "AND df.analysis_id = ? ";
156  $arg .= ", $analysis_id";
157  }
158  $sql .= "ORDER BY df.ditag_pair_id";
159  my $sth = $self->prepare($sql);
160  $sth->execute(split(",",$arg));
161 
162  my $result = $self->_fetch($sth);
163 
164  return $result;
165 }
166 
167 
168 =head2 fetch_all_by_type
169 
170  Arg [1] : ditag type
171  Example : @my_tags = @{$ditagfeature_adaptor->fetch_all_by_type($type)};
172  Description: Retrieves all ditagFeatures from the database linking to a specific ditag-type
173  Returntype : listref of Bio::EnsEMBL::Map::DitagFeature
174  Caller : general
175  Status : At Risk
176 
177 =cut
178 
179 sub fetch_all_by_type {
180  my ($self, $ditag_type) = @_;
181 
182  my $sth = $self->prepare("SELECT df.ditag_feature_id, df.ditag_id, df.seq_region_id,
183  df.seq_region_start, df.seq_region_end, df.seq_region_strand,
184  df.analysis_id, df.hit_start, df.hit_end, df.hit_strand,
185  df.cigar_line, df.ditag_side, df.ditag_pair_id, d.tag_count
186  FROM ditag_feature df, ditag d
187  WHERE df.ditag_id=d.ditag_id AND d.type = ?
188  ORDER BY df.ditag_id, df.ditag_pair_id" );
189  $sth->execute($ditag_type);
190 
191  my $result = $self->_fetch($sth);
192 
193  return $result;
194 }
195 
196 
197 =head2 fetch_all_by_Slice
198 
199  Arg [1] : Bio::EnsEMBL::Slice
200  Arg [2] : (optional) ditag type name (specific library) or an aray ref with multiple type names
201  Arg [3] : (optional) analysis logic_name
202  Example : $tags = $ditagfeature_adaptor->fetch_all_by_Slice($slice, "SME005");
203  Description: Retrieves ditagFeatures from the database overlapping a specific region
204  and (optional) of a specific ditag type or analysis.
205  Start & end locations are returned in slice coordinates, now.
206  Returntype : listref of Bio::EnsEMBL::Map::DitagFeatures
207  Caller : general
208  Status : At Risk
209 
210 =cut
211 
212 sub fetch_all_by_Slice {
213  my ($self, $slice, $tagtype, $logic_name) = @_;
214 
215  my @result;
216  my $moresql;
217 
218  if(!ref($slice) || !$slice->isa("Bio::EnsEMBL::Slice")) {
219  throw("Bio::EnsEMBL::Slice argument expected not $slice.");
220  }
221 
222  #get affected ditag_feature_ids
223  my $sql = "SELECT df.ditag_feature_id, df.ditag_id, df.seq_region_id, df.seq_region_start,
224  df.seq_region_end, df.seq_region_strand, df.analysis_id, df.hit_start, df.hit_end,
225  df.hit_strand, df.cigar_line, df.ditag_side, df.ditag_pair_id,
226  d.tag_count
227  FROM ditag_feature df, ditag d
228  WHERE df.ditag_id=d.ditag_id";
229  if($tagtype){
230  my $tagtypes = '';
231  #check if array
232  if(ref $tagtype eq 'ARRAY'){
233  my @arraytype_mod;
234  foreach my $arraytype (@$tagtype){ push @arraytype_mod, '"'.$arraytype.'"' }
235  $tagtypes = join(", ", @arraytype_mod);
236  }
237  else{
238  $tagtypes = '"'.$tagtype.'"';
239  }
240  $sql .= " AND d.type IN(".$tagtypes.")";
241  }
242  if($logic_name){
243  my $analysis = $self->db->get_AnalysisAdaptor->fetch_by_logic_name($logic_name);
244  if(!$analysis) {
245  return undef;
246  }
247  $sql .= " AND df.analysis_id = ".$analysis->dbID();
248  }
249  $sql .= " AND df.seq_region_id = ".$slice->get_seq_region_id.
250  " AND df.seq_region_start <= ".$slice->end.
251  " AND df.seq_region_end >= ".$slice->start;
252 
253  my $sth = $self->prepare($sql);
254  $sth->execute();
255 
256  my $result = $self->_fetch($sth, $slice);
257  push(@result, @$result);
258 
259  return \@result;
260 }
261 
262 
263 =head2 fetch_pairs_by_Slice
264 
265  Arg [1] : Bio::EnsEMBL::Slice
266  Arg [2] : (optional) ditag type (specific library)
267  Arg [3] : (optional) analysis logic_name
268  Example : my $ditagfeatures = $dfa->fetch_pairs_by_Slice($slice);
269  foreach my $ditagfeature (@$ditagfeatures){
270  $minstart = $$ditagfeature2{'start'};
271  $maxend = $$ditagfeature2{'end'};
272  $bothstrand = $$ditagfeature2{'strand'};
273  $tag_count = $$ditagfeature2{'tag_count'};
274  print "$minstart, $maxend, $bothstrand, $tag_count\n";
275  }
276  Description: Retrieves ditagFeature information in pairs from the database overlapping a specific region
277  and (optional) of a specific ditag type or analysis. The absotute start and end points are
278  fetched.
279  Slices should be SMALL!
280  Returntype : array ref with hash ref of artifical DitagFeature object
281  Caller : general
282  Status : At Risk
283 
284 =cut
285 
286 sub fetch_pairs_by_Slice {
287  my ($self, $slice, $tagtype, $logic_name) = @_;
288  my ($tag_id, $pair_id, $seq_region_id, $start, $end, $strand, $analysis_id, $tag_count);
289  my @result;
290 
291  my $sql = "SELECT df.ditag_id, df.ditag_pair_id, df.seq_region_id, MIN(df.seq_region_start), ".
292  "MAX(df.seq_region_end), df.seq_region_strand, df.analysis_id, d.tag_count ".
293  "FROM ditag_feature df, ditag d ".
294  "WHERE df.ditag_id=d.ditag_id ";
295  if($tagtype){
296  $sql .= "AND d.type = \"".$tagtype."\"";
297  }
298  $sql .= " AND df.seq_region_id = ".$slice->get_seq_region_id.
299  " AND df.seq_region_start <= ".$slice->end.
300  " AND df.seq_region_end >= ".$slice->start;
301  if($logic_name){
302  my $analysis = $self->db->get_AnalysisAdaptor->fetch_by_logic_name($logic_name);
303  if(!$analysis) {
304  return undef;
305  }
306  $sql .= " AND df.analysis_id = ".$analysis->dbID();
307  }
308  $sql .= " GROUP BY df.ditag_id, df.ditag_pair_id;";
309  my $sth = $self->prepare($sql);
310  $sth->execute();
311  $sth->bind_columns( \$tag_id, \$pair_id, \$seq_region_id, \$start, \$end, \$strand, \$analysis_id ,\$tag_count);
312  while ( $sth->fetch ) {
313  # convert into relative slice coordinates
314  my $seq_region_len = $slice->seq_region_length();
315 
316  if ($slice->strand == 1) { # Positive strand
317  $start = $start - $slice->start + 1;
318  $end = $end - $slice->start + 1;
319 
320  if ($slice->is_circular()) { # Handle circular chromosomes
321  if ($start > $end) { # Looking at a feature overlapping the chromsome origin
322  if ($end > $slice->start) {
323  # Looking at the region in the beginning of the chromosome
324  $start -= $seq_region_len;
325  }
326 
327  if ($end < 0) {
328  $end += $seq_region_len;
329  }
330  } else {
331  if ($slice->start > $slice->end && $end < 0) {
332  # Looking at the region overlapping the chromosome origin and
333  # a feature which is at the beginning of the chromosome
334  $start += $seq_region_len;
335  $end += $seq_region_len;
336  }
337  }
338  } # end if ($dest_slice->is_circular...)
339 
340  } else { # Negative strand
341  my ($seq_region_start, $seq_region_end) = ($start, $end);
342  $start = $slice->end - $seq_region_end + 1;
343  $end = $slice->end - $seq_region_start + 1;
344 
345  if ($slice->is_circular()) {
346  if ($slice->start > $slice->end) { # slice spans origin or replication
347  if ($seq_region_start >= $slice->start) {
348  $end += $seq_region_len;
349  $start += $seq_region_len
350  if $seq_region_end > $slice->start;
351 
352  } elsif ($seq_region_start <= $slice->end) {
353  # do nothing
354  } elsif ($seq_region_end >= $slice->start) {
355  $start += $seq_region_len;
356  $end += $seq_region_len;
357 
358  } elsif ($seq_region_end <= $slice->end) {
359  $end += $seq_region_len
360  if $end < 0;
361  } elsif ($seq_region_start > $seq_region_end) {
362  $end += $seq_region_len;
363  } else { }
364 
365  } else {
366  if ($seq_region_start <= $slice->end and $seq_region_end >= $slice->start) {
367  # do nothing
368  } elsif ($seq_region_start > $seq_region_end) {
369  if ($seq_region_start <= $slice->end) {
370  $start -= $seq_region_len;
371  } elsif ($seq_region_end >= $slice->start) {
372  $end += $seq_region_len;
373  } else { }
374  }
375  }
376  }
377 
378  $strand *= -1;
379  }
380 
381  my %ditag_feature_pair = (
382  ditag => $tag_id,
383  pair_id => $pair_id,
384  region => $seq_region_id,
385  start => $start,
386  end => $end,
387  strand => $strand,
388  analysis => $analysis_id,
389  tag_count => $tag_count
390  );
391  push(@result, \%ditag_feature_pair);
392  }
393 
394  return \@result;
395 }
396 
397 
398 =head2 _fetch
399 
400  Arg [1] : statement handler
401  Arg [2] : (optional) target-slice for the feature
402  Description: generic sql-fetch function for the DitagFeature fetch methods
403  Returntype : listref of Bio::EnsEMBL::Map::DitagFeatures
404  Caller : private
405  Status : At Risk
406 
407 =cut
408 
409 sub _fetch {
410  my ($self, $sth, $dest_slice) = @_;
411 
412  my ( $tag_id, $mothertag_id, $seqreg, $seqstart, $seqend, $strand, $analysis_id, $hit_start,
413  $hit_end, $hit_strand, $cigar_line, $ditag_side, $ditag_pair_id, $tag_count );
414  $sth->bind_columns( \$tag_id, \$mothertag_id, \$seqreg,
415  \$seqstart, \$seqend, \$strand,
416  \$analysis_id, \$hit_start, \$hit_end,
417  \$hit_strand, \$cigar_line, \$ditag_side,
418  \$ditag_pair_id, \$tag_count );
419 
420  my @ditag_features;
421  my $dest_slice_start;
422  my $dest_slice_end;
423  my $dest_slice_strand;
424  if($dest_slice) {
425  $dest_slice_start = $dest_slice->start();
426  $dest_slice_end = $dest_slice->end();
427  $dest_slice_strand = $dest_slice->strand();
428  }
429 
430  while ( $sth->fetch ) {
431  my $analysis_obj = $self->db->get_AnalysisAdaptor->fetch_by_dbID($analysis_id);
432  my $slice = $self->db->get_SliceAdaptor->fetch_by_seq_region_id($seqreg);
433 
434  if($dest_slice) {
435  if($dest_slice_start != 1 || $dest_slice_strand != 1) {
436  if($dest_slice_strand == 1) {
437  $seqstart = $seqstart - $dest_slice_start + 1;
438  $seqend = $seqend - $dest_slice_start + 1;
439  } else {
440  my $tmp_seq_region_start = $seqstart;
441  $seqstart = $dest_slice_end - $seqend + 1;
442  $seqend = $dest_slice_end - $tmp_seq_region_start + 1;
443  $strand *= -1;
444  }
445  }
446 
447  my $seq_region_len = $dest_slice->seq_region_length();
448 
449  if ($dest_slice_strand == 1) { # Positive strand
450  $seqstart = $seqstart - $dest_slice_start + 1;
451  $seqend = $seqend - $dest_slice_start + 1;
452 
453  if ($dest_slice->is_circular()) { # Handle cicular chromosomes
454  if ($seqstart > $seqend) { # Looking at a feature overlapping the chromsome origin
455  if ($seqend > $dest_slice_start) {
456  # Looking at the region in the beginning of the chromosome.
457  $seqstart -= $seq_region_len;
458  }
459 
460  if ($seqend < 0) {
461  $seqend += $seq_region_len;
462  }
463  } else {
464  if ($dest_slice_start > $dest_slice_end && $seqend < 0) {
465  # Looking at the region overlapping the chromosome origin and
466  # a feature which is at the beginning of the chromosome.
467  $seqstart += $seq_region_len;
468  $seqend += $seq_region_len;
469  }
470  }
471  }
472  } else { # Negative strand
473  my $start = $dest_slice_end - $seqend + 1;
474  my $end = $dest_slice_end - $seqstart + 1;
475 
476  if ($dest_slice->is_circular()) {
477  if ($dest_slice_start > $dest_slice_end) {
478  # slice spans origin or replication
479  if ($seqstart >= $dest_slice_start) {
480  $end += $seq_region_len;
481  $start += $seq_region_len
482  if $seqend > $dest_slice_start;
483 
484  } elsif ($seqstart <= $dest_slice_end) {
485  # do nothing
486  } elsif ($seqend >= $dest_slice_start) {
487  $start += $seq_region_len;
488  $end += $seq_region_len;
489  } elsif ($seqend <= $dest_slice_end) {
490  $end += $seq_region_len
491  if $end < 0;
492  } elsif ($seqstart > $seqend) {
493  $end += $seq_region_len;
494  } else { }
495  } else {
496  if ($seqstart <= $dest_slice_end and $seqend >= $dest_slice_start) {
497  # do nothing
498  } elsif ($seqstart > $seqend) {
499  if ($seqstart <= $dest_slice_end) {
500  $start -= $seq_region_len;
501  } elsif ($seqend >= $dest_slice_start) {
502  $end += $seq_region_len;
503  } else { }
504  }
505  }
506  }
507 
508  $seqstart = $start;
509  $seqend = $end;
510  $strand *= -1;
511  }
512 
513  $slice = $dest_slice;
514  }
515 
516  push @ditag_features,
517  Bio::EnsEMBL::Map::DitagFeature->new( -dbid => $tag_id,
518  -slice => $slice,
519  -start => $seqstart,
520  -end => $seqend,
521  -strand => $strand,
522  -analysis => $analysis_obj,
523  -hit_start => $hit_start,
524  -hit_end => $hit_end,
525  -hit_strand => $hit_strand,
526  -ditag_id => $mothertag_id,
527  -cigar_line => $cigar_line,
528  -ditag_side => $ditag_side,
529  -ditag_pair_id => $ditag_pair_id,
530  -ditag => undef,
531  -tag_count => $tag_count,
532  -adaptor => $self,
533  );
534  }
535 
536  return \@ditag_features;
537 }
538 
539 
540 =head2 sequence
541 
542  Arg [1] : dbID of DitagFeature
543  Example : $ditagfeature_adaptor->get_sequence($ditagFeature->dbID)
544  Description: get the part of the sequence of a ditag,
545  that is actully aligned to the genome.
546  Returntype : string
547  Exceptions : thrown if not all data needed for storing is populated in the
548  ditag features
550  Status : At Risk
551 
552 =cut
553 
554 sub sequence {
555  my ($self, $dbID) = @_;
556 
557  my $sequence = undef;
558  my $db = $self->db() or throw "Couldn t get database connection.";
559  my $sql = "SELECT d.sequence, df.hit_start, df.hit_end, df.hit_strand ".
560  "FROM ditag d, ditag_feature df ".
561  "WHERE df.ditag_id=d.ditag_id and df.ditag_feature_id = ?";
562  my $sth = $db->dbc->prepare($sql);
563  $sth->execute( $dbID );
564  my ($seq, $start, $end, $strand) = $sth->fetchrow_array();
565  if($seq and $start and $end and $strand){
566  $sequence = substr($seq, ($start-1), ($end-$strand));
567  if($strand == -1) {
568  $sequence =~ tr/acgtrymkswhbvdnxACGTRYMKSWHBVDNX/tgcayrkmswdvbhnxTGCAYRKMSWDVBHNX/;
569  }
570  }
571 
572  return $sequence;
573 }
574 
575 
576 =head2 store
577 
578  Arg [1] : (Array ref of) Bio::EnsEMBL::Map::DitagFeature
579  Example : $ditagfeature_adaptor->store(@ditag_features);
580  Description: Stores a single ditagFeature or
581  a list of ditagFeatures in this database.
582  Returntype : none
583  Exceptions : thrown if not all data needed for storing is populated in the
584  ditag features
585  Caller : general
586  Status : At Risk
587 
588 =cut
589 
590 sub store {
591  my ( $self, $ditag_features ) = @_;
592 
593  if ( ref $ditag_features eq 'ARRAY' ) {
594  if ( scalar(@$ditag_features) == 0 ) {
595  throw( "Must call store with ditag_feature or list ref of ditags_features" );
596  }
597  } elsif ($ditag_features) {
598  my @ditag_features;
599  push @ditag_features, $ditag_features;
600  $ditag_features = \@ditag_features;
601  } else {
602  throw( "Must call store with ditag_feature or list ref of ditag_features." );
603  }
604 
605  my $db = $self->db() or throw "Couldn t get database connection.";
606 
607  my $sth1 = $self->prepare( "INSERT INTO ditag_feature( ditag_id, seq_region_id, seq_region_start,
608  seq_region_end, seq_region_strand, analysis_id, hit_start, hit_end,
609  hit_strand, cigar_line, ditag_side, ditag_pair_id )
610  VALUES( ?,?,?,?,?,?,?,?,?,?,?,? )" );
611  my $sth2 = $self->prepare( "INSERT INTO ditag_feature( ditag_feature_ID, ditag_id, seq_region_id,
612  seq_region_start, seq_region_end, seq_region_strand, analysis_id, hit_start,
613  hit_end, hit_strand, cigar_line, ditag_side, ditag_pair_id )
614  VALUES( ?,?,?,?,?,?,?,?,?,?,?,?,? )" );
615 # my $sth3 = $self->prepare( "SELECT COUNT(*) FROM ditag_feature
616 # WHERE ditag_id = ?" );
617 
618 TAG:
619  foreach my $ditag_feature (@$ditag_features) {
620 
621  if ( !ref $ditag_feature || !$ditag_feature->isa("Bio::EnsEMBL::Map::DitagFeature") ) {
622  throw( "Object must be an Ensembl DitagFeature, "
623  . "not a " . ref($ditag_feature) );
624  }
625  if ( $ditag_feature->is_stored($db) ) {
626  warning( "DitagFeature " . $ditag_feature->dbID .
627  " is already stored in this database,".
628  " maybe you need to use the update() method?" );
629  next TAG;
630  }
631  if(!$ditag_feature->ditag_id or !($self->db->get_DitagAdaptor->fetch_by_dbID($ditag_feature->ditag_id))){
632  throw("DitagFeature must be supplied with the id of a corresponding Ditag object.");
633  }
634  if(!$ditag_feature->ditag or !$ditag_feature->ditag->isa("Bio::EnsEMBL::Map::Ditag")){
635  throw("DitagFeature must be linked to a valid Ditag object.");
636  }
637 
638 
639 # #check if more than x tags with this ditag id exist
640 # $sth3->execute( $ditag_feature->ditag_id );
641 # my ($num) = $sth3->fetchrow_array();
642 # if ( ($num) and ($num > 1) ) {
643 # warning( "There are already at least 2 DitagFeatures relating to Ditag ".
644 # $ditag->ditag_id." stored in this database." );
645 # if ( $num > 4 ) {
646 # warning( "not storing" );
647 # next TAG;
648 # }
649 # }
650 
651  if ( $ditag_feature->dbID ) {
652  $sth2->bind_param( 1, $ditag_feature->dbID, SQL_INTEGER );
653  $sth2->bind_param( 2, $ditag_feature->ditag_id, SQL_INTEGER );
654  $sth2->bind_param( 3, ($ditag_feature->slice->get_seq_region_id), SQL_INTEGER );
655  $sth2->bind_param( 4, $ditag_feature->start, SQL_INTEGER );
656  $sth2->bind_param( 5, $ditag_feature->end, SQL_INTEGER );
657  $sth2->bind_param( 6, $ditag_feature->strand, SQL_VARCHAR );
658  $sth2->bind_param( 7, $ditag_feature->analysis->dbID, SQL_INTEGER );
659  $sth2->bind_param( 8, $ditag_feature->hit_start, SQL_INTEGER );
660  $sth2->bind_param( 9, $ditag_feature->hit_end, SQL_INTEGER );
661  $sth2->bind_param( 10, $ditag_feature->hit_strand, SQL_VARCHAR );
662  $sth2->bind_param( 11, $ditag_feature->cigar_line, SQL_VARCHAR );
663  $sth2->bind_param( 12, $ditag_feature->ditag_side, SQL_VARCHAR );
664  $sth2->bind_param( 13, $ditag_feature->ditag_pair_id, SQL_VARCHAR );
665  $sth2->execute();
666  }
667  else{
668  $sth1->bind_param( 1, $ditag_feature->ditag_id, SQL_INTEGER );
669  $sth1->bind_param( 2, ($ditag_feature->slice->get_seq_region_id), SQL_INTEGER );
670  $sth1->bind_param( 3, $ditag_feature->start, SQL_INTEGER );
671  $sth1->bind_param( 4, $ditag_feature->end, SQL_INTEGER );
672  $sth1->bind_param( 5, $ditag_feature->strand, SQL_VARCHAR );
673  $sth1->bind_param( 6, $ditag_feature->analysis->dbID, SQL_INTEGER );
674  $sth1->bind_param( 7, $ditag_feature->hit_start, SQL_INTEGER );
675  $sth1->bind_param( 8, $ditag_feature->hit_end, SQL_INTEGER );
676  $sth1->bind_param( 9, $ditag_feature->hit_strand, SQL_VARCHAR );
677  $sth1->bind_param( 10, $ditag_feature->cigar_line, SQL_VARCHAR );
678  $sth1->bind_param( 11, $ditag_feature->ditag_side, SQL_VARCHAR );
679  $sth1->bind_param( 12, $ditag_feature->ditag_pair_id, SQL_VARCHAR );
680  $sth1->execute();
681  my $dbID = $self->last_insert_id('ditag_feature_id', undef, 'ditag_feature');
682  $ditag_feature->dbID($dbID);
683  $ditag_feature->adaptor($self);
684  }
685 
686  }
687 }
688 
689 
690 =head2 batch_store
691 
692  Arg [1] : (Array ref of) Bio::EnsEMBL::Map::DitagFeatures
693  Arg [2] : bool have_dbIDs
694  Example : $ditagfeature_adaptor->batch_store(\@ditag_features);
695  Description: Stores a list of ditagFeatures in this database.
696  DitagFeatures are expected to have no dbID yet unless flag "have_dbIDs" is true.
697  They are inserted in one combined INSERT for better performance.
698  Returntype : none
699  Exceptions : thrown if not all data needed for storing is given for the
700  ditag features
701  Caller : general
702  Status : At Risk
703 
704 =cut
705 
706 sub batch_store {
707  my ( $self, $ditag_features, $have_dbIDs ) = @_;
708 
709  my @good_ditag_features;
710  my ($sql, $sqladd);
711  my $inserts = 0;
712 
713  if ( ref $ditag_features eq 'ARRAY' ) {
714  if ( scalar(@$ditag_features) == 0 ) {
715  throw( "Must call store with ditag_feature or list ref of ditag_features." );
716  }
717  } elsif ($ditag_features) {
718  my @ditag_features;
719  push @ditag_features, $ditag_features;
720  $ditag_features = \@ditag_features;
721  } else {
722  throw( "Must call store with ditag_feature or list ref of ditag_features." );
723  }
724 
725  my $db = $self->db() or throw "Couldn t get database connection.";
726 
727  #check whether it s a DitagFeature object and is not stored already
728  foreach my $ditag_feature (@$ditag_features) {
729 
730  if ( !ref $ditag_feature || !$ditag_feature->isa("Bio::EnsEMBL::Map::DitagFeature") ) {
731  throw( "Object must be an Ensembl DitagFeature, "
732  . "not a " . ref($ditag_feature) );
733  }
734  if(!$ditag_feature->ditag_id or !($self->db->get_DitagAdaptor->fetch_by_dbID($ditag_feature->ditag_id))){
735  throw("DitagFeature must be supplied with the id of a corresponding Ditag object.");
736  }
737 
738  if(!$ditag_feature->ditag or !$ditag_feature->ditag->isa("Bio::EnsEMBL::Map::Ditag")){
739  throw("DitagFeature must be linked to a valid Ditag object.");
740  }
741  if ( $ditag_feature->is_stored($db) ) {
742  warning( "DitagFeature " . $ditag_feature->dbID
743  . " is already stored in this database." );
744  next;
745  }
746  push(@good_ditag_features, $ditag_feature);
747  }
748  $ditag_features = undef;
749 
750  #create batch INSERT
751  if($have_dbIDs){
752  $sql = "INSERT INTO ditag_feature ( ditag_feature_id, ditag_id, seq_region_id, seq_region_start, ".
753  "seq_region_end, seq_region_strand, analysis_id, hit_start, hit_end, ".
754  "hit_strand, cigar_line, ditag_side, ditag_pair_id ) VALUES ";
755  foreach my $ditag_feature (@good_ditag_features) {
756  $sqladd = "";
757  if($inserts){ $sqladd = ", " }
758  $sqladd .= "(". $ditag_feature->ditag_feature_id.", ".$ditag_feature->ditag_id.", ".
759  ($ditag_feature->slice->get_seq_region_id).", ". $ditag_feature->start.", ".
760  $ditag_feature->end.", '".$ditag_feature->strand."', ".$ditag_feature->analysis->dbID.", ".
761  $ditag_feature->hit_start.", ".$ditag_feature->hit_end.", '".$ditag_feature->hit_strand.
762  "', '".$ditag_feature->cigar_line."', '".$ditag_feature->ditag_side."', ".
763  $ditag_feature->ditag_pair_id.")";
764  $sql .= $sqladd;
765  $inserts++;
766  }
767  }
768  else{
769  $sql = "INSERT INTO ditag_feature ( ditag_id, seq_region_id, seq_region_start, ".
770  "seq_region_end, seq_region_strand, analysis_id, hit_start, hit_end, ".
771  "hit_strand, cigar_line, ditag_side, ditag_pair_id ) VALUES ";
772  foreach my $ditag_feature (@good_ditag_features) {
773  $sqladd = "";
774  if($inserts){ $sqladd = ", " }
775  $sqladd .= "(". $ditag_feature->ditag_id.", ".($ditag_feature->slice->get_seq_region_id).", ".
776  $ditag_feature->start.", ".$ditag_feature->end.", '".$ditag_feature->strand."', ".
777  $ditag_feature->analysis->dbID.", ".$ditag_feature->hit_start.", ".$ditag_feature->hit_end.
778  ", '".$ditag_feature->hit_strand."', '".$ditag_feature->cigar_line."', '".
779  $ditag_feature->ditag_side."', ".$ditag_feature->ditag_pair_id.")";
780  $sql .= $sqladd;
781  $inserts++;
782  }
783  }
784 
785  #STORE
786  if($inserts){
787  print STDERR "\nHave $inserts Features.\n";
788  eval{
789  $db->dbc->do($sql);
790  };
791  if($@){
792  warning("Problem inserting ditag feature batch!".$@."\n");
793  }
794  }
795  else{
796  warn "Nothing stored!";
797  }
798 
799 }
800 
801 
802 =head2 update
803 
804  Arg [1] : ditagFeature to update
805  Description: update an existing ditagFeature with new values
806  Returntype : 1 on success
807  Status : At Risk
808 
809 =cut
810 
811 sub update {
812  my ($self, $ditagFeature) = @_;
813 
814  my $sth = $self->prepare( "UPDATE ditag_feature
815  SET ditag_id=?, seq_region_id=?, seq_region_start=?, seq_region_end=?,
816  seq_region_strand=?, analysis_id=?, hit_start=?, hit_end=?, hit_strand=?,
817  cigar_line=?, ditag_side=?, ditag_pair_id=?
818  where ditag_feature_id=?;" );
819 
820  $sth->bind_param(1, $ditagFeature->ditag_id, SQL_INTEGER);
821  $sth->bind_param(1, $ditagFeature->seq_region_id, SQL_INTEGER);
822  $sth->bind_param(1, $ditagFeature->seq_region_start, SQL_INTEGER);
823  $sth->bind_param(1, $ditagFeature->seq_region_end, SQL_INTEGER);
824  $sth->bind_param(1, $ditagFeature->seq_region_strand, SQL_TINYINT);
825  $sth->bind_param(1, $ditagFeature->hit_start, SQL_INTEGER);
826  $sth->bind_param(1, $ditagFeature->hit_end, SQL_INTEGER);
827  $sth->bind_param(1, $ditagFeature->hit_strand, SQL_TINYINT);
828  $sth->bind_param(1, $ditagFeature->cigar_line, SQL_LONGVARCHAR);
829  $sth->bind_param(1, $ditagFeature->ditag_side, SQL_VARCHAR);
830  $sth->bind_param(1, $ditagFeature->ditag_pair_id, SQL_INTEGER);
831  $sth->bind_param(1, $ditagFeature->dbID, SQL_INTEGER);
832 
833  my $result =$sth->execute();
834 
835  return $result;
836 }
837 
838 
839 =head2 list_dbIDs
840 
841  Args : None
842  Example : my @feature_ids = @{$dfa->list_dbIDs()};
843  Description: Gets an array of internal IDs for all DitagFeature objects in
844  the current database.
845  Arg[1] : <optional> int. not 0 for the ids to be sorted by the seq_region.
846  Returntype : List of ints
847  Exceptions : None
848  Status : Stable
849 
850 =cut
851 
852 sub list_dbIDs {
853  my ($self, $ordered) = @_;
854 
855  return $self->_list_dbIDs('ditag_feature', undef, $ordered);
856 }
857 
858 1;
Bio::EnsEMBL::Map::DBSQL::DitagFeatureAdaptor
Definition: DitagFeatureAdaptor.pm:30
Bio::EnsEMBL::Map::DitagFeature::new
public Bio::EnsEMBL::Map::DitagFeature new()
Bio::EnsEMBL::Slice
Definition: Slice.pm:50
Bio::EnsEMBL::Map::DitagFeature
Definition: DitagFeature.pm:35
Bio::EnsEMBL::Map::DBSQL::DitagFeatureAdaptor::fetch_all_by_Slice
public Listref fetch_all_by_Slice()
Bio::EnsEMBL::DBSQL::BaseAdaptor
Definition: BaseAdaptor.pm:71
Bio::EnsEMBL::Map::Ditag
Definition: Ditag.pm:28
Bio::EnsEMBL::Utils::Exception
Definition: Exception.pm:68