$vdb = Bio::EnsEMBL::Variation::DBSQL::DBAdaptor->new(...);
$db = Bio::EnsEMBL::DBSQL::DBAdaptor->new(...);
# tell the variation database where core database information can be
# be found
$vdb->dnadb($db);
$afa = $vdb->get_AlleleFeatureAdaptor();
$sa = $db->get_SliceAdaptor();
# Get a VariationFeature by its internal identifier
$af = $afa->fetch_by_dbID(145);
# get all AlleleFeatures in a region
$slice = $sa->fetch_by_region('chromosome', 'X', 1e6, 2e6);
foreach $af (@{$afa->fetch_all_by_Slice($slice)}) {
print $af->start(), '-', $af->end(), ' ', $af->allele(), "\n";
}
This adaptor provides database connectivity for AlleleFeature objects.
Genomic locations of alleles in samples can be obtained from the
database using this adaptor. See the base class BaseFeatureAdaptor for more information.
sub _columns
{ my $self = shift;
return ('vf.variation_id' ,'ig.sample_id', 'CONCAT(ig.allele_1,"|",ig.allele_2) as alleles',
'vf.seq_region_id', 'vf.seq_region_start', 'vf.seq_region_end',
'vf.seq_region_strand', 'vf.variation_name') if ($self->from_IndividualSlice());
return qw(vf.variation_id
vf.seq_region_id vf.seq_region_start vf.seq_region_end
vf.seq_region_strand vf.variation_name s.name vf.variation_feature_id vf.allele_string); } |
sub _objs_from_sth
{ my ($self, $sth, $mapper, $dest_slice) = @_;
my $sa = $self->db()->dnadb()->get_SliceAdaptor();
my @features;
my %slice_hash;
my %sr_name_hash;
my %sr_cs_hash;
my ($variation_id, $seq_region_id,
$seq_region_start,$seq_region_end, $seq_region_strand, $variation_name, $source_name, $variation_feature_id, $allele_string );
$sth->bind_columns(\$variation_id,\$
seq_region_id,\$seq_region_start,\$seq_region_end,\$seq_region_strand,\$
variation_name,\$ source_name,\$ variation_feature_id,\$ allele_string);
my $asm_cs;
my $cmp_cs;
my $asm_cs_vers;
my $asm_cs_name;
my $cmp_cs_vers;
my $cmp_cs_name;
if($mapper) {
$asm_cs = $mapper->assembled_CoordSystem();
$cmp_cs = $mapper->component_CoordSystem();
$asm_cs_name = $asm_cs->name();
$asm_cs_vers = $asm_cs->version();
$cmp_cs_name = $cmp_cs->name();
$cmp_cs_vers = $cmp_cs->version();
}
my $dest_slice_start;
my $dest_slice_end;
my $dest_slice_strand;
my $dest_slice_length;
if($dest_slice) {
$dest_slice_start = $dest_slice->start();
$dest_slice_end = $dest_slice->end();
$dest_slice_strand = $dest_slice->strand();
$dest_slice_length = $dest_slice->length();
}
FEATURE: while($sth->fetch()) {
my $slice = $slice_hash{"ID:".$seq_region_id};
if(!$slice) {
$slice = $sa->fetch_by_seq_region_id($seq_region_id);
$slice_hash{"ID:".$seq_region_id} = $slice;
$sr_name_hash{$seq_region_id} = $slice->seq_region_name();
$sr_cs_hash{$seq_region_id} = $slice->coord_system();
}
if($mapper) {
my $sr_name = $sr_name_hash{$seq_region_id};
my $sr_cs = $sr_cs_hash{$seq_region_id};
($sr_name,$seq_region_start,$seq_region_end,$seq_region_strand) =
$mapper->fastmap($sr_name, $seq_region_start, $seq_region_end,
$seq_region_strand, $sr_cs);
next FEATURE if(!defined($sr_name));
if($asm_cs == $sr_cs || ($cmp_cs != $sr_cs && $asm_cs->equals($sr_cs))) {
$slice = $slice_hash{"NAME:$sr_name:$cmp_cs_name:$cmp_cs_vers"} ||=
$sa->fetch_by_region($cmp_cs_name, $sr_name,undef, undef, undef,
$cmp_cs_vers);
} else {
$slice = $slice_hash{"NAME:$sr_name:$asm_cs_name:$asm_cs_vers"} ||=
$sa->fetch_by_region($asm_cs_name, $sr_name, undef, undef, undef,
$asm_cs_vers);
}
}
if($dest_slice) {
if($dest_slice_start != 1 || $dest_slice_strand != 1) {
if($dest_slice_strand == 1) {
$seq_region_start = $seq_region_start - $dest_slice_start + 1;
$seq_region_end = $seq_region_end - $dest_slice_start + 1;
} else {
my $tmp_seq_region_start = $seq_region_start;
$seq_region_start = $dest_slice_end - $seq_region_end + 1;
$seq_region_end = $dest_slice_end - $tmp_seq_region_start + 1;
$seq_region_strand *= -1;
}
if($seq_region_end < 1 || $seq_region_start > $dest_slice_length) {
next FEATURE;
}
}
$slice = $dest_slice;
}
push @features, Bio::EnsEMBL::Variation::AlleleFeature->new_fast(
{'start' => $seq_region_start,
'end' => $seq_region_end,
'strand' => $seq_region_strand,
'slice' => $slice,
'allele_string' => '',
'variation_name' => $variation_name,
'adaptor' => $self,
'source' => $source_name,
'_variation_id' => $variation_id,
'_variation_feature_id' => $variation_feature_id,
'_vf_allele_string' => $allele_string,
'_sample_id' => ''});
}
return\@features;
}
} |
sub _tables
{ my $self = shift;
if ($self->from_IndividualSlice){
return (['variation_feature','vf'], ['individual_genotype_single_bp','ig']) if (!$self->_multiple_bp());
return (['variation_feature','vf'], ['individual_genotype_multiple_bp','ig']) if ($self->_multiple_bp());
}
else{
return (['variation_feature','vf'], ['source','s FORCE INDEX(PRIMARY)']);
} } |
sub fetch_all_by_Slice
{ my $self = shift;
my $slice = shift;
my $individual = shift;
if(!ref($slice) || !$slice->isa('Bio::EnsEMBL::Slice')) {
throw('Bio::EnsEMBL::Slice arg expected');
}
if (defined $individual){
if(!ref($individual) || !$individual->isa('Bio::EnsEMBL::Variation::Individual')) {
throw('Bio::EnsEMBL::Variation::Individual arg expected');
}
if(!defined($individual->dbID())) {
throw("Individual arg must have defined dbID");
}
}
%{$self->{'_slice_feature_cache'}} = (); my $genotype_adaptor = $self->db->get_IndividualGenotypeAdaptor; my $genotypes = $genotype_adaptor->fetch_all_by_Slice($slice,$individual); my $afs = $self->SUPER::fetch_all_by_Slice($slice); my $last_position = 0;
my $new_afs = [];
my $string1;
my $string2;
my $alleles = {}; foreach my $af (@{$afs}){
$alleles = {}; map {$alleles->{$_}++} split /\//,$af->{'_vf_allele_string'}; for (my $i = $last_position;$i<@{$genotypes};$i++){
$string1 = $genotypes->[$i]->allele1;
$string2 = $genotypes->[$i]->allele2;
if ($af->seq_region_strand == -1){
$string1 =~ tr/ACGTN-/TGCAN-/;
$string2 =~ tr/ACGTN-/TGCAN-/;
}
if ($genotypes->[$i]->start == $af->seq_region_start && (exists $alleles->{$string1} || exists $alleles->{$string2})){
$alleles = {}; delete $af->{'_vf_allele_string'}; if ($af->seq_region_strand == -1){
$genotypes->[$i]->allele1($string1);
$genotypes->[$i]->allele2($string2);
}
if ($genotypes->[$i]->allele2 eq 'N'){
$af->{'_half_genotype'} = 1;
$af->allele_string($genotypes->[$i]->allele1); }
else{
$af->{'_half_genotype'} = 0;
if($genotypes->[$i]->allele1 eq '-' and $genotypes->[$i]->allele1 eq '-'){
1;
}
$af->allele_string(ambiguity_code($genotypes->[$i]->allele1 . '/' . $genotypes->[$i]->allele2)); }
$af->individual($genotypes->[$i]->individual);
$last_position++;
push @{$new_afs},$af;
}
elsif ($genotypes->[$i]->start < $af->seq_region_start){
$last_position++; }
else{
last;
}
}
}
return $new_afs; } |
sub get_all_synonym_sources
{ my $self = shift;
my $af = shift;
my %sources;
my @sources;
if(!ref($af) || !$af->isa('Bio::EnsEMBL::Variation::AlleleFeature')) {
throw("Bio::EnsEMBL::Variation::AlleleFeature argument expected");
}
if (!defined($af->{'_variation_id'}) && !defined($af->{'variation'})){
warning("Not possible to get synonym sources for the AlleleFeature: you need to attach a Variation first");
return\@ sources;
}
my $variation_id;
if (defined ($af->{'_variation_id'})){
$variation_id = $af->{'_variation_id'};
}
else{
$variation_id = $af->variation->dbID();
}
my $source_name;
my $sth = $self->prepare(qq{SELECT s.name
FROM variation_synonym vs, source s
WHERE s.source_id = vs.source_id
AND vs.variation_id = ?
});
$sth->bind_param(1,$variation_id,SQL_INTEGER);
$sth->execute();
$sth->bind_columns(\$source_name);
while ($sth->fetch){
$sources{$source_name}++;
}
@sources = keys(%sources);
return\@ sources;
}
1; } |