| 1 | package Legacy::DB::SyntenyIO; | ||
| 2 | |||
| 3 | use strict; | ||
| 4 | use DBI; | ||
| 5 | use Bio::DB::GFF::Util::Binning qw(bin bin_bot bin_top); | ||
| 6 | use Bio::DB::GFF::Util::Rearrange qw(rearrange); | ||
| 7 | use Legacy::DB::SyntenyBlock; | ||
| 8 | |||
| 9 | use constant MINBIN => 1000; | ||
| 10 | use constant MAXBIN => 1_000_000_000; | ||
| 11 | use constant EPSILON => 1e-7; # set to zero if you trust mysql's floating point comparisons | ||
| 12 | use constant POSRANGE => 200; | ||
| 13 | |||
| 14 | sub new { | ||
| 15 | my $class = shift; | ||
| 16 | my $dsn = shift; | ||
| 17 | my $dbh; | ||
| 18 | |||
| 19 | if (ref($dsn) && $dsn->isa('DBI::db')) { | ||
| 20 | $dbh = $dsn; | ||
| 21 | } else { | ||
| 22 | $dbh = DBI->connect($dsn,@_) or die "$dsn: Can't open; ",DBI->errstr; | ||
| 23 | } | ||
| 24 | |||
| 25 | my $self = {dbh=>$dbh}; | ||
| 26 | $self->{nomap} = 1 unless _has_map($dbh); | ||
| 27 | return bless $self, $class; | ||
| 28 | } | ||
| 29 | |||
| 30 | sub dbh { shift->{dbh} } | ||
| 31 | |||
| 32 | sub nomap { shift->{nomap} } | ||
| 33 | |||
| 34 | # a method to get the nearest residue position match | ||
| 35 | # (for truncating hits and gridlines). Return the nearest mapped | ||
| 36 | # source residue and the corresponding target residue. | ||
| 37 | sub get_nearest_position_match { | ||
| 38 | my $self = shift; | ||
| 39 | my ($hit,$src,$pos,$range) = @_; | ||
| 40 | |||
| 41 | my @hits = ref $hit && $hit->parts > 1 ? @{$hit->parts} : ($hit); | ||
| 42 | $range ||= POSRANGE; | ||
| 43 | |||
| 44 | for my $h (@hits) { | ||
| 45 | my $min = $pos - int($range/2); | ||
| 46 | my $max = $pos + int($range/2); | ||
| 47 | my $sth = $self->position_handle; | ||
| 48 | my $hname = ref $h ? $h->name : $h; | ||
| 49 | $hname =~ s/r|\.\d+//g; | ||
| 50 | $sth->execute($hname,$src,$min,$max) or die $self->dbh->errstr; | ||
| 51 | my %match; | ||
| 52 | while (my @row = $sth->fetchrow_array) { | ||
| 53 | $match{abs($row[0] - $pos)} = \@row; | ||
| 54 | } | ||
| 55 | |||
| 56 | my ($nearest) = map {$match{$_}} sort {$a <=> $b} keys %match; | ||
| 57 | $nearest ||= [undef,undef]; | ||
| 58 | return @$nearest if ref $nearest && defined $nearest->[0]; | ||
| 59 | } | ||
| 60 | } | ||
| 61 | |||
| 62 | # a method to get a range of exact grid coordinates | ||
| 63 | # for synteny data with sparse gridlines that are | ||
| 64 | # not suitable for rounding off to the nearest multiple of 10 | ||
| 65 | sub grid_coords_by_range { | ||
| 66 | my $self = shift; | ||
| 67 | my ($hit,$src) = @_; | ||
| 68 | |||
| 69 | my @hits = ref $hit && $hit->parts > 1 ? @{$hit->parts} : ($hit); | ||
| 70 | my @pairs; | ||
| 71 | my $sth = $self->position_handle; | ||
| 72 | |||
| 73 | for my $h (@hits) { | ||
| 74 | my $hname = ref $h ? $h->name : $h; | ||
| 75 | $hname =~ s/r|\.\d+//g; | ||
| 76 | $sth->execute($hname,$src,$hit->start,$hit->end); | ||
| 77 | my $pairs = $sth->fetchall_arrayref; | ||
| 78 | push @pairs, @$pairs; | ||
| 79 | } | ||
| 80 | |||
| 81 | return @pairs; | ||
| 82 | } | ||
| 83 | |||
| 84 | |||
| 85 | # Check to see of grid-lines are possible. Some data sources | ||
| 86 | # may lack the grid coordinate data (not that there is anything | ||
| 87 | # wrong with that). | ||
| 88 | sub _has_map { | ||
| 89 | my $dbh = shift; | ||
| 90 | my $sth = $dbh->prepare('SELECT count(*) FROM map'); | ||
| 91 | $sth->execute; | ||
| 92 | my ($count) = $sth->fetchrow_array; | ||
| 93 | return $count; | ||
| 94 | } | ||
| 95 | |||
| 96 | |||
| 97 | sub position_handle { | ||
| 98 | my $self = shift; | ||
| 99 | |||
| 100 | unless (defined $self->{position_query}) { | ||
| 101 | my $query = <<END; | ||
| 102 | select pos1,pos2 from map | ||
| 103 | WHERE hit_name = ? | ||
| 104 | AND src1 = ? | ||
| 105 | AND pos1 >= ? | ||
| 106 | AND pos1 <= ? | ||
| 107 | END | ||
| 108 | ; | ||
| 109 | $self->{position_query} = $self->dbh->prepare_cached($query) or die $self->dbh->errstr; | ||
| 110 | |||
| 111 | } | ||
| 112 | |||
| 113 | return $self->{position_query}; | ||
| 114 | } | ||
| 115 | |||
| 116 | sub get_synteny_by_range { | ||
| 117 | my $self = shift; | ||
| 118 | my ($src, # a symbolic data source, like "worm" | ||
| 119 | $ref, # reference for search range - contig or chromosome name | ||
| 120 | $start, # start of search range | ||
| 121 | $end, # end of search range | ||
| 122 | $tgt # optional data source target, like "yeast" | ||
| 123 | ) = rearrange([qw(SRC REF START END TGT)],@_); | ||
| 124 | my ($query,@args) = $self->make_range_query($src,$ref,$start,$end,$tgt); | ||
| 125 | my $sth = $self->dbh->prepare_cached($query) or die $self->dbh->errstr; | ||
| 126 | $sth->execute(@args) or die $sth->errstr; | ||
| 127 | my %HITS; | ||
| 128 | while (my($hit, | ||
| 129 | $src,$ref1,$start1,$end1,$strand1,$seq1, | ||
| 130 | $tgt,$ref2,$start2,$end2,$strand2,$seq2) = $sth->fetchrow_array) { | ||
| 131 | $HITS{$hit} ||= Legacy::DB::SyntenyBlock->new($hit); | ||
| 132 | $HITS{$hit}->add_part([$src,$ref1,$start1,$end1,$strand1,$seq1], | ||
| 133 | [$tgt,$ref2,$start2,$end2,$strand2,$seq2] | ||
| 134 | ); | ||
| 135 | } | ||
| 136 | return values %HITS; | ||
| 137 | } | ||
| 138 | |||
| 139 | sub make_range_query { | ||
| 140 | my $self = shift; | ||
| 141 | my ($src,$ref,$start,$end,$tgt) = @_; | ||
| 142 | my $query = | ||
| 143 | "select hit_name,src1,ref1,start1,end1,strand1,seq1,src2,ref2,start2,end2,strand2,seq2\n\tFROM alignments"; | ||
| 144 | |||
| 145 | my @where = (); | ||
| 146 | my @args = (); | ||
| 147 | |||
| 148 | if (defined $src) { | ||
| 149 | push @where,'src1=?'; | ||
| 150 | push @args,$src; | ||
| 151 | } | ||
| 152 | |||
| 153 | if (defined $ref) { | ||
| 154 | push @where,'ref1=?'; | ||
| 155 | push @args,$ref; | ||
| 156 | } | ||
| 157 | |||
| 158 | if (defined $start and defined $end) { | ||
| 159 | my ($range_part,@range_args) = $self->bin_query($start,$end); | ||
| 160 | push @where,$range_part; | ||
| 161 | push @args,@range_args; | ||
| 162 | } | ||
| 163 | |||
| 164 | if (defined $tgt) { | ||
| 165 | push @where,'src2=?'; | ||
| 166 | push @args,$tgt; | ||
| 167 | } | ||
| 168 | |||
| 169 | if (@where) { | ||
| 170 | $query .= "\n\tWHERE ".join(' AND ',@where); | ||
| 171 | } | ||
| 172 | |||
| 173 | return ($query,@args); | ||
| 174 | } | ||
| 175 | |||
| 176 | # stolen from Bio::DB::GFF::Adaptor::dbi | ||
| 177 | sub bin_query { | ||
| 178 | my $self = shift; | ||
| 179 | my ($start,$end) = @_; | ||
| 180 | my ($query,@args); | ||
| 181 | |||
| 182 | $start = 0 unless defined($start); | ||
| 183 | $end = MAXBIN unless defined($end); | ||
| 184 | |||
| 185 | my @bins; | ||
| 186 | my $minbin = MINBIN; | ||
| 187 | my $maxbin = MAXBIN; | ||
| 188 | my $tier = $maxbin; | ||
| 189 | while ($tier >= $minbin) { | ||
| 190 | my ($tier_start,$tier_stop) = (bin_bot($tier,$start)-EPSILON(),bin_top($tier,$end)+EPSILON()); | ||
| 191 | if ($tier_start == $tier_stop) { | ||
| 192 | push @bins,'bin=?'; | ||
| 193 | push @args,$tier_start; | ||
| 194 | } else { | ||
| 195 | push @bins,'bin between ? and ?'; | ||
| 196 | push @args,($tier_start,$tier_stop); | ||
| 197 | } | ||
| 198 | $tier /= 10; | ||
| 199 | } | ||
| 200 | |||
| 201 | my $bin_part = join("\n\t OR ",@bins); | ||
| 202 | $query = "($bin_part) AND end1>=? AND start1<=?"; | ||
| 203 | return ($query,@args,$start,$end); | ||
| 204 | } | ||
| 205 | |||
| 206 | 1; |