ensembl-hive  2.8.1
transfer_karyotype.pl
Go to the documentation of this file.
1 #!/usr/bin/env perl
2 #
3 # See the NOTICE file distributed with this work for additional information
4 # regarding copyright ownership.
5 #
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
9 #
10 # http://www.apache.org/licenses/LICENSE-2.0
11 #
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.
17 
18 # Transfer karyotype data from old -> new database, scaling appropriately.
19 
20 use strict;
21 use warnings;
22 
23 
24 use DBI;
25 use Getopt::Long;
26 
27 my ( $oldhost, $olduser, $oldpass, $oldport, $olddbname, $newhost, $newuser, $newpass, $newport, $newdbname );
28 
29 
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);
40 
41 usage() if (!$oldhost);
42 
43 my $old_db = DBI->connect("DBI:mysql:host=$oldhost;dbname=$olddbname;port=$oldport", $olduser, $oldpass);
44 
45 my $new_db = DBI->connect("DBI:mysql:host=$newhost;dbname=$newdbname;port=$newport", $newuser, $newpass);
46 
47 # check for rows in new table
48 my $chk_sth = $new_db->prepare("SELECT COUNT(*) FROM karyotype");
49 $chk_sth->execute();
50 my $count = ($chk_sth->fetchrow_array())[0];
51 if ($count > 0) {
52  print STDERR "Karyotype table in $newdbname should be empty but has $count rows - delete and re-run\n";
53  exit(1);
54 }
55 
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");
57 $old_sth->execute();
58 
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);
61 
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);
64 
65 my $insert_sth = $new_db->prepare("INSERT INTO karyotype (seq_region_id, seq_region_start, seq_region_end, band, stain) VALUES(?,?,?,?,?)");
66 
67 my $count;
68 
69 while ($old_sth->fetch()) {
70 
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);
74  my $scale_factor;
75  if ($new_sth->fetch()) {
76 
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;
82  }
83 
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";
86  $count++;
87 
88  } else {
89  warn("Can't get new seq_region ID corresponding to $old_cs_name:$old_sr_name\n");
90  }
91 
92 }
93 
94 print "Inserted $count rows into $newdbname.karyotype\n";
95 
96 
97 sub usage {
98 
99  print<<EOF;
100 Transfer karyotype data from old to new database, scaling appropriately.
101 
102 perl transfer_karyotype.pl -oldhost ... -olduser ... -oldpass ... -oldport ... -olddbname ... -newhost ... -newuser ... -newpass ... -newport ... -newdbname
103 
104 EOF
105 
106  exit(1);
107 
108 }
usage
public usage()