EdgeExpressDB

 view release on metacpan or  search on metacpan

lib/EEDB/Feature.pm  view on Meta::CPAN

  
  unless(defined($source) && $source->isa('EEDB::FeatureSource')) {
    die('second parameter [source] must be a EEDB::FeatureSource');
  }
  unless(defined($symbol) && $symbol->isa('EEDB::Symbol')) {
    die('third parameter [symbol] must be a EEDB::Symbol');
  }

  my $sql = sprintf("SELECT f.* FROM feature f ".
                    "JOIN feature_2_symbol using(feature_id)  ".
                    "WHERE symbol_id = %d ", 
                    $symbol->id);
  if(defined($source)) {
    $sql .= sprintf("AND feature_source_id=%d", $source->id);
  }
  $sql .= sprintf(" GROUP BY feature_id");
  if($response_limit) {
    $sql .= sprintf(" LIMIT %d", $response_limit);
  }

  #print($sql, "\n", );
  return $class->fetch_multiple($db, $sql);
}


#does a multi-symbol search, all symbols must be linked to the feature
sub fetch_all_with_symbols {
  my $class = shift;
  my $db = shift;
  my $source = shift; #FeatureSource object
  my @symbols = @_; #Symbol objects
  
  if(defined($source) && !($source->isa('EEDB::FeatureSource'))) {
    die('second parameter [source] must be a EEDB::FeatureSource');
  }
  
  my @symbol_ids;
  foreach my $symbol (@symbols) {
    unless(defined($symbol) && $symbol->isa('EEDB::Symbol')) {
      die("$symbol is not a EEDB::Symbol");
    }
    push @symbol_ids, $symbol->id;
  }
    
  my $sql = sprintf("SELECT f.* FROM feature f, ".
                    "(select feature_id, count(*) cnt from feature_2_symbol where symbol_id in(%s) ".
                    " group by feature_id)t where cnt=%d and f.feature_id = t.feature_id ", 
                   join(',', @symbol_ids),
                   scalar(@symbol_ids)
                   );
  if(defined($source)) {
    $sql .= sprintf(" AND feature_source_id=%d", $source->id);
  }

  #print($sql, "\n", );
  return $class->fetch_multiple($db, $sql);
}

###############################################################################################
#
# streaming API section
#
###############################################################################################


=head2 stream_all

  Description: stream all features out of database with a given set of source filters
  Arg (1)    : $database (MQdb::Database)
  Arg (2...) : hash named filter parameters. 
                 sources=>[$fsrc1, $fsrc2,$fsrc3],  instances of EEDB::FeatureSource
  Returntype : a DBStream instance
  Exceptions : none 

=cut

sub stream_all {
  my $class = shift;
  my $db = shift;  #database
  my %options = @_;  #like sources=>[$fsrc1, $fsrc2,$fsrc3]
    
  return [] unless($db);

  my @sources = @{$options{'sources'}} if($options{'sources'});
  
  my @fsrc_ids;
  foreach my $source (@sources) {
    next unless($source);
    if($source->class eq 'FeatureSource') { push @fsrc_ids, $source->id; }
  }
 
  my $sql = "SELECT * FROM feature f WHERE 1=1 ";
  if(@fsrc_ids) { $sql .= sprintf(" AND feature_source_id in(%s) ", join(',', @fsrc_ids)); }
  $sql .= " ORDER BY chrom_id, chrom_start, chrom_end, feature_id";
  #print($sql, "\n");
  return $class->stream_multiple($db, $sql);
}


=head2 stream_by_chrom

  Description     : uses an EEDB::Chrom object to stream all features in sorted order by chrom start,end
                    Useful for feeding features into EEDB::Tools::OverlapCompare2
  Arg [1]         : EEDB::Chrom object with database connection 
  Arg (2...)      : hash named filter parameters. 
                      sources=>[$fsrc1, $fsrc2,$fsrc3],  instances of EEDB::FeatureSource
  Returntype      : MQdb::DBStream object 
  Exceptions      : die if anything other than EEDB::FeatureSource(s) are passed in the optional parameter list
  Example         :   
                    my $chroms = EEDB::Chrom->fetch_all_by_assembly($assembly);
                    foreach my $chrom (@$chroms) {
                      my $stream  = EEDB::Feature->stream_by_chrom($chrom, sources=>[$fsrc1, $fsrc2]);
                      my $feature1 = $stream1->next_in_stream;
                      while($feature1) {
                        #do something
                        $feature1 = $stream1->next_in_stream;
                      }
                    }

=cut



( run in 0.746 second using v1.01-cache-2.11-cpan-140bd7fdf52 )