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