blib/script/gbrowse_syn_load_alignment_database.pl untested

filter: Statementstmt 0.0% 0 / 83 Branchbran 0.0% 0 / 28 Conditioncond 0.0% 0 / 33 MC/DCmcdc n/a Subroutinesub 0.0% 0 / 8 Podpod 0.0% 0 / 2 totaltotal 0.0% 0 / 154 CC 3 SCAR 16.1
CC
3
Cov
0%
invert
12.0
pad
2.0
BEGIN
2.0
CRAP 5.0
1 #!/usr/local/bin/perl -w
2 # $Id: load_alignment_database.pl,v 1.1.2.4 2009-06-03 08:09:26 sheldon_mckay Exp $
3 use strict;
4  
5 # load_alignment_database.pl -- a script to load the database for gbrowse_syn.
6  
7 # The expected file format is tab-delimited (shown below):
8 # species1 ref1 start1 end1 strand1 cigar_string1 species2 ref2 start2 end2 strand2 cigar_string2 coords1... | coords2...
9 # the coordinate format: pos1_species1 pos1_species2 ... posn_species1 posn_species2 | pos1_species2 pos1_species1 ... posn_species2 posn_species1,
10 # where pos is the matching sequence coordinate (ungapped) in each species.
11 #
12  
13 use strict;
14 use Bio::DB::GFF::Util::Binning 'bin';
15 use Getopt::Long;
16  
17 use constant MINBIN => 1000;
18  
19 use vars qw/$create $user $pass $dsn $verbose $create $aln_idx/;
20  
21 $| = 1;
22 GetOptions(
23 'user=s' => \$user,
24 'pass=s' => \$pass,
25 'dsn=s' => \$dsn,
26 'verbose' => \$verbose,
27 'create' => \$create
28 );
29  
30 my $usage = "Usage: load_alignment_database.pl -u username -p password -d database [-v, -c] file1, file2 ... filen\n\n";
31  
32 $dsn || die "Error: no database name\n$usage";
33 $user || die "Error: no user name\n$usage";
34  
35 if ($create && $verbose) {
36 print STDERR "\nNote: a new database $dsn will be initialized\n EXISTING DATA WILL BE DELETED\n\n";
37 }
38 elsif ($verbose) {
39 print STDERR "\nNote: data will be appended to the existing database $dsn\n\n";
40 }
41  
42  
43 $dsn = "dbi:mysql:$dsn" unless $dsn =~ /^dbi/;
44 eval "require DBI; 1" or die "DBI module not installed. Cannot continue";
45 my $dbh = DBI->connect($dsn, $user, $pass);
46 unless ($dbh) {
47 $dsn =~ s/\S+\:([^:]+)$/$1/;
48 my $error = DBI->errstr;
49 die <<END;
50  
51 Error: $error
52  
53 There was a problem opening a connection to database $dsn.
54 Make sure you have used the correct username ($user), password ($pass) and that you have
55 created the database $dsn via your mysql client.
56
57 Consult the error message above for more information.
58 END
59 ;
60 }
61  
62 if ($create) {
63 $dbh->do('drop table if exists alignments') or die DBI->errstr;
64 $dbh->do('drop table if exists map') or die DBI->errstr;
65  
66 $dbh->do(<<END) or die DBI->errstr;
67 create table alignments (
68 hit_id int not null auto_increment,
69 hit_name varchar(100) not null,
70 src1 varchar(100) not null,
71 ref1 varchar(100) not null,
72 start1 int not null,
73 end1 int not null,
74 strand1 enum('+','-') not null,
75 seq1 mediumtext,
76 bin double(20,6) not null,
77 src2 varchar(100) not null,
78 ref2 varchar(100) not null,
79 start2 int not null,
80 end2 int not null,
81 strand2 enum('+','-') not null,
82 seq2 mediumtext,
83 primary key(hit_id),
84 index(src1,ref1,bin,start1,end1)
85 )
86 END
87 ;
88  
89 $dbh->do(<<END) or die DBI->errstr;
90 create table map (
91 map_id int not null auto_increment,
92 hit_name varchar(100) not null,
93 src1 varchar(100),
94 pos1 int not null,
95 pos2 int not null,
96 primary key(map_id),
97 index(hit_name)
98 )
99 END
100 ;
101 }
102  
103 $dbh->do('alter table alignments disable keys');
104 $dbh->do('alter table map');
105 my $sth=$dbh->prepare(<<END) or die $dbh->errstr;
106 insert into alignments (hit_name,src1,ref1,start1,end1,strand1,seq1,bin,src2,ref2,start2,end2,strand2,seq2)
107 values(?,?,?,?,?,?,?,?,?,?,?,?,?,?)
108 END
109 ;
110  
111 my $sth2 = $dbh->prepare('insert into map (hit_name,src1,pos1,pos2) values(?,?,?,?)');
112  
113 my $hit_idx;
114 while (<>) {
115 chomp;
116 my ($src1,$ref1,$start1,$end1,$strand1,$seq1,$src2,$ref2,$start2,$end2,$strand2,$seq2,@maps) = split "\t";
117  
118 # not using the cigar strings right now
119 ($seq1,$seq2) = ('','');
120  
121 # deal with coordinate maps
122 my ($switch,@map1,@map2);
123 for (@maps) {
124 if ($_ eq '|') {
125 $switch++;
126 next;
127 }
128 $switch ? push @map2, $_ : push @map1, $_;
129 }
130 my %map1 = @map1;
131 my %map2 = @map2;
132  
133 # standardize hit names
134 my $hit1 = 'H'.pad(++$hit_idx);
135 my $bin1 = scalar bin($start1,$end1,MINBIN);
136 my $bin2 = scalar bin($start2,$end2,MINBIN);
137 my $hit2 = "${hit1}r";
138
139 # force ref strand to always be positive and invert target strand as required
140 invert(\$strand1,\$strand2) if $strand1 eq '-';
141  
142 $sth->execute($hit1,$src1,$ref1,$start1,$end1,$strand1,$seq1,$bin1,
143 $src2,$ref2,$start2,$end2,$strand2,$seq2) or warn $sth->errstr;
144  
145 for my $pos (sort {$a<=>$b} keys %map1) {
146 next unless $pos && $map1{$pos};
147 $sth2->execute($hit1,$src1,$pos,$map1{$pos}) or die $sth->errstr;
148 }
149  
150 # reciprocal hit is also saved to facilitate switching amongst reference sequences
151 invert(\$strand1,\$strand2) if $strand2 eq '-';
152
153 $sth->execute($hit2,$src2,$ref2,$start2,$end2,$strand2,$seq2,$bin2,
154 $src1,$ref1,$start1,$end1,$strand1,$seq1) or warn $sth->errstr;
155  
156  
157 # saving pair-wise coordinate maps -- these are needed for gridlines
158 for my $pos (sort {$a<=>$b} keys %map2) {
159 next unless $pos && $map2{$pos};
160 $sth2->execute($hit1,$src2,$pos,$map2{$pos}) or die $sth->errstr;
161 }
162  
163 print STDERR " processed $hit1!\r" if $verbose;
164 }
165  
166  
167 $dbh->do('alter table alignments enable keys');
168  
169 print "\nDone\n";
170  
171 sub pad {
172 my $num = shift;
173 until ((length $num) >=6) {
174 $num = '0' . $num;
175 }
176 $num;
177 }
178  
179  
180 sub invert {
181 my $strand1 = shift;
182 my $strand2 = shift;
183 $$strand1 = $$strand1 eq '+' ? '-' : '+';
184 $$strand2 = $$strand2 eq '+' ? '-' : '+';
185 }