| 1 | =head1 LICENSE | ||
| 2 | |||
| 3 | See the NOTICE file distributed with this work for additional information | ||
| 4 | regarding copyright ownership. | ||
| 5 | |||
| 6 | Licensed under the Apache License, Version 2.0 (the "License"); | ||
| 7 | you may not use this file except in compliance with the License. | ||
| 8 | You may obtain a copy of the License at | ||
| 9 | |||
| 10 | http://www.apache.org/licenses/LICENSE-2.0 | ||
| 11 | |||
| 12 | Unless required by applicable law or agreed to in writing, software | ||
| 13 | distributed under the License is distributed on an "AS IS" BASIS, | ||
| 14 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
| 15 | See the License for the specific language governing permissions and | ||
| 16 | limitations under the License. | ||
| 17 | |||
| 18 | =cut | ||
| 19 | |||
| 20 | |||
| 21 | =head1 CONTACT | ||
| 22 | |||
| 23 | Please email comments or questions to the public Ensembl | ||
| 24 | developers list at <http://lists.ensembl.org/mailman/listinfo/dev>. | ||
| 25 | |||
| 26 | Questions may also be sent to the Ensembl help desk at | ||
| 27 | <http://www.ensembl.org/Help/Contact>. | ||
| 28 | |||
| 29 | =cut | ||
| 30 | |||
| 31 | =head1 NAME | ||
| 32 | |||
| 33 | Bio::EnsEMBL::IdMapping::Archiver - create gene_archive and peptide_archive | ||
| 34 | |||
| 35 | =head1 SYNOPSIS | ||
| 36 | |||
| 37 | my $archiver = Bio::EnsEMBL::IdMapping::Archiver->new( | ||
| 38 | -LOGGER => $logger, | ||
| 39 | -CONF => $conf, | ||
| 40 | -CACHE => $cache | ||
| 41 | ); | ||
| 42 | |||
| 43 | # create gene and peptide archive | ||
| 44 | $archiver->create_archive($mapping_session_id); | ||
| 45 | |||
| 46 | # dump existing archive tables to file | ||
| 47 | my $num_entries = | ||
| 48 | $archiver->dump_table_to_file( 'source', 'gene_archive', | ||
| 49 | 'gene_archive_existing.txt', 1 ); | ||
| 50 | |||
| 51 | =head1 DESCRIPTION | ||
| 52 | |||
| 53 | This module creates the gene_archive and peptide_archive | ||
| 54 | tables. Data is written to a file as tab-delimited text for | ||
| 55 | loading into a MySQL database (this can be done manually, or using | ||
| 56 | StableIdmapper->upload_file_into_table()). | ||
| 57 | |||
| 58 | An archive entry for a given source gene is created if no target | ||
| 59 | gene exists, or if any of its transcripts or their translations | ||
| 60 | changed. Non-coding transcripts only have an entry in gene_archive (i.e. | ||
| 61 | without a corresponding peptide_archive entry). | ||
| 62 | |||
| 63 | =head1 METHODS | ||
| 64 | |||
| 65 | create_archive | ||
| 66 | dump_gene | ||
| 67 | dump_tuple | ||
| 68 | dump_nc_row | ||
| 69 | mapping_session_id | ||
| 70 | |||
| 71 | =cut | ||
| 72 | |||
| 73 | |||
| 74 | package Bio::EnsEMBL::IdMapping::Archiver; | ||
| 75 | $Bio::EnsEMBL::IdMapping::Archiver::VERSION = '114.0.0_50'; # TRIAL | ||
| 76 | $Bio::EnsEMBL::IdMapping::Archiver::VERSION = '114.0.050'; | ||
| 77 | use strict; | ||
| 78 | use warnings; | ||
| 79 | no warnings 'uninitialized'; | ||
| 80 | |||
| 81 | use Bio::EnsEMBL::IdMapping::BaseObject; | ||
| 82 | our @ISA = qw(Bio::EnsEMBL::IdMapping::BaseObject); | ||
| 83 | |||
| 84 | use Bio::EnsEMBL::Utils::Exception qw(throw warning); | ||
| 85 | use Bio::EnsEMBL::Utils::ScriptUtils qw(path_append); | ||
| 86 | use Digest::MD5 qw(md5_hex); | ||
| 87 | |||
| 88 | |||
| 89 | # instance variables | ||
| 90 | my $pa_id; | ||
| 91 | |||
| 92 | |||
| 93 | =head2 create_archive | ||
| 94 | |||
| 95 | Arg[1] : Int $mapping_session_id - the mapping_session_id for this run | ||
| 96 | Example : $archiver->create_archive($stable_id_mapper->mapping_session_id); | ||
| 97 | Description : Creates the gene_archive and peptide_archive tables and writes | ||
| 98 | the data to a tab-delimited file. The decision as to what to | ||
| 99 | archive is deferred to dump_gene(), see documentation there for | ||
| 100 | details. | ||
| 101 | Return type : none | ||
| 102 | Exceptions : Thrown on missing argument. | ||
| 103 | Caller : id_mapping.pl | ||
| 104 | Status : At Risk | ||
| 105 | : under development | ||
| 106 | |||
| 107 | =cut | ||
| 108 | |||
| 109 | sub create_archive { | ||
| 110 | my $self = shift; | ||
| 111 | my $mapping_session_id = shift; | ||
| 112 | |||
| 113 | # argument check | ||
| 114 | unless ($mapping_session_id) { | ||
| 115 | $self->logger->warning("No mapping_session_id set."); | ||
| 116 | } | ||
| 117 | |||
| 118 | $self->mapping_session_id($mapping_session_id); | ||
| 119 | |||
| 120 | # get filehandles to write gene and peptide archive | ||
| 121 | my $ga_fh = $self->get_filehandle('gene_archive_new.txt', 'tables'); | ||
| 122 | my $pa_fh = $self->get_filehandle('peptide_archive_new.txt', 'tables'); | ||
| 123 | |||
| 124 | # get the currently highest peptide_archive_id from the source db | ||
| 125 | my $s_dba = $self->cache->get_DBAdaptor('source'); | ||
| 126 | my $s_dbh = $s_dba->dbc->db_handle; | ||
| 127 | my $sql = qq(SELECT MAX(peptide_archive_id) FROM peptide_archive); | ||
| 128 | $pa_id = $self->fetch_value_from_db($s_dbh, $sql); | ||
| 129 | |||
| 130 | unless ($pa_id) { | ||
| 131 | $self->logger->warning("No max(peptide_archive_id) found in db.\n", 1); | ||
| 132 | $self->logger->info("That's ok if this is the first stable ID mapping for this species.\n", 1); | ||
| 133 | } | ||
| 134 | |||
| 135 | $pa_id++; | ||
| 136 | $self->logger->debug("Starting with peptide_archive_id $pa_id.\n"); | ||
| 137 | |||
| 138 | # lookup hash of target gene stable IDs | ||
| 139 | my %target_genes = map { $_->stable_id => $_ } | ||
| 140 | values %{ $self->cache->get_by_name("genes_by_id", 'target') }; | ||
| 141 | |||
| 142 | # loop over source genes and dump to archive (dump_gene() will decide whether | ||
| 143 | # to do full or partial dump) | ||
| 144 | foreach my $source_gene (values %{ $self->cache->get_by_name("genes_by_id", | ||
| 145 | 'source') }) { | ||
| 146 | |||
| 147 | $self->dump_gene($source_gene, $target_genes{$source_gene->stable_id}, | ||
| 148 | $ga_fh, $pa_fh); | ||
| 149 | } | ||
| 150 | |||
| 151 | close($ga_fh); | ||
| 152 | close($pa_fh); | ||
| 153 | } | ||
| 154 | |||
| 155 | |||
| 156 | =head2 dump_gene | ||
| 157 | |||
| 158 | Arg[1] : Bio::EnsEMBL::IdMapping::TinyGene $s_gene - source gene | ||
| 159 | Arg[2] : Bio::EnsEMBL::IdMapping::TinyGene $t_gene - target gene | ||
| 160 | Arg[3] : Filehandle $ga_fh - filehandle for writing gene_archive data | ||
| 161 | Arg[4] : Filehandle $pa_fh - filehandle for writing peptide_archive data | ||
| 162 | Example : my $target_gene = $gene_mappings{$source_gene->stable_id}; | ||
| 163 | $archiver->dump_gene($source_gene, $target_gene, $ga_fh, $pa_fh); | ||
| 164 | Description : Given a source gene, it will write a gene_achive and | ||
| 165 | peptide_achive entry for it if no target gene exists, or if any | ||
| 166 | of its transcripts or their translation changed. | ||
| 167 | Return type : none | ||
| 168 | Exceptions : none | ||
| 169 | Caller : create_archive() | ||
| 170 | Status : At Risk | ||
| 171 | : under development | ||
| 172 | |||
| 173 | =cut | ||
| 174 | |||
| 175 | sub dump_gene { | ||
| 176 | my ($self, $s_gene, $t_gene, $ga_fh, $pa_fh) = @_; | ||
| 177 | |||
| 178 | # private method, so no argument check done for performance reasons | ||
| 179 | |||
| 180 | # loop over source transcripts | ||
| 181 | foreach my $s_tr (@{ $s_gene->get_all_Transcripts }) { | ||
| 182 | |||
| 183 | my $changed_flag = 1; | ||
| 184 | my $t_tl; | ||
| 185 | my $s_tl = $s_tr->translation; | ||
| 186 | |||
| 187 | if ( $t_gene ) { | ||
| 188 | |||
| 189 | foreach my $t_tr (@{ $t_gene->get_all_Transcripts }) { | ||
| 190 | |||
| 191 | $t_tl = $t_tr->translation; | ||
| 192 | |||
| 193 | # If there is a translation, there should also be a target translation | ||
| 194 | # and both transcript and translation should have same stable id and version | ||
| 195 | if ( $s_tl ) { | ||
| 196 | |||
| 197 | if ( $t_tl ) { | ||
| 198 | |||
| 199 | if ( $s_tl->stable_id eq $t_tl->stable_id | ||
| 200 | and $s_tl->version == $t_tl->version | ||
| 201 | and $s_tr->stable_id eq $t_tr->stable_id | ||
| 202 | and $s_tr->version == $t_tr->version ) { | ||
| 203 | |||
| 204 | $changed_flag = 0; | ||
| 205 | |||
| 206 | } | ||
| 207 | } | ||
| 208 | } else { | ||
| 209 | |||
| 210 | # nothing changed if source transcript has a target transcript with same stable id and version | ||
| 211 | if ( $s_tr->stable_id eq $t_tr->stable_id | ||
| 212 | and $s_tr->version == $t_tr->version ) { | ||
| 213 | |||
| 214 | $changed_flag = 0; | ||
| 215 | |||
| 216 | } | ||
| 217 | } | ||
| 218 | } | ||
| 219 | } | ||
| 220 | |||
| 221 | if ($changed_flag) { | ||
| 222 | $self->dump_tuple($s_gene, $s_tr, $s_tl, $ga_fh, $pa_fh); | ||
| 223 | } | ||
| 224 | } | ||
| 225 | } | ||
| 226 | |||
| 227 | |||
| 228 | =head2 dump_tuple | ||
| 229 | |||
| 230 | Arg[1] : Bio::EnsEMBL::IdMapping::TinyGene $gene - gene to archive | ||
| 231 | Arg[2] : Bio::EnsEMBL::IdMapping::TinyTrancript $tr - its transcript | ||
| 232 | Arg[3] : Bio::EnsEMBL::IdMapping::TinyTranslation $tl - its translation | ||
| 233 | Arg[4] : Filehandle $ga_fh - filehandle for writing gene_archive data | ||
| 234 | Arg[5] : Filehandle $pa_fh - filehandle for writing peptide_archive data | ||
| 235 | Example : $archive->dump_tuple($s_gene, $s_tr, $s_tl, $ga_fh, $pa_fh); | ||
| 236 | Description : Writes entry lines for gene_archive and peptide_archive. | ||
| 237 | Return type : none | ||
| 238 | Exceptions : none | ||
| 239 | Caller : dump_gene() | ||
| 240 | Status : At Risk | ||
| 241 | : under development | ||
| 242 | |||
| 243 | =cut | ||
| 244 | |||
| 245 | sub dump_tuple { | ||
| 246 | my ($self, $gene, $tr, $tl, $ga_fh, $pa_fh) = @_; | ||
| 247 | |||
| 248 | # private method, so no argument check done for performance reasons | ||
| 249 | |||
| 250 | # gene archive | ||
| 251 | print $ga_fh join("\t", | ||
| 252 | $gene->stable_id, | ||
| 253 | $gene->version, | ||
| 254 | $tr->stable_id, | ||
| 255 | $tr->version | ||
| 256 | ); | ||
| 257 | |||
| 258 | print $ga_fh "\t"; | ||
| 259 | |||
| 260 | if ( $tl ) { | ||
| 261 | print $ga_fh join("\t", | ||
| 262 | $tl->stable_id, | ||
| 263 | $tl->version, | ||
| 264 | $pa_id, | ||
| 265 | $self->mapping_session_id | ||
| 266 | ); | ||
| 267 | |||
| 268 | # peptide archive | ||
| 269 | my $pep_seq = $tl->seq; | ||
| 270 | print $pa_fh join("\t", $pa_id, md5_hex($pep_seq), $pep_seq); | ||
| 271 | print $pa_fh "\n"; | ||
| 272 | # increment peptide_archive_id | ||
| 273 | $pa_id++; | ||
| 274 | } else { | ||
| 275 | print $ga_fh join ("\t", | ||
| 276 | '\N', | ||
| 277 | '\N', | ||
| 278 | '\N', | ||
| 279 | $self->mapping_session_id | ||
| 280 | ); | ||
| 281 | } | ||
| 282 | print $ga_fh "\n"; | ||
| 283 | } | ||
| 284 | |||
| 285 | |||
| 286 | =head2 mapping_session_id | ||
| 287 | |||
| 288 | Arg[1] : (optional) Int - mapping_session_id to set | ||
| 289 | Example : my $msi = $archiver->mapping_session_id; | ||
| 290 | Description : Getter/setter for mapping_session_id. | ||
| 291 | Return type : Int | ||
| 292 | Exceptions : none | ||
| 293 | Caller : create_archive() | ||
| 294 | Status : At Risk | ||
| 295 | : under development | ||
| 296 | |||
| 297 | =cut | ||
| 298 | |||
| 299 | sub mapping_session_id { | ||
| 300 | my $self = shift; | ||
| 301 | $self->{'_mapping_session_id'} = shift if (@_); | ||
| 302 | return $self->{'_mapping_session_id'}; | ||
| 303 | } | ||
| 304 | |||
| 305 | |||
| 306 | 1; | ||
| 307 |