| 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::DBSQL::BiotypeAdaptor - An adaptor which performs database | ||
| 34 | interaction relating to the storage and retrieval of Biotypes | ||
| 35 | |||
| 36 | =head1 SYNOPSIS | ||
| 37 | |||
| 38 | my $biotype = $db_adaptor->fetch_by_name_object_type('protein_coding', 'gene'); | ||
| 39 | |||
| 40 | =head1 DESCRIPTION | ||
| 41 | |||
| 42 | This adaptor provides a means to retrieve and store information related | ||
| 43 | to Biotypes. Primarily this involves the retrieval or storage of | ||
| 44 | Bio::EnsEMBL::Biotype objects from a database. | ||
| 45 | |||
| 46 | See Bio::EnsEMBL::Biotype for details of the Biotype class. | ||
| 47 | |||
| 48 | =head1 METHODS | ||
| 49 | |||
| 50 | =cut | ||
| 51 | |||
| 52 | package Bio::EnsEMBL::DBSQL::BiotypeAdaptor; | ||
| 53 | $Bio::EnsEMBL::DBSQL::BiotypeAdaptor::VERSION = '114.0.0_50'; # TRIAL | ||
| 54 | $Bio::EnsEMBL::DBSQL::BiotypeAdaptor::VERSION = '114.0.050'; | ||
| 55 | use Bio::EnsEMBL::DBSQL::BaseAdaptor; | ||
| 56 | use Bio::EnsEMBL::Utils::Exception qw(throw deprecate warning); | ||
| 57 | use Bio::EnsEMBL::Biotype; | ||
| 58 | |||
| 59 | use strict; | ||
| 60 | use warnings; | ||
| 61 | |||
| 62 | use parent qw(Bio::EnsEMBL::DBSQL::BaseAdaptor); | ||
| 63 | |||
| 64 | =head2 _tables | ||
| 65 | |||
| 66 | Arg [1] : none | ||
| 67 | Description: PROTECTED implementation of superclass abstract method. | ||
| 68 | Returns the names, aliases of the tables to use for queries. | ||
| 69 | Returntype : list of arrays of strings | ||
| 70 | Exceptions : none | ||
| 71 | |||
| 72 | =cut | ||
| 73 | |||
| 74 | sub _tables { | ||
| 75 | my $self = shift; | ||
| 76 | |||
| 77 | return (['biotype', 'b']); | ||
| 78 | } | ||
| 79 | |||
| 80 | =head2 _columns | ||
| 81 | |||
| 82 | Arg [1] : none | ||
| 83 | Example : none | ||
| 84 | Description: PROTECTED implementation of superclass abstract method. | ||
| 85 | Returns a list of columns to use for queries. | ||
| 86 | Returntype : list of strings | ||
| 87 | Exceptions : none | ||
| 88 | |||
| 89 | =cut | ||
| 90 | |||
| 91 | sub _columns { | ||
| 92 | my $self = shift; | ||
| 93 | |||
| 94 | return ('b.biotype_id', 'b.name', 'b.object_type', 'b.db_type', 'b.attrib_type_id', 'b.description', 'b.biotype_group', 'b.so_acc', 'b.so_term'); | ||
| 95 | } | ||
| 96 | |||
| 97 | =head2 _objs_from_sth | ||
| 98 | |||
| 99 | Arg [1] : StatementHandle $sth | ||
| 100 | Example : none | ||
| 101 | Description: PROTECTED implementation of abstract superclass method. | ||
| 102 | responsible for the creation of ProteinFeatures | ||
| 103 | Returntype : arrayref of Bio::EnsEMBL::Biotype objects | ||
| 104 | Exceptions : none | ||
| 105 | |||
| 106 | =cut | ||
| 107 | |||
| 108 | sub _objs_from_sth { | ||
| 109 | my ($self, $sth) = @_; | ||
| 110 | |||
| 111 | my ($dbID, $name, $object_type, $db_type, $attrib_type_id, $description, $biotype_group, $so_acc, $so_term); | ||
| 112 | |||
| 113 | $sth->bind_columns(\$dbID, \$name, \$object_type, \$db_type, \$attrib_type_id, \$description, \$biotype_group, \$so_acc, \$so_term); | ||
| 114 | |||
| 115 | my @biotypes; | ||
| 116 | |||
| 117 | while($sth->fetch()) { | ||
| 118 | push( @biotypes, | ||
| 119 | my $feat = Bio::EnsEMBL::Biotype->new_fast( { | ||
| 120 | 'dbID' => $dbID, | ||
| 121 | 'name' => $name, | ||
| 122 | 'object_type' => $object_type, | ||
| 123 | 'db_type' => $db_type, | ||
| 124 | 'attrib_type_id' => $attrib_type_id, | ||
| 125 | 'description' => $description, | ||
| 126 | 'biotype_group' => $biotype_group, | ||
| 127 | 'so_acc' => $so_acc, | ||
| 128 | 'so_term' => $so_term, | ||
| 129 | } ) | ||
| 130 | ); | ||
| 131 | } | ||
| 132 | |||
| 133 | return \@biotypes; | ||
| 134 | } | ||
| 135 | |||
| 136 | |||
| 137 | =head2 fetch_by_name_object_type | ||
| 138 | |||
| 139 | Arg [1] : String $name | ||
| 140 | The name of the biotype to retrieve | ||
| 141 | Arg [2] : String $object_type | ||
| 142 | The object type of the biotype to retrieve (gene or transcript) | ||
| 143 | Example : $biotype = $biotype_adaptor->fetch_by_name_object_type('mRNA', 'gene'); | ||
| 144 | Description: Retrieves a biotype object from the database via its combined key (name, object_type). | ||
| 145 | If the Biotype requested does not exist in the database, a new Biotype object is | ||
| 146 | created with the provided name and object_type to be returned. | ||
| 147 | Returntype : Bio::EnsEMBL::Biotype | ||
| 148 | Exceptions : none | ||
| 149 | |||
| 150 | =cut | ||
| 151 | |||
| 152 | sub fetch_by_name_object_type { | ||
| 153 | my ($self, $name, $object_type) = @_; | ||
| 154 | |||
| 155 | my $biotype; | ||
| 156 | |||
| 157 | # biotype table implemented on schema_version 93 | ||
| 158 | if ($self->schema_version > 92) { | ||
| 159 | my $constraint = "b.name = ? AND b.object_type = ?"; | ||
| 160 | $self->bind_param_generic_fetch($name, SQL_VARCHAR); | ||
| 161 | $self->bind_param_generic_fetch($object_type, SQL_VARCHAR); | ||
| 162 | $biotype = shift @{$self->generic_fetch($constraint)}; | ||
| 163 | } | ||
| 164 | |||
| 165 | # If request biotype does not exist in the table | ||
| 166 | # create a new biotype object containing name and object_type only | ||
| 167 | # this is required by genebuild in pipelines | ||
| 168 | if (!defined $biotype) { | ||
| 169 | $biotype = Bio::EnsEMBL::Biotype->new( | ||
| 170 | -NAME => $name, | ||
| 171 | -OBJECT_TYPE => $object_type, | ||
| 172 | ) | ||
| 173 | } | ||
| 174 | |||
| 175 | return $biotype; | ||
| 176 | } | ||
| 177 | |||
| 178 | =head2 fetch_all_by_object_type | ||
| 179 | |||
| 180 | Arg [1] : String $object_type | ||
| 181 | The object_type of the biotypes to retrieve (gene or transcript). | ||
| 182 | Example : $biotypes = $biotype_adaptor->fetch_all_by_object_type('gene'); | ||
| 183 | Description: Retrieves an array reference of biotype objects from the database. | ||
| 184 | Returntype : arrayref of Bio::EnsEMBL::Biotype objects or empty arrayref | ||
| 185 | Warning : If empty arrayref is to be returned | ||
| 186 | Exceptions : none | ||
| 187 | |||
| 188 | =cut | ||
| 189 | |||
| 190 | sub fetch_all_by_object_type { | ||
| 191 | my ($self, $object_type) = @_; | ||
| 192 | |||
| 193 | my $constraint = "b.object_type = ?"; | ||
| 194 | $self->bind_param_generic_fetch($object_type, SQL_VARCHAR); | ||
| 195 | my @biotypes = @{$self->generic_fetch($constraint)}; | ||
| 196 | |||
| 197 | if ( !@biotypes ) { | ||
| 198 | warning("No objects retrieved. Check if object_type '$object_type' is correct.") | ||
| 199 | } | ||
| 200 | |||
| 201 | return \@biotypes; | ||
| 202 | } | ||
| 203 | |||
| 204 | =head2 fetch_all_by_name | ||
| 205 | |||
| 206 | Arg [1] : String $name | ||
| 207 | The name of the biotype to retrieve | ||
| 208 | Arg [2] : (optional) String $object_type | ||
| 209 | The object_type of the biotypes to retrieve (gene or transcript). | ||
| 210 | Example : $biotypes = $biotype_adaptor->fetch_all_by_name('lincRNA'); | ||
| 211 | Description: Retrieves an array reference of biotype objects from the database. | ||
| 212 | Returntype : arrayref of Bio::EnsEMBL::Biotype objects or empty arrayref | ||
| 213 | Warning : If empty arrayref is to be returned | ||
| 214 | Exceptions : none | ||
| 215 | |||
| 216 | =cut | ||
| 217 | |||
| 218 | sub fetch_all_by_name{ | ||
| 219 | my ($self, $name, $object_type) = @_; | ||
| 220 | |||
| 221 | my $constraint = "b.name = ?"; | ||
| 222 | $self->bind_param_generic_fetch($name, SQL_VARCHAR); | ||
| 223 | if (defined $object_type) { | ||
| 224 | $constraint .= "AND b.object_type = ?"; | ||
| 225 | $self->bind_param_generic_fetch($object_type, SQL_VARCHAR); | ||
| 226 | } | ||
| 227 | my @biotypes = @{$self->generic_fetch($constraint)}; | ||
| 228 | |||
| 229 | if ( !@biotypes ) { | ||
| 230 | warning("No objects retrieved. Check if name '$name' is correct.") | ||
| 231 | } | ||
| 232 | |||
| 233 | return \@biotypes; | ||
| 234 | } | ||
| 235 | |||
| 236 | =head2 fetch_all_by_group_object_db_type | ||
| 237 | |||
| 238 | Arg [1] : String $biotype_group | ||
| 239 | The group of the biotypes to retrieve | ||
| 240 | Arg [2] : String $object_type | ||
| 241 | The object type of the biotypes to retrieve (gene or transcript) | ||
| 242 | Arg [3] : (optional) String $db_type | ||
| 243 | The db_type of the biotypes to retrieve. If not provided defaults to 'core'. | ||
| 244 | Example : $biotype = $biotype_adaptor->fetch_all_by_group_object_db_type('coding', 'gene'); | ||
| 245 | Description: Retrieves an array reference of biotype objects from the database of the provided | ||
| 246 | biotype_group and object_type and core db_type. | ||
| 247 | Returntype : arrayref of Bio::EnsEMBL::Biotype objects or empty arrayref | ||
| 248 | Warning : If empty arrayref is to be returned | ||
| 249 | Exceptions : none | ||
| 250 | |||
| 251 | =cut | ||
| 252 | |||
| 253 | sub fetch_all_by_group_object_db_type { | ||
| 254 | my ($self, $biotype_group, $object_type, $db_type) = @_; | ||
| 255 | |||
| 256 | $db_type //= 'core'; | ||
| 257 | $db_type = '%' . $db_type . '%'; | ||
| 258 | |||
| 259 | my $constraint = "b.biotype_group = ? AND b.db_type LIKE ? "; | ||
| 260 | $self->bind_param_generic_fetch($biotype_group, SQL_VARCHAR); | ||
| 261 | $self->bind_param_generic_fetch($db_type, SQL_VARCHAR); | ||
| 262 | if (defined $object_type) { | ||
| 263 | $constraint .= " AND b.object_type = ?"; | ||
| 264 | $self->bind_param_generic_fetch($object_type, SQL_VARCHAR); | ||
| 265 | } | ||
| 266 | my @biotypes = @{$self->generic_fetch($constraint)}; | ||
| 267 | |||
| 268 | if ( !@biotypes ) { | ||
| 269 | warning("No objects retrieved. Check if object_type '$object_type' is correct.") | ||
| 270 | } | ||
| 271 | |||
| 272 | return \@biotypes; | ||
| 273 | } | ||
| 274 | |||
| 275 | 1; |