| 1 | #!/usr/local/bin/perl -w | ||
| 2 | # $Id: load_alignments_msa.pl,v 1.1.2.2 2009-07-19 09:15:43 sheldon_mckay Exp $ | ||
| 3 | # This script will load the gbrowse_syn alignment database directly from a | ||
| 4 | # multiple sequence alignment file. | ||
| 5 | BEGIN { | ||
| 6 | #Check for DBI before running the script | ||
| 7 | # Doing this here will allow the "compile" tests to pass for GBrowse | ||
| 8 | # even if DBI is not installed. | ||
| 9 | eval { | ||
| 10 | require DBI; | ||
| 11 | DBI->import; | ||
| 12 | }; | ||
| 13 | |||
| 14 | if ($@) { | ||
| 15 | die "The DBI perl module is required to run this script\n"; | ||
| 16 | } | ||
| 17 | } | ||
| 18 | |||
| 19 | |||
| 20 | use strict; | ||
| 21 | use Bio::AlignIO; | ||
| 22 | use List::Util 'sum'; | ||
| 23 | use Getopt::Long; | ||
| 24 | #use DBI; | ||
| 25 | use Bio::DB::GFF::Util::Binning 'bin'; | ||
| 26 | |||
| 27 | use Data::Dumper; | ||
| 28 | |||
| 29 | use constant MINBIN => 1000; | ||
| 30 | use constant FORMAT => 'clustalw'; | ||
| 31 | use constant VERBOSE => 0; | ||
| 32 | use constant MAPRES => 100; | ||
| 33 | |||
| 34 | use vars qw/$format $create $user $pass $dsn $aln_idx $verbose $nomap $mapres $hit_idx $pidx %map/; | ||
| 35 | |||
| 36 | $| = 1; | ||
| 37 | GetOptions( | ||
| 38 | 'format=s' => \$format, | ||
| 39 | 'user=s' => \$user, | ||
| 40 | 'pass=s' => \$pass, | ||
| 41 | 'dsn=s' => \$dsn, | ||
| 42 | 'map=i' => \$mapres, | ||
| 43 | 'verbose' => \$verbose, | ||
| 44 | 'nomap' => \$nomap, | ||
| 45 | 'create' => \$create | ||
| 46 | ); | ||
| 47 | |||
| 48 | my $usage = "Usage: load_alignments_msa.pl -u username -p password -d database [-f format, -m map_resolution, -v, -n, -c] file1, file2 ... filen\n\n"; | ||
| 49 | |||
| 50 | $dsn || die $usage; | ||
| 51 | $user || die $usage; | ||
| 52 | $format ||= FORMAT; | ||
| 53 | $verbose ||= VERBOSE; | ||
| 54 | $mapres ||= MAPRES; | ||
| 55 | |||
| 56 | my ($dbh,$sth_hit,$sth_map) = prepare_database($dsn,$user,$pass); | ||
| 57 | |||
| 58 | while (my $infile = shift) { | ||
| 59 | print "Processing alignment file $infile...\n" if $verbose; | ||
| 60 | my $alignIO = Bio::AlignIO->new( -file => $infile, | ||
| 61 | -format => $format); | ||
| 62 | |||
| 63 | while (my $aln = $alignIO->next_aln) { | ||
| 64 | my $len = $aln->length; | ||
| 65 | $pidx = 0; | ||
| 66 | print STDERR "Processing Multiple Sequence Alignment " . ++$aln_idx . " (length $len)\t\t\t\r" if $verbose; | ||
| 67 | next if $aln->num_sequences < 2; | ||
| 68 | my %seq; | ||
| 69 | %map = (); | ||
| 70 | my $map = {}; | ||
| 71 | for my $seq ($aln->each_seq) { | ||
| 72 | my $seqid = $seq->id; | ||
| 73 | my ($species,$ref,$strand) = check_name_format($seqid,$seq); | ||
| 74 | next if $seq->seq =~ /^-+$/; | ||
| 75 | # We have to tell the sequence object what its strand is | ||
| 76 | $seq->strand($strand eq '-' ? -1 : 1); | ||
| 77 | $seq{$species} = [$ref, $seq->display_name, $seq->start, $seq->end, $strand, $seq->seq, $seq]; | ||
| 78 | } | ||
| 79 | |||
| 80 | # make all pairwise hits and grid coordinates | ||
| 81 | my @species = keys %seq; | ||
| 82 | |||
| 83 | for my $p (map_pairwise(@species)) { | ||
| 84 | my ($s1,$s2) = @$p; | ||
| 85 | my $array1 = $seq{$s1}; | ||
| 86 | my $array2 = $seq{$s2}; | ||
| 87 | |||
| 88 | my $seq1 = $$array1[6]; | ||
| 89 | my $seq2 = $$array2[6]; | ||
| 90 | |||
| 91 | unless ($nomap) { | ||
| 92 | $array1->[7] = make_map($seq1,$seq2,$map); | ||
| 93 | $array2->[7] = make_map($seq2,$seq1,$map); | ||
| 94 | } | ||
| 95 | |||
| 96 | make_hit($s1 => $array1, $s2 => $array2); | ||
| 97 | } | ||
| 98 | } | ||
| 99 | } | ||
| 100 | |||
| 101 | # Make coordinate maps at the specified resolution | ||
| 102 | sub make_map { | ||
| 103 | my ($s1,$s2,$map) = @_; | ||
| 104 | $s1 && $s2 || return {}; | ||
| 105 | unless (UNIVERSAL::can($s1,'isa')) { | ||
| 106 | warn "WTF? $s1 $s2\n" and next; | ||
| 107 | } | ||
| 108 | |||
| 109 | column_to_residue_number($s1,$s2); | ||
| 110 | my $coord = nearest($mapres,$s1->start); | ||
| 111 | $coord += $mapres if $coord < $s1->start; | ||
| 112 | my @map; | ||
| 113 | |||
| 114 | my $reverse = $s1->strand ne $s2->strand; | ||
| 115 | |||
| 116 | # have to get the column number from residue position, then | ||
| 117 | # the matching residue num from the column number | ||
| 118 | while ($coord < $s1->end) { | ||
| 119 | my $col = column_from_residue_number($s1,$coord); | ||
| 120 | my $coord2 = residue_from_column_number($s2,$col) if $col; | ||
| 121 | push @map, ($coord,$coord2) if $coord2; | ||
| 122 | $coord += $mapres; | ||
| 123 | } | ||
| 124 | return {@map}; | ||
| 125 | } | ||
| 126 | |||
| 127 | sub column_to_residue_number { | ||
| 128 | for my $seq (@_) { | ||
| 129 | my $str = $seq->seq; | ||
| 130 | my $id = $seq->id; | ||
| 131 | next if $map{$id}; | ||
| 132 | my $rev = $seq->strand < 0; | ||
| 133 | my $res = $rev ? $seq->end - 1 : $seq->start + 1; | ||
| 134 | my @cols = split '', $str; | ||
| 135 | |||
| 136 | my $pos; | ||
| 137 | my $col; | ||
| 138 | for my $chr (@cols) { | ||
| 139 | unless ($chr eq '-') { | ||
| 140 | $rev ? $res-- : $res++; | ||
| 141 | } | ||
| 142 | |||
| 143 | $col++; | ||
| 144 | $map{$id}{col}{$col} = $res; | ||
| 145 | $map{$id}{res}{$res} ||= $col; | ||
| 146 | } | ||
| 147 | } | ||
| 148 | } | ||
| 149 | |||
| 150 | sub column_from_residue_number { | ||
| 151 | my ($seq, $res) = @_; | ||
| 152 | my $id = $seq->id; | ||
| 153 | return $map{$id}{res}{$res}; | ||
| 154 | } | ||
| 155 | |||
| 156 | sub residue_from_column_number { | ||
| 157 | my ($seq, $col) = @_; | ||
| 158 | my $id = $seq->id; | ||
| 159 | print"WTF? $seq $id $col\n" unless $id &&$col; | ||
| 160 | return $map{$id}{col}{$col}; | ||
| 161 | } | ||
| 162 | |||
| 163 | sub make_hit { | ||
| 164 | my ($s1,$aln1,$s2,$aln2,$fh) = @_; | ||
| 165 | my $rightnum = $nomap ? 7 : 8; | ||
| 166 | die "wrong number of keys @$aln1" unless @$aln1 == $rightnum; | ||
| 167 | die "wrong number of keys @$aln2" unless @$aln2 == $rightnum; | ||
| 168 | my $map1 = $aln1->[7] || {}; | ||
| 169 | my $map2 = $aln2->[7] || {}; | ||
| 170 | |||
| 171 | # not using these yet | ||
| 172 | my ($cigar1,$cigar2) = qw/. ./; | ||
| 173 | |||
| 174 | load_alignment($s1,@{$aln1}[0,2..4],$cigar1,$s2,@{$aln2}[0,2..4],$cigar2,$map1,$map2); | ||
| 175 | } | ||
| 176 | |||
| 177 | sub map_pairwise { | ||
| 178 | my @out; | ||
| 179 | for my $i (0..$#_) { | ||
| 180 | for my $j ($i+1..$#_) { | ||
| 181 | push @out, [$_[$i], $_[$j]]; | ||
| 182 | } | ||
| 183 | } | ||
| 184 | return @out; | ||
| 185 | } | ||
| 186 | |||
| 187 | # stolen from Math::Round | ||
| 188 | sub nearest { | ||
| 189 | my $targ = abs(shift); | ||
| 190 | my $half = 0.50000000000008; | ||
| 191 | my @res = map { | ||
| 192 | if ($_ >= 0) { $targ * int(($_ + $half * $targ) / $targ); } | ||
| 193 | else { $targ * POSIX::ceil(($_ - $half * $targ) / $targ); } | ||
| 194 | } @_; | ||
| 195 | |||
| 196 | return (wantarray) ? @res : $res[0]; | ||
| 197 | } | ||
| 198 | |||
| 199 | sub check_name_format { | ||
| 200 | my $name = shift; | ||
| 201 | my $seq = shift; | ||
| 202 | |||
| 203 | my $nogood = <<" END"; | ||
| 204 | |||
| 205 | I am sorry, I do not like the sequence name: $name | ||
| 206 | |||
| 207 | This will not work unless you use the name format described below for each | ||
| 208 | sequence in the alignment. | ||
| 209 | |||
| 210 | We need the species, sequence name, strand, start and end for | ||
| 211 | each sequence in the alignment. | ||
| 212 | |||
| 213 | Name format: | ||
| 214 | species-sequence(strand)/start-end | ||
| 215 | |||
| 216 | where species = name of species, genome, strain, etc (string with no '-' characters) | ||
| 217 | sequence = name of reference sequence (string with no '/' characters) | ||
| 218 | (strand) = orientation of the alignment (relative to the reference sequence; + or -) | ||
| 219 | start = start coordinate of the alignment relative to the reference sequence (integer) | ||
| 220 | end = end coordinate of the alignment relative to the reference sequence (integer) | ||
| 221 | |||
| 222 | Examples: | ||
| 223 | c_elegans-I(+)/1..2300 | ||
| 224 | myco_bovis-chr1(-)/15000..25000 | ||
| 225 | |||
| 226 | END | ||
| 227 | ; | ||
| 228 | |||
| 229 | die $nogood unless $name =~ /^([^-]+)-([^\(]+)\(([+-])\)$/; | ||
| 230 | die $nogood unless $seq->start && $seq->end; | ||
| 231 | return ($1,$2,$3); | ||
| 232 | } | ||
| 233 | |||
| 234 | |||
| 235 | sub prepare_database { | ||
| 236 | my ($dns,$user,$pass) = @_; | ||
| 237 | $dsn = "dbi:mysql:$dsn" unless $dsn =~ /^dbi/; | ||
| 238 | |||
| 239 | my $dbh = DBI->connect($dsn, $user, $pass) or die DBI->errstr; | ||
| 240 | |||
| 241 | if ($create) { | ||
| 242 | $dbh->do('drop table if exists alignments') or die DBI->errstr; | ||
| 243 | $dbh->do('drop table if exists map') or die DBI->errstr; | ||
| 244 | |||
| 245 | $dbh->do(<<" END;") or die DBI->errstr; | ||
| 246 | create table alignments ( | ||
| 247 | hit_id int not null auto_increment, | ||
| 248 | hit_name varchar(100) not null, | ||
| 249 | src1 varchar(100) not null, | ||
| 250 | ref1 varchar(100) not null, | ||
| 251 | start1 int not null, | ||
| 252 | end1 int not null, | ||
| 253 | strand1 enum('+','-') not null, | ||
| 254 | seq1 mediumtext, | ||
| 255 | bin double(20,6) not null, | ||
| 256 | src2 varchar(100) not null, | ||
| 257 | ref2 varchar(100) not null, | ||
| 258 | start2 int not null, | ||
| 259 | end2 int not null, | ||
| 260 | strand2 enum('+','-') not null, | ||
| 261 | seq2 mediumtext, | ||
| 262 | primary key(hit_id), | ||
| 263 | index(src1,ref1,bin,start1,end1) | ||
| 264 | ) | ||
| 265 | END; | ||
| 266 | |||
| 267 | $dbh->do(<<" END;") or die DBI->errstr; | ||
| 268 | create table map ( | ||
| 269 | map_id int not null auto_increment, | ||
| 270 | hit_name varchar(100) not null, | ||
| 271 | src1 varchar(100), | ||
| 272 | pos1 int not null, | ||
| 273 | pos2 int not null, | ||
| 274 | primary key(map_id), | ||
| 275 | index(hit_name) | ||
| 276 | ) | ||
| 277 | END; | ||
| 278 | |||
| 279 | $dbh->do('alter table alignments disable keys'); | ||
| 280 | $dbh->do('alter table map'); | ||
| 281 | } | ||
| 282 | |||
| 283 | my $hit_insert = $dbh->prepare(<<END) or die $dbh->errstr; | ||
| 284 | insert into alignments (hit_name,src1,ref1,start1,end1,strand1,seq1,bin,src2,ref2,start2,end2,strand2,seq2) | ||
| 285 | values(?,?,?,?,?,?,?,?,?,?,?,?,?,?) | ||
| 286 | END | ||
| 287 | ; | ||
| 288 | |||
| 289 | my $map_insert = $dbh->prepare('insert into map (hit_name,src1,pos1,pos2) values(?,?,?,?)'); | ||
| 290 | |||
| 291 | return $dbh, $hit_insert, $map_insert; | ||
| 292 | } | ||
| 293 | |||
| 294 | sub load_alignment { | ||
| 295 | my ($src1,$ref1,$start1,$end1,$strand1,$seq1,$src2,$ref2,$start2,$end2,$strand2,$seq2,$map1,$map2) = @_; | ||
| 296 | |||
| 297 | # not using the cigar strings right now | ||
| 298 | ($seq1,$seq2) = ('',''); | ||
| 299 | |||
| 300 | $map1 ||= {}; | ||
| 301 | $map2 ||= {}; | ||
| 302 | |||
| 303 | my %map1 = %$map1; | ||
| 304 | my %map2 = %$map2; | ||
| 305 | |||
| 306 | # standardize hit names | ||
| 307 | my $hit1 = 'H' . sprintf('%010s', ++$hit_idx); | ||
| 308 | my $bin1 = scalar bin($start1,$end1,MINBIN); | ||
| 309 | my $bin2 = scalar bin($start2,$end2,MINBIN); | ||
| 310 | my $hit2 = "${hit1}r"; | ||
| 311 | |||
| 312 | # force ref strand to always be positive and invert target strand as required | ||
| 313 | invert(\$strand1,\$strand2) if $strand1 eq '-'; | ||
| 314 | |||
| 315 | $sth_hit->execute($hit1,$src1,$ref1,$start1,$end1,$strand1,$seq1,$bin1, | ||
| 316 | $src2,$ref2,$start2,$end2,$strand2,$seq2) or warn $sth_hit->errstr; | ||
| 317 | |||
| 318 | unless ($nomap) { | ||
| 319 | for my $pos (sort {$a<=>$b} keys %map1) { | ||
| 320 | next unless $pos && $map1{$pos}; | ||
| 321 | $sth_map->execute($hit1,$src1,$pos,$map1{$pos}) or die $sth_map->errstr; | ||
| 322 | } | ||
| 323 | } | ||
| 324 | |||
| 325 | # reciprocal hit is also saved to facilitate switching amongst reference sequences | ||
| 326 | invert(\$strand1,\$strand2) if $strand2 eq '-'; | ||
| 327 | |||
| 328 | $sth_hit->execute($hit2,$src2,$ref2,$start2,$end2,$strand2,$seq2,$bin2, | ||
| 329 | $src1,$ref1,$start1,$end1,$strand1,$seq1) or warn $sth_hit->errstr; | ||
| 330 | |||
| 331 | |||
| 332 | # saving pair-wise coordinate maps -- these are needed for gridlines | ||
| 333 | unless ($nomap) { | ||
| 334 | for my $pos (sort {$a<=>$b} keys %map2) { | ||
| 335 | next unless $pos && $map2{$pos}; | ||
| 336 | $sth_map->execute($hit1,$src2,$pos,$map2{$pos}) or die $sth_map->errstr; | ||
| 337 | } | ||
| 338 | } | ||
| 339 | |||
| 340 | print STDERR "Processed pair-wise alignment ".++$pidx."\r" if $verbose; | ||
| 341 | |||
| 342 | } | ||
| 343 | |||
| 344 | sub done { | ||
| 345 | $dbh->do('alter table alignments enable keys'); | ||
| 346 | print "\nDone!\n\n"; | ||
| 347 | } | ||
| 348 | |||
| 349 | sub invert { | ||
| 350 | my $strand1 = shift; | ||
| 351 | my $strand2 = shift; | ||
| 352 | $$strand1 = $$strand1 eq '+' ? '-' : '+'; | ||
| 353 | $$strand2 = $$strand2 eq '+' ? '-' : '+'; | ||
| 354 | } |