Archive Ensembl HomeArchive Ensembl Home
ProjectedDisplayXrefWriter.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::ProjectedDisplayXrefWriter
00010 
00011 =head1 DESCRIPTION
00012 
00013 This class will write the results of the given projections to a 
00014 database given at construction by setting the target Display Xref and 
00015 description if required.
00016 
00017 =head1 AUTHOR
00018 
00019 Andy Yates (ayatesatebiacuk)
00020 
00021 =head1 CONTACT
00022 
00023 This modules is part of the EnsEMBL project (http://www.ensembl.org)
00024 
00025 Questions can be posted to the dev mailing list: dev@ensembl.org
00026 
00027 =cut
00028 
00029 package Bio::EnsEMBL::Compara::Production::Projection::Writer::ProjectedDisplayXrefWriter;
00030 
00031 use strict;
00032 use warnings;
00033 use Bio::EnsEMBL::Utils::Argument qw(rearrange);
00034 use Bio::EnsEMBL::Utils::Scalar qw(assert_ref check_ref);
00035 
00036 use base qw(Bio::EnsEMBL::Compara::Production::Projection::Writer::BaseWriter);
00037 
00038 =head2 new()
00039 
00040   Arg[-dba]           : required; Assumed to be a DBA which we can write to
00041   Arg[-descriptions]  : If set to true will force the transfer of descriptions
00042                         and not just the display xref. Defaults to false
00043   Description : New method used for a new instance of the given object. 
00044                 Required fields are indicated accordingly. Fields are specified
00045                 using the Arguments syntax (case insensitive).
00046 
00047 =cut
00048 
00049 sub new {
00050   my ( $class, @args ) = @_;
00051   my $self = $class->SUPER::new(@args);
00052   my ( $dba, $descriptions) = rearrange( [qw(dba descriptions)], @args );
00053 
00054   assert_ref( $dba, 'Bio::EnsEMBL::DBSQL::DBAdaptor' );
00055   confess(
00056 'The attribute dba must be specified during construction or provide a builder subroutine'
00057   ) if !defined $dba;
00058   $self->{dba} = $dba if defined $dba;
00059   
00060   $descriptions = $self->_descriptions_builder() if ! defined $descriptions;
00061   $self->{descriptions} = $descriptions if defined $descriptions;
00062 
00063   return $self;
00064 }
00065 
00066 =head2 dba()
00067 
00068   Description : Getter. Should be a write enabled connection
00069 
00070 =cut
00071 
00072 sub dba {
00073   my ($self) = @_;
00074   return $self->{dba};
00075 }
00076 
00077 =head2 descriptions()
00078 
00079   Description : Getter. Returns if we want to propagate the descriptions or not
00080 
00081 =cut
00082 
00083 
00084 sub descriptions {
00085   my ($self) = @_;
00086   return $self->{descriptions};
00087 }
00088 
00089 =head2 _dbname_to_original_type()
00090 
00091   Arg[1]      : DBAdaptor; should point to the source of the Hash (normally
00092                 the source)
00093   Description : Getter. Returns what type of object to link the 
00094 
00095 =cut
00096 
00097 
00098 sub _dbname_to_original_type {
00099   my ($self, $dba) = @_;
00100   if(! exists $self->{dbname_to_original_type}) {
00101     $self->{dbname_to_original_type} = $self->_dbname_to_original_type_builder($dba);
00102   }
00103   return $self->{dbname_to_original_type};
00104 }
00105 
00106 sub _descriptions_builder {
00107   my ($self) = @_;
00108   return 0;
00109 }
00110 
00111 
00112 =head2 write_projection()
00113 
00114 Re-assigns the attacted DBEntry from the list of projections to the
00115 core database which was given to this object during construction. This method
00116 also stores the newly assigned DBEntry.
00117 
00118 =cut
00119 
00120 sub write_projection {
00121   my ($self, $p) = @_;
00122   my $db_entry_a  = $self->dba()->get_DBEntryAdaptor();
00123   my $gene_a      = $self->dba()->get_GeneAdaptor();
00124     
00125   #Do Processing
00126   $self->_process_entry($p);
00127   $self->_process_description($p);
00128   $self->_update_status($p->to()->get_Gene());
00129   
00130   #Store Xref on original type of object
00131   my ($type, $target_object) = $self->_type_and_object($p);
00132   $db_entry_a->store($p->entry(), $target_object, $type, 1);
00133   #Update gene now we have stored the DBEntry
00134   $gene_a->update($p->to()->get_Gene());
00135   
00136   return;
00137 }
00138 
00139 =head2 _process_entry()
00140 
00141 Sets up an Xref to be persisted back to the target core database for 
00142 later processing.
00143 
00144 =cut
00145 
00146 sub _process_entry {
00147   my ($self, $p) = @_;
00148   
00149   my $entry = $p->entry();
00150   my $from_gene = $p->from()->get_Gene();
00151   my $from_species = $p->from()->genome_db()->name();
00152   my $from_stable_id = $from_gene->stable_id();
00153   
00154   #Setup Xref
00155   my $info_txt = "from $from_species gene $from_stable_id";
00156   $entry->info_type('PROJECTION');
00157   $entry->info_text($info_txt);
00158   
00159   #Check for modification of display if we projected 1:m
00160   #Code taken from original projection code. Can only do this if
00161   #we were given a DisplayProjection object
00162   if(check_ref($p, 'Bio::EnsEMBL::Compara::Production::Projection::DisplayProjection') && $p->total() > 1) {
00163     my $total = $p->total();
00164     my $index = $p->current_index();
00165     my $tuple_txt = " ($index of $total)";
00166     my $existing = $entry->display_id();
00167     $existing =~ s/\(\d+ of \d+\)//;
00168     $entry->display_id($existing . $tuple_txt);
00169     $info_txt .= $tuple_txt;
00170   }
00171   
00172   #Assign it to the gene
00173   my $to_gene = $p->to()->get_Gene();
00174   $to_gene->display_xref($entry);
00175   
00176   return;
00177 }
00178 
00179 =head2 _process_description()
00180 
00181 Copies the description from a source to a target gene if allowed
00182 
00183 =cut
00184 
00185 sub _process_description {
00186   my ($self, $p) = @_;
00187   my $from_gene = $p->from()->get_Gene();
00188   my $to_gene = $p->to()->get_Gene();
00189   if($self->description()) {
00190     $to_gene->description($from_gene->description());
00191   }
00192   return;
00193 }
00194 
00195 =head2 _type_and_object()
00196 
00197 Performs the lookup of what the original entry was located on and then
00198 returns the object the DBEntry should be attached to and the type of
00199 object it is. We also allow for the mapping of those which are marked as
00200 HGNC*transcript or Gene to go to the correct object
00201 
00202 =cut
00203 
00204 sub _type_and_object {
00205   my ($self, $p) = @_;
00206   my $entry = $p->entry();
00207   my $dbname = $entry->dbname();
00208   my $to_gene = $p->to()->get_Gene();
00209   
00210   my $src_dba = $p->from()->genome_db()->db_adpator();
00211   my $dbname_to_type = $self->_dbname_to_original_type($src_dba);
00212   my $type = $dbname_to_type->{$entry->dbname()};
00213   
00214   my $target_object;
00215   if($type eq 'Gene' || $dbname =~ /HGNC_.*gene/) {
00216     $type = 'Gene';
00217     $target_object = $to_gene;
00218   }
00219   elsif($type eq 'Transcript' || $dbname =~ /HGNC_.*transcript/ ) {
00220     $type = 'Transcript';
00221     $target_object = $to_gene->canonical_transcript();
00222   }
00223   elsif($type eq 'Translation') {
00224     $target_object = $to_gene->canonical_transcript()->translation();
00225   }
00226   else {
00227     throw('Cannot handle Xrefs of the type '.$type);
00228   }
00229   return ($target_object, $type);
00230 }
00231 
00232 =head2 _update_status()
00233 
00234 Assigns everything to KNOWN_BY_PROJECTION for the given gene and transcripts
00235 
00236 =cut
00237 
00238 sub _update_status {
00239   my ($self, $gene) = @_;
00240   my $status = 'KNOWN_BY_PROJECTION';
00241   $gene->status($status);
00242   foreach my $t (@{$gene->get_all_Transcripts()}) {
00243     $t->status($status);
00244   }
00245   return;
00246 }
00247 
00248 sub _dbname_to_original_type_builder {
00249   my ($self, $dba) = @_;
00250   my $h = Bio::EnsEMBL::Utils::SqlHelper->new(-DB_CONNECTION => $dba->dbc());
00251   my $sql = <<SQL;
00252 SELECT DISTINCT e.db_name, ox.ensembl_object_type 
00253 FROM external_db e, xref x, object_xref ox 
00254 WHERE x.xref_id=ox.xref_id 
00255 AND e.external_db_id=x.external_db_id
00256 SQL
00257   return $h->execute_into_hash(-SQL => $sql);
00258 }
00259 
00260 1;