$seq_adptr = $database_adaptor->get_SequenceAdaptor();
$dna =
${ $seq_adptr->fetch_by_Slice_start_end_strand( $slice, 1, 1000,
-1 ) };
sub _fetch_seq
{ my $self = shift;
my $seq_region_id = shift;
my $start = shift;
my $len = shift;
my $comp_start = ($start-1 >> 2) + 1;
my $comp_len = ($len >> 2) + 2;
my ($bvector, $nline);
my $sth = $self->prepare(
"SELECT SUBSTRING( d.sequence, ?, ?), n_line
FROM dnac d
WHERE d.seq_region_id = ?");
$sth->bind_param(1,$comp_start,SQL_INTEGER);
$sth->bind_param(2,$comp_len ,SQL_INTEGER);
$sth->bind_param(3,$seq_region_id,SQL_INTEGER);
$sth->execute();
$sth->bind_columns(\$bvector,\$ nline);
$sth->fetch();
$sth->finish();
my $bitlen = length($bvector) << 2; my $str = ''; for(my $i=0; $i < $bitlen; $i++) { $str .= vec($bvector, $i, 2); }
#convert from 0123 to ACTG $str =~ tr/0123/ACTG/;
$str = substr($str, ($start-1)%4, $len);
#expand the nlines and place them back in the sequence my @nlines = split(/:/, $nline); foreach my $nl (@nlines) { my ($offset,$char,$nlen) = $nl =~ /(\d+)(\D)(\d+)/; #skip nlines entirely out of range next if(($offset+$nlen-1) < $start || $offset > ($start+$len-1)); #obtain relative offset into requested region $offset = $offset - $start + 1;
#nlines that partially overlap requested region have to be shrunk if($offset < 1) { $nlen = $nlen - (1-$offset); $offset = 1; } if($offset + $nlen > $start+$len) { $nlen = $len - $offset + 1; }
substr($str,$offset-1,$nlen) = $char x $nlen; }
return \$str;
} |
sub store
{ my ($self, $seq_region_id, $sequence) = @_;
if(!$seq_region_id) {
throw('seq_region_id is required');
}
$sequence = uc($sequence);
my $bvector = '';
$sequence =~ tr/ACTG/0123/;
my($nline_char,$nline_len,$nline_off);
my @nlines;
my $len = length($sequence);
for(my $i=0; $i < $len; $i++) {
my $char = substr($sequence,$i,1);
if($char =~ /[0-3]/) {
vec($bvector, $i,2) = $char;
if($nline_char) {
push @nlines, "$nline_off$nline_char$nline_len";
$nline_char = undef;
$nline_len = 0;
$nline_off = 0;
}
} else {
if($nline_char) {
if($nline_char eq $char) {
$nline_len++;
} else {
push @nlines, "$nline_off$nline_char$nline_len";
$nline_char = $char;
$nline_len = 1;
$nline_off = $i+1;
}
} else {
$nline_char = $char;
$nline_len = 1;
$nline_off = $i+1;
}
$char = 0; }
vec($bvector, $i,2) = $char;
}
my $nline = join(':', @nlines);
my $statement = $self->prepare(
"INSERT INTO dnac(seq_region_id, sequence, n_line) VALUES(?,?,?)");
$statement->bind_param(1,$seq_region_id,SQL_INTEGER);
$statement->bind_param(2,$bvector,SQL_BLOB);
$statement->bind_param(3,$nline,SQL_LONGVARCHAR);
$statement->execute();
$statement->finish();
return;
}
1; } |