Archive Ensembl HomeArchive Ensembl Home
ProjectedDBEntryWriter.pm
Go to the documentation of this file.
00001 #
00002 # You may distribute this module under the same terms as perl itself
00003 #
00004 
00005 =pod
00006 
00007 =head1 NAME
00008 
00009 Bio::EnsEMBL::Compara::Production::Projection::Writer::ProjectedDBEntryWriter
00010 
00011 =head1 DESCRIPTION
00012 
00013 This class will write the results of the given projections to a 
00014 database given at construction.
00015 
00016 =head1 CONTACT
00017 
00018 This modules is part of the EnsEMBL project (http://www.ensembl.org)
00019 
00020 Questions can be posted to the dev mailing list: dev@ensembl.org
00021 
00022 =cut
00023 
00024 package Bio::EnsEMBL::Compara::Production::Projection::Writer::ProjectedDBEntryWriter;
00025 
00026 use strict;
00027 use warnings;
00028 use Bio::EnsEMBL::Utils::Argument qw(rearrange);
00029 use Bio::EnsEMBL::Utils::Exception qw(throw);
00030 use Bio::EnsEMBL::Utils::Scalar qw(assert_ref);
00031 
00032 use base qw(Bio::EnsEMBL::Compara::Production::Projection::Writer::BaseWriter);
00033 
00034 =head2 new()
00035 
00036   Arg[-DBA]       : Bio::EnsEMBL::DBSQL::DBAdaptor; Assumed to be a DBA which 
00037                     can be written to. Required argument 
00038   Arg[-ANALYSIS]  : Bio::EnsEMBL::Analysis; analysis used to link DBEntries to
00039   Returntype      : Bio::EnsEMBL::Compara::Production::Projection::Writer::ProjectedDBEntryWriter
00040   Description     : New method used for a new instance of the given object. 
00041                     Required fields are indicated accordingly. Fields are 
00042                     specified using the Arguments syntax (case insensitive).
00043   Exceptions      : If the DBAdaptor was not defined 
00044   Status          : Stable
00045 
00046 =cut
00047 
00048 sub new {
00049   my ( $class, @args ) = @_;
00050   my $self = $class->SUPER::new(@args);
00051   my ( $dba, $analysis, ) = rearrange( [qw(dba analysis)], @args );
00052 
00053   assert_ref( $dba, 'Bio::EnsEMBL::DBSQL::DBAdaptor' );
00054   $self->{dba} = $dba;
00055   if(defined $analysis) {
00056     assert_ref( $analysis, 'Bio::EnsEMBL::Analysis' );
00057     $self->{analysis} = $analysis;
00058   }
00059 
00060   return $self;
00061 }
00062 
00063 =head2 dba()
00064 
00065   Description : Getter. Should be a write enabled connection
00066 
00067 =cut
00068 
00069 sub dba {
00070   my ($self) = @_;
00071   return $self->{dba};
00072 }
00073 
00074 =pod
00075 
00076 =head2 analysis()
00077 
00078   Description : Getter for the analysis to write  
00079   Returntype  : Bio::EnsEMBL::Analysis or undef
00080   Exceptions  : None
00081   Status      : Stable
00082 
00083 =cut
00084 
00085 sub analysis {
00086   my ($self) = @_;
00087   return $self->{analysis};
00088 }
00089 
00090 
00091 =head2 write_projection()
00092 
00093 Re-assigns the attacted DBEntry from the list of projections to the
00094 core database which was given to this object during construction. This method
00095 also stores the newly assigned DBEntry.
00096 
00097 =cut
00098 
00099 sub write_projection {
00100   my ($self, $p) = @_;
00101   my $db_entry_a = $self->dba()->get_DBEntryAdaptor();
00102   my $entry = $self->_process_entry($p);
00103   my ($object, $type) = $self->_to_ensembl_object($p->to());
00104   $object->add_DBEntry($entry);
00105   $db_entry_a->store($entry, $object->dbID(), $type);
00106   return;
00107 }
00108 
00109 =head2 _to_ensembl_object()
00110 
00111 Maps a member object to its Ensembl core object and returns the expected type
00112 for the DBEntryAdaptor to correctly write the xref back
00113 
00114 =cut
00115 
00116 sub _to_ensembl_object {
00117   my ($self, $member) = @_;
00118   my $source = $member->source_name();
00119   my $object;
00120   my $type;
00121   if($source eq 'ENSEMBLGENE') {
00122     ($object, $type) = ($member->get_Gene(), 'Gene');
00123   }
00124   elsif($source eq 'ENSEMBLTRANS') {
00125     ($object, $type) = ($member->get_Transcript(), 'Transcript');
00126   }
00127   elsif($source eq 'ENSEMBLPEP') {
00128     ($object, $type) = ($member->get_Translation(), 'Translation');
00129   }
00130   else {
00131     throw "Cannot understand how to write an Xref back for the source type $source";
00132   }
00133   return ($object, $type);
00134 }
00135 
00136 =head2 _process_entry()
00137 
00138 Performs processing which at the moment is concerned with 
00139 processing the Entry if it is an OntologyXref
00140 
00141 All entries are setup with a linkage type of IEA, info type of PROJECTION
00142 and a text description showing what species the linkage was from and
00143 the stable ID of the source object.
00144 
00145 =cut
00146 
00147 sub _process_entry {
00148   my ($self, $p) = @_;
00149   my $entry = $p->entry();
00150   return $entry unless $entry->isa('Bio::EnsEMBL::OntologyXref');
00151   
00152   my $from_species = $p->from()->genome_db()->name();
00153   my $from = $p->from()->stable_id();
00154   $entry->flush_linkage_types();
00155   $entry->add_linkage_type("IEA");
00156   my $txt = "from $from_species translation $from";
00157   $entry->info_type("PROJECTION");
00158   $entry->info_text($txt);
00159   $self->_add_analysis($entry);
00160   return $entry;
00161 }
00162 
00163 sub _add_analysis {
00164   my ($self, $entry) = @_;
00165   my $a = $self->analysis();
00166   if(defined $a) {
00167     $entry->analysis($a);
00168   }
00169   return;
00170 }
00171 
00172 1;