Bio::EnsEMBL::ExternalData::FASTA FASTAAdaptor
SummaryIncluded librariesPackage variablesSynopsisDescriptionGeneral documentationMethods
Toolbar
WebCvsRaw content
Summary
FASTAAdaptor - DESCRIPTION of Object
  This object represents a database of fasta sequences.
Package variables
No package variables defined.
Included modules
Bio::EnsEMBL::DBSQL::BaseAdaptor
Bio::Root::Object
Bio::Seq
DBI
Inherit
Bio::EnsEMBL::DBSQL::BaseAdaptor
Synopsis
use Bio::EnsEMBL::DBSQL::DBAdaptor;
use Bio::EnsEMBL::ExternalData::FASTA::FASTAAdaptor;
$db = Bio::EnsEMBL::DBSQL::DBAdaptor->new(
-user => 'ensro',
-dbname => 'fasta_8_1',
-host => 'ecs3d',
-driver => 'mysql',
);
my $fasta_adtor = Bio::EnsEMBL::ExternalData::FASTA::FASTAAdaptor->new($db);
$seqobj = $fasta_adtor->fetch_fasta_by_id('AP000869.1'); # fasta id
Description
This module is an entry point into a database of fasta sequences,
The objects can only be read from the database, not written. (They are
loaded using a separate perl script).
Methods
_ensdb
No description
Code
create_fasta_tableDescriptionCode
delete_fasta_metadata
No description
Code
delete_fasta_record
No description
Code
drop_fasta_tableDescriptionCode
fetch_fasta_by_idDescriptionCode
fetch_fasta_table_metadataDescriptionCode
insert_fasta_metadata
No description
Code
insert_fasta_recordDescriptionCode
Methods description
create_fasta_tablecode    nextTop
 Title   : create_fasta_table
Usage : $db->create_fasta_table('foo');
Function:
Example :
Returns :
Args : id
drop_fasta_tablecodeprevnextTop
 Title   : drop_fasta_table
Usage : $db->drop_fasta_table('foo');
Function:
Example :
Returns :
Args : id
fetch_fasta_by_idcodeprevnextTop
 Title   : fetch_fasta_by_id
Usage : $db->fetch_fasta_by_id("my_table", 'AP000869.1');
Function:
Example :
Returns : a bioperl seq object, empty list otherwise
Args : id
fetch_fasta_table_metadatacodeprevnextTop
 Title   : fetch_fasta_table_metadata
Usage : $db->fetch_fasta_table_metadata('foo');
Function:
Example :
Returns :
Args : id
insert_fasta_recordcodeprevnextTop
 Title   : insert_fasta_record
Usage : $db->insert_fasta_record($tablename, $seq);
Function:
Example :
Returns :
Args : table, bioperl seq obj
Methods code
_ensdbdescriptionprevnextTop
sub _ensdb {
  my $self = shift;
  $self->{'_ensdb'} = shift if @_;
  return $self->{'_ensdb'};
}


# get/set handle on fasta database
#sub _fastadb {
# my $self = shift;
# $self->{'_fastadb'} = shift if @_;
# return $self->{'_fastadb'};
#}
# get/set handle on fasta database
#sub adaptor {
# my $self = shift;
# $self->{'_adaptor'} = shift if @_;
# return $self->{'_adaptor'};
#}
# set/get handle on fasta database
#sub _db_handle {
# my $self = shift;
# $self->{'_db_handle'} = shift if @_;
# return $self->{'_db_handle'};
#}
#sub DESTROY {
# my ($self) = @_;
# if( $self->{'_db_handle'} ) {
# $self->{'_db_handle'}->disconnect;
# $self->{'_db_handle'} = undef;
# }
#}
1;
}
create_fasta_tabledescriptionprevnextTop
sub create_fasta_table {
  my ($self, $id) = @_;
  
  my $SCHEMA =qq(
  CREATE TABLE $id (
    id int(10) unsigned NOT NULL auto_increment,
    name varchar(40) NOT NULL default '',  
    description varchar(255) NOT NULL default '',  
    sequence mediumtext NOT NULL,
    PRIMARY KEY  (id),
    UNIQUE KEY  (name)
  ) TYPE=MyISAM
  );
  
  my $meta = "${id}_meta";
  
  my $META =qq(
  CREATE TABLE $meta (
    db varchar(40) NOT NULL default '',  
    title varchar(255) NOT NULL default '',  
    description mediumtext NOT NULL,
    methods mediumtext NOT NULL,
    credits mediumtext NOT NULL,
    links mediumtext NOT NULL,
    PRIMARY KEY  (db)
  ) TYPE=MyISAM
  );

  my $sth = $self->prepare($SCHEMA);
  $sth->execute();
  $sth = $self->prepare($META);
  $sth->execute();
}
delete_fasta_metadatadescriptionprevnextTop
sub delete_fasta_metadata {
  my ($self, $id) = @_;

  my $meta = "${id}_meta";
  my $q  =qq( DELETE FROM $meta);
  
  my $sth = $self->prepare($q);
  $sth->execute();
  return();
}
delete_fasta_recorddescriptionprevnextTop
sub delete_fasta_record {
  my ($self, $table, $seq) = @_;
  
  my $id = $seq->id();

  my $sql =qq( DELETE FROM 
          $table
         WHERE 
           id="$id"
        );
  my $sth = $self->prepare($sql);
  $sth->execute();
}
drop_fasta_tabledescriptionprevnextTop
sub drop_fasta_table {
  my ($self, $id) = @_;
  
  my $sql =qq( DROP TABLE $id );

  eval {
    my $sth = $self->prepare($sql);
    $sth->execute();
  };
  $@ && warn ("Database error! $@\n") and return 0;

  $sql =qq( DROP TABLE ${id}_meta );

  eval {
    my $sth = $self->prepare($sql);
    $sth->execute();
  };
  $@ && warn ("Database error! $@\n") and return 0;
}
fetch_fasta_by_iddescriptionprevnextTop
sub fetch_fasta_by_id {
  my ($self, $table, $id) = @_; 
  warn join (" ",@_);
  return () if( "$id" eq '');
  my $q  =qq( SELECT name,description,sequence FROM $table WHERE name="$id" );
  
#  warn ("SQL: $q\n");
my $sth = $self->prepare($q); $sth->execute(); my $seq; my $rowhash = $sth->fetchrow_hashref(); if($sth->rows() > 0){ $seq = Bio::Seq->new( -id => $rowhash->{'name'}, -desc => $rowhash->{'description'}, -seq => $rowhash->{'sequence'}, ); return($seq); } # This is a "heuristic" search for the ID in the decription.
# Only executes if we can't find the ID.
# Wish the NCBI would produce parseable bloody fasta lines!
$q =qq( SELECT name,description,sequence FROM $table WHERE description like "%$id%"); # warn ("Second try for fasta for $q!\n");
$sth = $self->prepare($q); $sth->execute(); $rowhash = $sth->fetchrow_hashref(); if($sth->rows() > 0){ $seq = Bio::Seq->new( -id => $rowhash->{'name'}, -desc => $rowhash->{'description'}, -seq => $rowhash->{'sequence'}, ); return($seq); } return(); } # set/get handle on ensembl database
}
fetch_fasta_table_metadatadescriptionprevnextTop
sub fetch_fasta_table_metadata {
  my ($self, $id) = @_;

  my $meta = "${id}_meta";
  my $q  =qq( SELECT title,description,methods,credits,links FROM $meta);

  my $sth;
  my $rv;
  eval {
    $sth = $self->prepare($q);
    $rv = $sth->execute();
  };
  $@ && ( warn( $@ ) && return );
  if( $rv == 0 ){ warn( "$q returned no rows" ) }
  return $sth->fetchrow_hashref();
}
insert_fasta_metadatadescriptionprevnextTop
sub insert_fasta_metadata {
  my ($self,$table,$title,$desc,$methods,$credits,$links) = @_;
  
  my $meta = "${table}_meta";

  my $sql =qq( INSERT INTO $meta VALUES ("$table","$title","$desc","$methods","$credits","$links"));
        
  my $sth = $self->prepare($sql);
  $sth->execute();
}
insert_fasta_recorddescriptionprevnextTop
sub insert_fasta_record {
  my ($self, $table, $seq, $parser) = @_;
  if( $parser && ref($parser) ne "CODE" ){ die( "Parser not a ref" ) }
  if($parser){
    #print STDERR "BEFORE: $id ";
my $id = &$parser($seq->id()); $id || ( warn( "$seq has no parsable ID!" ) && return ); $seq->id($id); #print STDERR "AFTER: ",$seq->id(),"\n";
} my $desc = $seq->desc(); my $data = $seq->primary_seq->seq(); my $id = $seq->id(); my $sql =qq( INSERT INTO $table VALUES (NULL,"$id","$desc","$data") ); my $rv; eval{ my $sth = $self->prepare($sql); $rv = $sth->execute(); }; if($@){ warn("Error inserting record $id:\n$@\n"); } return $rv;
}
General documentation
CONTACTTop
 Tony Cox <Lavc@sanger.ac.uk>
APPENDIXTop
The rest of the documentation details each of the object methods. Internal methods are usually preceded with a _