2 # See the NOTICE file distributed with this work for additional information
3 # regarding copyright ownership.
5 # Licensed under the Apache License, Version 2.0 (the "License");
6 # you may not use this file except in compliance with the License.
7 # You may obtain a copy of the License at
9 # http://www.apache.org/licenses/LICENSE-2.0
11 # Unless required by applicable law or agreed to in writing, software
12 # distributed under the License is distributed on an "AS IS" BASIS,
13 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 # See the License for the specific language governing permissions and
15 # limitations under the License.
18 #The script generates a FreePlane mind map file of xrefs for a given species.
19 #To view the generated file, download free mind mapping software Freeplane from http://freeplane.sourceforge.net
23 use DBI qw( :sql_types );
31 use vars qw($SERVERROOT);
50 GetOptions(
"xhost|xh=s" => \$xhost,
52 "xuser|xu=s" => \$xuser,
53 "xpass|xp=s" => \$xpass,
57 "species|s=s",\$species,
58 "db_version=i",\$db_version,
59 "dontdrop|d", \$dontdrop,
64 usage() if (!defined $xhost || !defined $xuser || !defined $xpass || !defined $host || !defined $user || !defined $species);
70 $db_version ||= software_version();
75 $registry->load_registry_from_db( -host => $host, -port => $port,-user => $user, -db_version => $db_version, -species => $species);
78 my $dba = $registry->get_DBAdaptor($species, "core");
81 die(
"$species database version $db_version not found on host $host port $port");
86 #create and connect to the xref_mindmap database
87 my $dsn =
"DBI:mysql:host=$xhost;";
89 $dsn .=
"port=$xport;";
91 my $xdbname =
'xref_mindmap_'.$species.
'_'.$db_version;
94 $dbh = DBI->connect( $dsn, $xuser, $xpass,
95 {
'PrintError' => 1,
'RaiseError' => 1 } );
98 my @objects_with_xrefs = (
'Gene',
'Transcript',
'Translation');
100 my %object_display_names = (
'Gene' =>
'Gene',
'Transcript' =>
'Transcript',
'Translation' =>
'Protein');
103 print STDOUT
"Creating database $xdbname\n";
106 $dbh->do(
"drop database if exists $xdbname");
107 $dbh->do(
"create database $xdbname");
109 my $cmd =
"mysql -h $xhost";
111 $cmd .=
" -P $xport";
113 $cmd .=
" -u $xuser --password=$xpass $xdbname < $SERVERROOT/sql/tables.sql";
114 system($cmd) == 0 or die(
"error encountered when creating schema for database $xdbname\n");
116 $dbh->do(
"use $xdbname");
121 die(
"An SQL error occured while creating database $xdbname:\n$@");
126 #copy the external_db information
128 my $external_db_info_sth = $dba->dbc()->prepare(
"select external_db_id, db_name, db_display_name from external_db");
129 my $insert_external_db_info_sth = $dbh->prepare(
"insert into external_db_type (external_db_id, db_name, db_display_name) values (?,?,?)");
132 $external_db_info_sth->execute();
134 while ( my ($external_db_id, $db_name, $db_display_name) = $external_db_info_sth->fetchrow_array() ) {
136 $insert_external_db_info_sth->execute($external_db_id, $db_name, $db_display_name);
139 $external_db_info_sth->finish();
140 $insert_external_db_info_sth->finish();
142 #populate db types in external_db_type
144 my $cmd =
"mysql -h $xhost";
146 $cmd .=
" -P $xport";
148 $cmd .=
" -u $xuser --password=$xpass $xdbname < $SERVERROOT/sql/update.sql";
149 system($cmd) == 0 or die(
"error encountered when updating database $xdbname\n");
152 #get distinct db_names for an object type
153 my $external_db_sth = $dba->dbc()->prepare(
"select distinct external_db_id from object_xref join xref using(xref_id) join external_db using(external_db_id) where ensembl_object_type = ? and info_type != 'UNMAPPED' order by db_name");
155 #get xref types for an object type and external db
156 my $xref_types_sth = $dba->dbc()->prepare(
"select distinct info_type from object_xref join xref using(xref_id) where ensembl_object_type = ? and external_db_id = ? and info_type != 'UNMAPPED'");
158 #get master xref db_names
159 my $master_db_sth = $dba->dbc()->prepare(
"select distinct e.external_db_id from object_xref ox join xref x1 on (ox.xref_id = x1.xref_id) join dependent_xref on (x1.xref_id = dependent_xref_id) join xref x2 on (master_xref_id = x2.xref_id) join external_db e on (x2.external_db_id = e.external_db_id) where ensembl_object_type = ? and x1.external_db_id = ? and x1.info_type != 'UNMAPPED'");
161 #get species from which xrefs were projected
162 my $projected_from_species_sth = $dba->dbc()->prepare(
"select distinct substr(info_text,1,locate(' ', info_text, locate(' ', info_text)+1 )) from object_xref join xref using(xref_id) where info_text like 'from%' and ensembl_object_type = ? and external_db_id = ? and info_type = 'PROJECTION'");
165 #xref_mindmap db statements
167 my $insert_object_xref_linkage = $dbh->prepare(
"insert into object_xref_linkage values(?,?,?,?,?)");
169 my $insert_object_external_db_node = $dbh->prepare(
"insert into object_external_db_node values (?,?,?)");
171 my $link_type_sth = $dbh->prepare(
"select link_type_id from link_type where link_type = ?");
174 foreach my $object (@objects_with_xrefs) {
176 $external_db_sth->execute($object);
178 while (my ($db_id) = $external_db_sth->fetchrow_array() ) {
179 push @db_ids, $db_id;
182 foreach my $db_id (@db_ids) {
183 $xref_types_sth->execute($object, $db_id);
185 #store the xref db node
186 $insert_object_external_db_node->execute($object,$db_id, $object.$db_id);
188 while (my ($link_type) = $xref_types_sth->fetchrow_array() ) {
191 $link_type_sth->execute($link_type);
192 my ($link_type_id) = $link_type_sth->fetchrow_array();
196 if ($link_type eq
'DEPENDENT') {
198 $master_db_sth->execute($object,$db_id);
199 while (my ($master_db_id) = $master_db_sth->fetchrow_array() ) {
201 $insert_object_xref_linkage->execute($db_id,$object,$link_type_id, $master_db_id, undef);
204 } elsif ($link_type eq
'PROJECTION') {
206 $projected_from_species_sth->execute($object, $db_id);
207 while (my ($linked_node_text) = $projected_from_species_sth->fetchrow_array()) {
208 $insert_object_xref_linkage->execute($db_id,$object,$link_type_id, undef, $linked_node_text);
213 $insert_object_xref_linkage->execute($db_id,$object,$link_type_id, undef, undef);
226 #populate 'generated_from' link types in object_xref_linkage - this is for xrefs to do with gene and transcript naming
228 my $naming_dbs_sth = $dbh->prepare(
"select external_db_id, db_name from external_db_type where db_name like ?");
229 my $db_id_sth = $dbh->prepare(
"select external_db_id from external_db_type where db_name = ?");
231 $link_type_sth->execute(
'GENERATED_FROM');
232 my ($link_type_id) = $link_type_sth->fetchrow_array();
234 foreach my $object (@objects_with_xrefs) {
236 $naming_dbs_sth->execute(
'%'.$object.
'\_name');
237 while (my ($db_id, $db_name) = $naming_dbs_sth->fetchrow_array() ) {
239 if ($db_name =~ m/(.*)\_$object\_name/i) {
241 my $linked_db_name = $1;
242 if ($linked_db_name) {
244 #find the linked db id
245 $db_id_sth->execute($linked_db_name);
246 my ($linked_db_id) = $db_id_sth->fetchrow_array();
249 $insert_object_xref_linkage->execute($db_id,$object,$link_type_id, $linked_db_id, undef);
256 foreach my $name ( (
'Vega',
'Ensembl')) {
257 $naming_dbs_sth->execute(
'Clone\_based\_'.$name.
'\_'.$object);
258 my ($db_id, $db_name) = $naming_dbs_sth->fetchrow_array();
260 $insert_object_xref_linkage->execute($db_id,$object,$link_type_id, undef,
"$name clone name");
265 $naming_dbs_sth->finish();
269 $db_id_sth->finish();
271 $external_db_sth->finish();
272 $xref_types_sth->finish();
273 $master_db_sth->finish();
274 $projected_from_species_sth->finish();
276 $insert_object_xref_linkage->finish();
277 $insert_object_external_db_node->finish();
278 $link_type_sth->finish();
282 #create the .mm file based on info from xref_mindmap db
284 my $file_name = $species .
'_xrefs_'. $db_version .
'.mm';
286 open( FH,
">$file_name" ) or die("Can't open $file_name\n");
288 print STDOUT "Writing to file $file_name\n";
291 my $header = <<HEADER;
292 <
map version='0.9.0'>
293 <!--To view this file, download free mind mapping software Freeplane from http:
294 <node TEXT='External References' ID='ID_000000001' COLOR='
#18898b' STYLE='fork'>
295 <font NAME=
'Liberation Sans' SIZE=
'12' BOLD=
'true'/>
296 <hook NAME=
'MapStyle' max_node_width=
'600'/>
297 <edge STYLE=
'bezier' COLOR=
'#808080' WIDTH=
'thin'/>
303 my $category_and_db_name_nodes_sth = $dbh->prepare(
"select db_type, external_db_id, db_display_name, mindmap_tag_id from object_external_db_node join external_db_type using(external_db_id) join db_type using(db_type_id) where ensembl_object_type = ? order by db_type, db_display_name;");
305 my $db_linkage_types_sth = $dbh->prepare(
"select link_description, linked_external_db_id, linked_node_text from object_xref_linkage join link_type using(link_type_id) where ensembl_object_type = ? and external_db_id = ? and link_description is not null order by link_description, linked_external_db_id, linked_node_text");
307 #this is used to link to dbs from the closest level (e.g. if Orphanet xrefs for a Gene are dependent on HGNC xrefs
308 #we will try to link to the Gene HGNC node then transcript etc..
310 my $object_distance_sth = $dbh->prepare(
"select to_object from object_distance where from_object = ? order by distance");
312 my $node_id_sth = $dbh->prepare(
"select mindmap_tag_id from object_external_db_node where ensembl_object_type = ? and external_db_id = ?");
315 my $node_id_count = 1;
317 foreach my $object (@objects_with_xrefs) {
319 my $node_id =
'ID_'.$node_id_count;
320 #write out the object node
321 my $object_node =
"<node TEXT='".$object_display_names{$object}.
"' POSITION='right' ID='".$node_id.
"' COLOR='#cc3300' STYLE='fork'>
322 <font NAME='Liberation Sans' SIZE='12' BOLD='true'/>
323 <edge STYLE='bezier' COLOR='#808080' WIDTH='thin'/>
325 print FH $object_node;
327 $category_and_db_name_nodes_sth->execute($object);
329 my $last_type =
'none';
330 while (my ($db_type, $db_id, $db_display_name, $tag_id) = $category_and_db_name_nodes_sth->fetchrow_array() ) {
332 if ($db_type ne $last_type) {
334 if ($last_type ne
'none') {
335 print FH
"</node>\n"; #db type node end tag
338 $node_id =
'ID_'.$node_id_count;
339 #write out new db type node
340 my $db_type_node =
"<node TEXT='".encode_entities($db_type).
"' ID='".$node_id.
"' COLOR='#669900'>
341 <font NAME='Liberation Sans' SIZE='12' BOLD='true'/>
343 print FH $db_type_node;
346 #write out the db name node
347 $node_id_sth->execute($object,$db_id);
348 ($node_id) = $node_id_sth->fetchrow_array();
349 my $db_name_node =
"<node TEXT='".encode_entities($db_display_name).
"' ID='".$node_id.
"'>
351 print FH $db_name_node;
353 $db_linkage_types_sth->execute($object,$db_id);
355 my $last_link_desc =
'none';
356 while (my ($link_description, $linked_db_id, $linked_node_text)
357 = $db_linkage_types_sth->fetchrow_array() ){
359 if ($link_description ne $last_link_desc) {
360 if ($last_link_desc ne
'none') {
361 print FH
"</node>\n"; #link type node end tag
363 #write out the link node
365 $node_id =
'ID_'.$node_id_count;
366 my $link_node =
"<node TEXT='".encode_entities($link_description).
"' ID='".$node_id.
"'>
374 #find the node id to link to
376 $object_distance_sth->execute($object);
378 while (my ($object_type) = $object_distance_sth->fetchrow_array() ) {
379 push @closest_level, $object_type;
383 foreach my $object_type (@closest_level) {
385 $node_id_sth->execute($object_type,$linked_db_id);
386 ($linked_node_id) = $node_id_sth->fetchrow_array();
387 last
if ($linked_node_id);
390 if ($linked_node_id) {
391 #write out arrow link to the node
392 my $arrow_link_node =
"<arrowlink DESTINATION='".$linked_node_id.
"' STARTARROW='NONE' ENDARROW='DEFAULT'/>
394 print FH $arrow_link_node;
397 } elsif ($linked_node_text) {
401 $node_id =
'ID_'.$node_id_count;
402 my $node =
"<node TEXT='".encode_entities($linked_node_text).
"' ID='".$node_id.
"'/>
408 $last_link_desc = $link_description;
412 if ($last_link_desc ne
'none') {
413 print FH
"</node>\n"; #link type node end tag
417 print FH
"</node>\n"; #db name node end tag
419 $last_type = $db_type;
421 if ($last_type ne
'none') {
422 print FH
"</node>\n"; #db type node end tag
426 print FH
"</node>\n" #
object node end tag;
430 $category_and_db_name_nodes_sth->finish();
431 $db_linkage_types_sth->finish();
432 $object_distance_sth->finish();
433 $node_id_sth->finish();
436 #external references end tag and map end tag
437 my $footer = <<FOOTER;
448 print STDOUT
"Dropping database $xdbname\n";
450 $dbh->do(
"drop database if exists $xdbname");
453 die(
"An SQL error occured while dropping database $xdbname:\n$@");
463 my $indent =
' ' x length($0);
464 print <<EOF; exit(0);
466 The script populates a stable_id lookup database with all stable ids found in databases
467 on a specified server (or servers)
for a specified db release.
468 Stable ids are copied
for objects listed in hash %group_objects
470 Options -lhost -luser -lpass are mandatory and specify the credentials
for the server on which a stable
id lookup database exists or is to be created (
if using option -create). If an argument
for option -ldbname is not provided, the
default name
for the database wil be used:
'ensembl_stable_id_lookup_xx', where xx is the database release (option -db_version).
472 Options -host -user -port specify the credentials of the server(s) where stable ids are to be copied from.
474 To
run the script cd into the directory where the script lives eg:
475 cd ensembl/misc-scripts/stable_id_lookup/
478 This command will create database ensembl_stable_id_lookup_67 on server ens-staging1 and will copy stable ids from databases
for release 67 found on ens-staging1 and ens-staging2:
480 populate_stable_id_lookup.pl -lhost ens-staging1 -luser ensadmin -lpass xxxx -create -db_version 67 -host ens-staging1 -host ens-staging2 -user ensro
485 $0 -xhost host_name -xuser user_name -xpass password
486 $indent [-xport port_number]
487 $indent -host host_name -user user_name
488 $indent [-port port_number] [-db_version]
489 $indent -species species_name
495 -h|host Database host where the species db lives
497 -u|user Database user where the species db lives
499 -port Database port the species db lives
501 -xh|xhost Database host where the xref mindmap db is to be created
503 -xu|xuser Database user where the xref mindmap db is to be created
505 -xp|xpass Database password where the xref mindmap db is to be created
507 -xport Database port where the xref mindmap db is to be created
509 -s|species Species name to generate xref mindmap
for
511 -d|dontdrop Don\
't drop the xref_mindmap db at the end of the script
513 -db_version If not specified, software_version() returned by the ApiVersion module will be used