3 # See the NOTICE file distributed with this work for additional information
4 # regarding copyright ownership.
6 # Licensed under the Apache License, Version 2.0 (the "License");
7 # you may not use this file except in compliance with the License.
8 # You may obtain a copy of the License at
10 # http://www.apache.org/licenses/LICENSE-2.0
12 # Unless required by applicable law or agreed to in writing, software
13 # distributed under the License is distributed on an "AS IS" BASIS,
14 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15 # See the License for the specific language governing permissions and
16 # limitations under the License.
18 # Transfer karyotype data from old -> new database, scaling appropriately.
27 my ( $oldhost, $olduser, $oldpass, $oldport, $olddbname, $newhost, $newuser, $newpass, $newport, $newdbname );
30 GetOptions(
"oldhost=s", \$oldhost,
31 "olduser=s", \$olduser,
32 "oldpass=s", \$oldpass,
33 "oldport=i", \$oldport,
34 "olddbname=s", \$olddbname,
35 "newhost=s", \$newhost,
36 "newuser=s", \$newuser,
37 "newpass=s", \$newpass,
38 "newport=i", \$newport,
39 "newdbname=s", \$newdbname);
41 usage() if (!$oldhost);
43 my $old_db = DBI->connect("DBI:mysql:host=$oldhost;dbname=$olddbname;port=$oldport", $olduser, $oldpass);
45 my $new_db = DBI->connect("DBI:mysql:host=$newhost;dbname=$newdbname;port=$newport", $newuser, $newpass);
47 # check for rows in new table
48 my $chk_sth = $new_db->prepare(
"SELECT COUNT(*) FROM karyotype");
50 my $count = ($chk_sth->fetchrow_array())[0];
52 print STDERR
"Karyotype table in $newdbname should be empty but has $count rows - delete and re-run\n";
56 my $old_sth = $old_db->prepare(
"SELECT sr.name, cs.name, sr.length, k.seq_region_start, k.seq_region_end, k.band, k.stain FROM seq_region sr, coord_system cs, karyotype k WHERE sr.coord_system_id=cs.coord_system_id AND sr.seq_region_id=k.seq_region_id");
59 my ($old_sr_name, $old_cs_name, $old_sr_length, $old_k_start, $old_k_end, $band, $stain);
60 $old_sth->bind_columns(\$old_sr_name, \$old_cs_name, \$old_sr_length, \$old_k_start, \$old_k_end, \$band, \$stain);
62 my $new_sth = $new_db->prepare(
'SELECT sr.seq_region_id, sr.length FROM seq_region sr, coord_system cs WHERE sr.name=? and cs.name=? AND sr.coord_system_id=cs.coord_system_id AND cs.attrib like "%default_version%" ');
63 my ($new_sr_id, $new_sr_length);
65 my $insert_sth = $new_db->prepare(
"INSERT INTO karyotype (seq_region_id, seq_region_start, seq_region_end, band, stain) VALUES(?,?,?,?,?)");
69 while ($old_sth->fetch()) {
71 # get matching seq region from new database & calculate scaling factor
72 $new_sth->execute($old_sr_name, $old_cs_name);
73 $new_sth->bind_columns(\$new_sr_id, \$new_sr_length);
75 if ($new_sth->fetch()) {
77 $scale_factor = ($new_sr_length/$old_sr_length);
78 my $new_k_start = int($old_k_start * $scale_factor) || 1;
79 my $new_k_end = int($old_k_end * $scale_factor);
80 if ($old_k_end == $old_sr_length) {
81 $new_k_end = $new_sr_length;
84 # Add new entry to new karyotype table
85 $insert_sth->execute($new_sr_id, $new_k_start, $new_k_end, $band, $stain) || die
"Error inserting into new karyotype table";
89 warn(
"Can't get new seq_region ID corresponding to $old_cs_name:$old_sr_name\n");
94 print
"Inserted $count rows into $newdbname.karyotype\n";
100 Transfer karyotype data from old to
new database, scaling appropriately.
102 perl transfer_karyotype.pl -oldhost ... -olduser ... -oldpass ... -oldport ... -olddbname ... -newhost ... -newuser ... -newpass ... -newport ... -newdbname