ChromosomeAdaptor.pm 9.14 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
#
# Ensembl module for Bio::EnsEMBL::DBSQL::ChromosomeAdaptor
#
# Cared for by Ewan Birney <birney@ebi.ac.uk>
#
# Copyright Ewan Birney
#
# You may distribute this module under the same terms as perl itself

# POD documentation - main docs before the code

=head1 NAME

Bio::EnsEMBL::DBSQL::ChromosomeAdaptor - DESCRIPTION of Object

=head1 SYNOPSIS

18 19
$chromosome_adaptor = $db_adaptor->get_ChromosomeAdaptor();
$chromosome = $chromosome_adaptor->fetch_by_chr_name('12');
20 21 22

=head1 DESCRIPTION

23
This is a database adaptor used to retrieve chromosome objects from a database.
24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45

=head1 AUTHOR - Ewan Birney

This modules is part of the Ensembl project http://www.ensembl.org

Email birney@ebi.ac.uk


=head1 APPENDIX

The rest of the documentation details each of the object methods. Internal methods are usually preceded with a _

=cut


# Let the code begin...


package Bio::EnsEMBL::DBSQL::ChromosomeAdaptor;
use vars qw(@ISA);
use strict;

46
# Object preamble - inherits from Bio::EnsEMBL::Root
47 48 49 50 51 52

use Bio::EnsEMBL::DBSQL::BaseAdaptor;
use Bio::EnsEMBL::Chromosome;

@ISA = qw(Bio::EnsEMBL::DBSQL::BaseAdaptor);

53
# new inherited from BaseAdaptor
54

55 56


57
=head2 fetch_by_dbID
58

59 60 61 62 63 64 65 66 67 68
  Arg [1]    : int $id 
               unique database identifier for chromosome to retrieve  
  Example    : my $chromosome = $chromosome_adaptor->fetch_by_dbID(1);
  Description: Retrieves a Chromosome object from the database using its
               unique identifier.  Note the the identifier is the dbID and
               does NOT correspond to the chromosome name.  dbID 1 does NOT
               necessarily correspond to chromosome '1'
  Returntype : Bio::EnsEMBL::Chromosome
  Exceptions : thrown if $id not defined
  Caller     : general
69 70 71

=cut

72
sub fetch_by_dbID {
73 74 75 76
  my ($self,$id) = @_;

  my $chr = (); 

77 78 79 80
  unless(defined $id) {
    $self->throw("Chromosome dbID argument required\n");
  }

81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99
  unless(defined $self->{'_chr_cache'} ) {
    $self->{'_chr_cache'} = {};
  }

  #
  # If there is not already a cached version of this chromosome pull it
  # from the database and add it to the cache.
  #
  unless($chr = $self->{'_chr_cache'}->{$id}) {
    my $sth = $self->prepare( "SELECT name, known_genes, unknown_genes, 
                                      snps, length
                               FROM chromosome
                               WHERE chromosome_id = $id" );
    $sth->execute();
    
    my($name, $known_genes, $unknown_genes, $snps, $length);
    $sth->bind_columns(\$name,\$known_genes,\$unknown_genes,\$snps,\$length); 
    $sth->fetch();
   
100

101 102 103 104
    if($@) {
      $self->throw("Could not create chromosome from dbID $id\n" .
		   "Exception: $@\n");
    }
105

106 107 108
    unless($name) {
      $self->throw("Could determine chromosome name from dbID $id\n");
    }
109

110
    $chr = new Bio::EnsEMBL::Chromosome( -adaptor => $self,
111
					 -chr_name => $name,
112 113 114 115
					 -chromosome_id => $id,
					 -known_genes => $known_genes,
					 -unknown_genes => $unknown_genes,
					 -snps => $snps,
David Barnwell's avatar
David Barnwell committed
116
					 '-length' => $length );
117

118 119 120 121
    $self->{'_chr_cache'}->{$id} = $chr;
  }

  return $chr;
122 123 124
}


125
=head2 fetch_by_chr_name
126

127 128 129 130 131 132 133
  Arg [1]    : string $chr_name
               the name of the chromosome to retrieve
  Example    : $chromosome = $chromosome_adaptor->fetch_by_chr_name('X');
  Description: Retrieves a chromosome object from the database using its name.
  Returntype : Bio::EnsEMBL::Chromosome
  Exceptions : none
  Caller     : general
134 135 136

=cut

137
sub fetch_by_chr_name{
138
   my ($self,$chr_name) = @_;
139

140
   #Convert the name to the dbID
141
   my $dbID = $self->get_dbID_by_chr_name($chr_name);
142 143 144 145 146

   unless(defined $dbID) {
     $self->warn("chromosome with name $chr_name not in database");
     return undef;
   } 
147 148
   
   return $self->fetch_by_dbID($dbID);
149 150
}

151 152 153 154 155 156

=head2 fetch_all

  Args       : none
  Example    : @chromosomes = $chromosome_adaptor->fetch_all(); 
  Description: Retrieves every chromosome object from the database.
Graham McVicker's avatar
Graham McVicker committed
157
  Returntype : listref of Bio::EnsEMBL::Chromosome
158 159 160 161 162
  Exceptions : none
  Caller     : general

=cut

163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180
sub fetch_all {
  my($self) = @_;
  
  my @chrs = (); 


    my $sth = $self->prepare( "SELECT chromosome_id, name, known_genes, 
                                      unknown_genes, snps, length
                               FROM chromosome" );
    $sth->execute();
    
    my($chromosome_id, $name, $known_genes, $unknown_genes, $snps, $length);
    $sth->bind_columns(\$chromosome_id,\$name,\$known_genes,
                       \$unknown_genes,\$snps,\$length); 

    while($sth->fetch()) {
   
    my $chr = new Bio::EnsEMBL::Chromosome( -adaptor => $self,
181
					 -chr_name => $name,
182 183 184 185
					 -chromosome_id => $chromosome_id,
					 -known_genes => $known_genes,
					 -unknown_genes => $unknown_genes,
					 -snps => $snps,
David Barnwell's avatar
David Barnwell committed
186
					 '-length' => $length );
187 188 189 190 191

    $self->{'_chr_cache'}->{$chromosome_id} = $chr;
    push @chrs, $chr;
  }

Graham McVicker's avatar
Graham McVicker committed
192
  return \@chrs;
193 194 195
}


196
=head2 get_dbID_by_chr_name
197

198 199 200 201 202 203 204 205 206 207 208 209
  Arg [1]    : string $chr_name
               the name of the chromosome whose dbID is wanted.
  Example    : $dbID = $chromosome_adaptor->fetch_by_dbID('X') 
  Description: Retrieves a unique database identifier for a chromosome
               using the chromosomes name.  It is not recommended that this
               method be used externally from ChromosomeAdaptor.  It should 
               probably be private and may be made private in the future. A
               better way to obtain a dbID is:
               $dbID = $chromosome_adaptor->fetch_by_chr_name('X')->dbID();
  Returntype : int
  Exceptions : none
  Caller     : Bio::EnsEMBL::ChromosomeAdaptor
210 211 212

=cut

213 214
sub get_dbID_by_chr_name {
  my ($self, $chr_name) = @_;
215

216 217
  unless (defined $self->{_chr_name_mapping}) {
    $self->{_chr_name_mapping} = {};
218

219 220 221 222 223 224 225 226 227
    #get the chromo names and ids from the database
    my $sth = $self->prepare('SELECT name, chromosome_id FROM chromosome');
    $sth->execute();
    
    #Construct the mapping of chromosome name to id
    while(my $a = $sth->fetchrow_arrayref()) {
      $self->{_chr_name_mapping}->{$a->[0]} = $a->[1];
    }
  }
228

229 230
  return $self->{_chr_name_mapping}->{$chr_name};
}    
231 232


Graham McVicker's avatar
Graham McVicker committed
233 234 235



236 237
=head2 get_landmark_MarkerFeatures

Graham McVicker's avatar
Graham McVicker committed
238 239
  Arg [1]    : none
  Example    : none
240
  Description: DEPRECATED use Slice::get_landmark_MarkerFeatures instead
Graham McVicker's avatar
Graham McVicker committed
241 242 243
  Returntype : none
  Exceptions : none
  Caller     : none
244 245 246 247

=cut

sub get_landmark_MarkerFeatures{
248 249
   my ($self,$chr_name,$glob) = @_;

250 251
   $self->warn("ChromosomeAdaptor::get_landmark_MarkerFeatures is deprecated " 
	       . "use Slice::get_landmark_MarkerFeatures instead\n");
252

253 254 255
   if( !defined $glob ) {
       $glob = 500000;
   }
256

257 258 259
   my $statement= " SELECT  chr_start,
			    chr_end,
			    chr_strand,
260
			    name 
Arne Stabenau's avatar
Arne Stabenau committed
261
		    FROM    landmark_marker 
262
		    WHERE   chr_name = '$chr_name'
Arne Stabenau's avatar
Arne Stabenau committed
263
		    ORDER BY chr_start
264
		";
265 266 267 268 269 270 271 272 273 274 275 276 277 278 279
   
   $statement =~ s/\s+/ /g;
   
   my $sth = $self->prepare($statement);
   $sth->execute;
   
   my ($start, $end, $strand, $name);
   
   my $analysis;
   my %analhash;
   
   $sth->bind_columns
       ( undef, \$start, \$end,  \$strand, \$name);
   
   my @out;
280
   my $prev;
281
   while( $sth->fetch ) {
282 283 284 285
       if( defined $prev && $prev->end + $glob > $start  && $prev->id eq $name ) {
           next;
       }

286 287 288 289 290 291
       my $sf = Bio::EnsEMBL::SeqFeature->new();
       $sf->start($start);
       $sf->end($end);
       $sf->strand($strand);
       $sf->id($name);
       push(@out,$sf);
292
       $prev = $sf;
293 294 295 296 297 298
   } 

   return @out;
}


299
=head2 get_landmark_MarkerFeatures_old
300

Graham McVicker's avatar
Graham McVicker committed
301 302
  Arg [1]    : none
  Example    : none
303
  Description: DEPRECATED do not use
Graham McVicker's avatar
Graham McVicker committed
304 305 306
  Returntype : none
  Exceptions : none
  Caller     : none
307 308 309

=cut

310 311
sub get_landmark_MarkerFeatures_old{
   my ($self,$chr_name) = @_;
312

313 314
   my $glob = 1000;
   $self->throw( "Method deprecated. " );
315

316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361
   return ();

#   my $statement= "   SELECT 
#                       IF     (sgp.raw_ori=1,(f.seq_start+sgp.chr_start-sgp.raw_start-1),
#                              (sgp.chr_start+sgp.raw_end-f.seq_end-1)),                                        
#                       IF     (sgp.raw_ori=1,(f.seq_end+sgp.chr_start-sgp.raw_start-1),
#                              (sgp.chr_start+sgp.raw_end-f.seq_start-1)), 
#                              f.score, 
#                       IF     (sgp.raw_ori=1,f.strand,(-f.strand)), 
#                              f.name, f.hstart, f.hend, 
#                              f.hid, f.analysis, c.name 
#                       FROM   contig_landmarkMarker c,
#                              static_golden_path sgp,
#                              feature f
#                       WHERE  f.contig = c.contig
#                       AND    f.hid=c.marker  
#                       AND    sgp.raw_id=f.contig 
#                       AND    sgp.chr_name='$chr_name'";
   
#   $statement =~ s/\s+/ /g;
   
#   my $sth = $self->prepare($statement);
#   $sth->execute;
   
#   my ($start, $end, $score, $strand, $hstart, 
#       $name, $hend, $hid, $analysisid,$synonym);
   
#   my $analysis;
#   my %analhash;
   
#   $sth->bind_columns
#       ( undef, \$start, \$end, \$score, \$strand, \$name, 
#	 \$hstart, \$hend, \$hid, \$analysisid,\$synonym);
   
#   my @out;
#   while( $sth->fetch ) {
#       my $sf = Bio::EnsEMBL::SeqFeature->new();
#       $sf->start($start);
#       $sf->end($end);
#       $sf->strand($strand);
#       $sf->id($synonym);
#       push(@out,$sf);
#   } 

#   return @out;
}
362