This adaptor provides database connectivity for IndividualGenotype objects.
IndividualGenotypes may be retrieved from the Ensembl variation database by
several means using this module.
sub _objs_from_sth
{ my ($self, $sth, $mapper, $dest_slice) = @_;
return $self->SUPER::_objs_from_sth($sth,$mapper,$dest_slice) if ($self->_multiple);
my $sa = $self->db()->dnadb()->get_SliceAdaptor();
my @results;
my %slice_hash;
my %sr_name_hash;
my %sr_cs_hash;
my %individual_hash;
my ($sample_id, $seq_region_id, $seq_region_start,
$seq_region_end, $seq_region_strand, $genotypes);
$sth->bind_columns(\$sample_id,\$ seq_region_id,\$
seq_region_start,\$ seq_region_end,\$ seq_region_strand,\$
genotypes);
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 = $seq_region_start - $dest_slice_start + 1;
$seq_region_end = $dest_slice_end - $tmp_seq_region_start + 1;
}
if($seq_region_end < 1 || $seq_region_start > $dest_slice_length) {
next FEATURE;
}
}
$slice = $dest_slice;
}
my $blob = substr($genotypes,2);
my @genotypes = unpack("naa" x (length($blob)/4),$blob);
unshift @genotypes, substr($genotypes,1,1); unshift @genotypes, substr($genotypes,0,1); unshift @genotypes, 0; my ($snp_start, $allele_1, $allele_2);
for (my $i=0; $i < @genotypes -1;$i+=3){
if ($i == 0){
$snp_start = $seq_region_start; }
else{
if ($genotypes[$i] == -1){ $snp_start += 1;
next;
}
$snp_start += ($genotypes[$i] +1);
}
$allele_1 = $genotypes[$i+1];
$allele_2 = $genotypes[$i+2];
my $igtype = Bio::EnsEMBL::Variation::IndividualGenotype->new_fast({
'start' => $snp_start,
'end' => $snp_start,
'strand' => $seq_region_strand,
'slice' => $slice,
'allele1' => $allele_1,
'allele2' => $allele_2,
'adaptor' => $self
});
$individual_hash{$sample_id} ||= [];
push @{$individual_hash{$sample_id}}, $igtype;
push @results, $igtype;
}
}
my @ind_ids = keys %individual_hash;
my $ia = $self->db()->get_IndividualAdaptor();
my $inds = $ia->fetch_all_by_dbID_list(\@ind_ids);
foreach my $i (@$inds) {
foreach my $igty (@{$individual_hash{$i->dbID()}}) {
$igty->individual($i);
}
}
return\@ results; } |
sub fetch_all_by_Slice
{ my $self = shift;
my $slice = shift;
my $individual = shift;
my @results;
my $features;
my $constraint;
if (!$self->_multiple){
if (defined $individual && defined $individual->dbID){
$constraint = ' c.sample_id = ' . $individual->dbID;
$features = $self->SUPER::fetch_all_by_Slice_constraint($slice,$constraint);
}
else{
$features = $self->SUPER::fetch_all_by_Slice($slice);
}
foreach my $indFeature (@{$features}){
if ($indFeature->start > 0 && ($slice->end-$slice->start +1) >= $indFeature->end){
if ($indFeature->slice->strand == -1){
$indFeature->slice->{'strand'} = 1;
my $newFeature = $indFeature->transfer($slice->seq_region_Slice);
$indFeature->slice->{'strand'} = -1;
$newFeature->slice->{'strand'} = -1;
$newFeature->variation($indFeature->variation);
push @results, $newFeature;
}
else{
push @results,$indFeature->transfer($slice->seq_region_Slice);
}
}
}
}
else{
if (defined $individual && defined $individual->dbID){
$constraint = ' c.sample_id = ' . $individual->dbID;
$features = $self->SUPER::fetch_all_by_Slice_constraint($slice,$constraint);
}
else{
$features = $self->SUPER::fetch_all_by_Slice($slice);
}
push @results, $features;
}
return\@ results; } |