| 1 | #!/usr/bin/env perl | ||
| 2 | # -*-CPerl-*- | ||
| 3 | # Last changed Time-stamp: <2014-12-04 22:20:43 mtw> | ||
| 4 | # | ||
| 5 | # Create a new genome database for the UCSC genome browser. Based on | ||
| 6 | # http://genomewiki.ucsc.edu/index.php/Building_a_new_genome_database | ||
| 7 | # | ||
| 8 | # usage: newUCSCdb.pl -db <DBNAME> -fa <FASTA> -genome <GENOME> -tax <TAXID> | ||
| 9 | # -sname <SCIENTIFIC_NAME> -assembly <ASSEMBLY> -clade <CLADE> | ||
| 10 | # -pos <DEFAULT_POSITION> -source <SOURCE> -key <INT> | ||
| 11 | # | ||
| 12 | # | ||
| 13 | # EXAMPLE 1: | ||
| 14 | # newUCSCdb.pl -db eColiMC4100 -fa NC_012759.fna -tax 595496 -genome | ||
| 15 | # "Escherichia coli MC4100" -assembly "2013-09-09" -pos | ||
| 16 | # "NC_012759.1:230000-867000" -key 204 -sname "Escherichia coli BW2952 | ||
| 17 | # uid 59391" -source "based on RefSeq NC_012759.1" -clade "bacteria" | ||
| 18 | # -h | ||
| 19 | # | ||
| 20 | # EXAMPLE 2: | ||
| 21 | # newUCSCdb.pl -db stenMaltK279a -tax 522373 -genome "Stenotrophomonas | ||
| 22 | # maltophilia K279a" -assembly "2008-06-10" -pos "NC_010943.1: | ||
| 23 | # 100000-200000" -fa NC_010943.fna -sname "Stenotrophomonas | ||
| 24 | # maltophilia K279a uid 61647" -key 223 -source "based on RefSeq | ||
| 25 | # NC_010943.1" -clade "bacteria" -h | ||
| 26 | |||
| 27 | # *********************************************************************** | ||
| 28 | # * Copyright notice | ||
| 29 | # * | ||
| 30 | # * Copyright 2013 Michael T. Wolfinger <michael@wolfinger.eu> | ||
| 31 | # * All rights reserved | ||
| 32 | # * | ||
| 33 | # * This program is free software: you can redistribute it and/or modify | ||
| 34 | # * it under the terms of the GNU General Public License as published by | ||
| 35 | # * the Free Software Foundation, either version 3 of the License, or | ||
| 36 | # * (at your option) any later version. | ||
| 37 | # * | ||
| 38 | # * This program is distributed in the hope that it will be useful, | ||
| 39 | # * but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
| 40 | # * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
| 41 | # * GNU General Public License for more details. | ||
| 42 | # * | ||
| 43 | # * You should have received a copy of the GNU General Public License | ||
| 44 | # * along with this program. If not, see <http://www.gnu.org/licenses/>. | ||
| 45 | # * | ||
| 46 | # * This copyright notice MUST APPEAR in all copies of the script! | ||
| 47 | # *********************************************************************** | ||
| 48 | # | ||
| 49 | |||
| 50 | use strict; | ||
| 51 | use warnings; | ||
| 52 | use Getopt::Long; | ||
| 53 | use Data::Dumper; | ||
| 54 | use File::Basename; | ||
| 55 | use POSIX qw(strftime); | ||
| 56 | |||
| 57 | #^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^# | ||
| 58 | #^^^^^^^^^^ Variables ^^^^^^^^^^^# | ||
| 59 | #^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^# | ||
| 60 | |||
| 61 | my $timestamp = strftime("%Y%m%d-%H%M", localtime(time)); # timestamp | ||
| 62 | my $awk = "awk"; # path to awk | ||
| 63 | my $hgsql = "hgsql"; # path to hgsql | ||
| 64 | my $hgFakeAgp = "hgFakeAgp "; # path to hgFakeAgp | ||
| 65 | my $faToTwoBit = "faToTwoBit "; # path to faToTwoBit | ||
| 66 | my $twoBitInfo = "twoBitInfo "; # path to twoBitInfo | ||
| 67 | my $hgLoadSqlTab = "hgLoadSqlTab "; # path to hgLoadSqlTab | ||
| 68 | my $hgGcPercent = "hgGcPercent "; | ||
| 69 | my $hgLoadWiggle = "hgLoadWiggle "; | ||
| 70 | my $hgGoldGapGl = "hgGoldGapGl "; | ||
| 71 | my $log = "newUCSCdb.log"; | ||
| 72 | my $db = my $fa = my $taxid = my $genome = my $sname = my $clade = undef; | ||
| 73 | my $assembly = my $position = my $orderKey = my $source = undef; | ||
| 74 | |||
| 75 | |||
| 76 | #^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^# | ||
| 77 | #^^^^^^^^^^^^^^ Main ^^^^^^^^^^^^^# | ||
| 78 | #^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^# | ||
| 79 | |||
| 80 | Getopt::Long::config('no_ignore_case'); | ||
| 81 | &usage() unless GetOptions("db=s" => \$db, | ||
| 82 | "fa=s" => \$fa, | ||
| 83 | "tax=s" => \$taxid, | ||
| 84 | "genome=s" => \$genome, | ||
| 85 | "sname=s" => \$sname, | ||
| 86 | "assembly=s" => \$assembly, | ||
| 87 | "pos=s" => \$position, | ||
| 88 | "key=i" => \$orderKey, | ||
| 89 | "source=s" => \$source, | ||
| 90 | "clade=s" => \$clade, | ||
| 91 | "-help" => \&usage, | ||
| 92 | "v"); | ||
| 93 | |||
| 94 | unless ((defined $db) && (defined $fa) && (defined $taxid) && (defined $genome)) { | ||
| 95 | die "Please provide command-line arguments for -db / -fa / -tax / -genome \n"; | ||
| 96 | } | ||
| 97 | unless (defined $sname) { | ||
| 98 | $sname = $genome; | ||
| 99 | } | ||
| 100 | unless (defined $assembly) { | ||
| 101 | $assembly = "Jan 2041"; | ||
| 102 | } | ||
| 103 | unless (defined $position) { | ||
| 104 | $position = "chr1:500000-1000000"; | ||
| 105 | } | ||
| 106 | unless (defined $orderKey) { | ||
| 107 | $orderKey = 9999; | ||
| 108 | } | ||
| 109 | unless (defined $source) { | ||
| 110 | $source = "new genome assembly"; | ||
| 111 | } | ||
| 112 | unless (defined $clade) { | ||
| 113 | $clade = "other"; # mammal / vertebrate / plants / bacteria | ||
| 114 | } | ||
| 115 | |||
| 116 | make_ucsc_db(); | ||
| 117 | |||
| 118 | |||
| 119 | #^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^# | ||
| 120 | #^^^^^^^^^^^ Subroutines ^^^^^^^^^^# | ||
| 121 | #^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^# | ||
| 122 | |||
| 123 | sub make_ucsc_db { | ||
| 124 | my $cmd; | ||
| 125 | my @log = (); | ||
| 126 | open my $logfile, ">", $log; | ||
| 127 | |||
| 128 | # log header | ||
| 129 | push @log, "*** START $timestamp\n"; | ||
| 130 | |||
| 131 | # create agp file | ||
| 132 | $cmd = $hgFakeAgp." -minContigGap=1 ".$fa." $db.agp\n"; | ||
| 133 | system($cmd); | ||
| 134 | push @log, $cmd; push @log, "\n"; | ||
| 135 | |||
| 136 | # convert fasta to 2bit | ||
| 137 | $cmd = "$faToTwoBit $fa $db.2bit\n"; | ||
| 138 | $cmd .= "mkdir -p /gbdb/$db/html\n"; | ||
| 139 | $cmd .= "cp $db.2bit /gbdb/$db/\n"; | ||
| 140 | system($cmd); | ||
| 141 | push @log, $cmd; push @log, "\n"; | ||
| 142 | |||
| 143 | # create chromInfo table | ||
| 144 | $cmd = "$twoBitInfo $db.2bit stdout | sort -k2nr > chrom.sizes\n"; | ||
| 145 | $cmd .= "mkdir -p bed/chromInfo\n"; | ||
| 146 | $cmd .= "awk \' {printf \"%s\\t%d\\t/gbdb/$db/$db.2bit\\n\", \$1, \$2}\' chrom.sizes > bed/chromInfo/chromInfo.tab\n"; | ||
| 147 | system($cmd); | ||
| 148 | push @log, $cmd; push @log, "\n"; | ||
| 149 | |||
| 150 | # start a new database | ||
| 151 | $cmd = "hgsql -e \"CREATE DATABASE IF NOT EXISTS $db;\" mysql\n"; | ||
| 152 | system($cmd); | ||
| 153 | push @log, $cmd; push @log, "\n"; | ||
| 154 | |||
| 155 | # load grp table; | ||
| 156 | $cmd = "hgsql $db < \$HOME/kent/src/hg/lib/grp.sql\n"; | ||
| 157 | system($cmd); | ||
| 158 | push @log, $cmd; push @log, "\n"; | ||
| 159 | |||
| 160 | # load chromInfo table; | ||
| 161 | $cmd = "$hgLoadSqlTab $db chromInfo \$HOME/kent/src/hg/lib/chromInfo.sql bed/chromInfo/chromInfo.tab\n"; | ||
| 162 | system($cmd); | ||
| 163 | push @log, $cmd; push @log, "\n"; | ||
| 164 | |||
| 165 | # load gold and gap tables | ||
| 166 | $cmd = "$hgGoldGapGl $db $db.agp\n"; | ||
| 167 | system($cmd); | ||
| 168 | push @log, $cmd; push @log, "\n"; | ||
| 169 | |||
| 170 | # generate gc5Base data and load the table | ||
| 171 | $cmd = "mkdir -p bed/gc5Base\n"; | ||
| 172 | $cmd .= "$hgGcPercent -wigOut -doGaps -file=gc5.wig -win=5 -verbose=0 $db $db.2bit \n"; | ||
| 173 | $cmd .= "wigEncode gc5.wig bed/gc5Base/gc5Base.wig bed/gc5Base/gc5Base.wib \n"; | ||
| 174 | $cmd .= "$hgLoadWiggle $db gc5Base bed/gc5Base/gc5Base.wig\n"; | ||
| 175 | $cmd .= "mkdir -p /gbdb/$db/wib\n"; | ||
| 176 | $cmd .= "cp bed/gc5Base/gc5Base.wib /gbdb/$db/wib\n"; | ||
| 177 | system($cmd); | ||
| 178 | push @log, $cmd; push @log, "\n"; | ||
| 179 | |||
| 180 | # create dbDbInsert.sql | ||
| 181 | open my $dbdb, ">", "dbDbInsert.sql"; | ||
| 182 | print $dbdb "INSERT INTO dbDb\n"; | ||
| 183 | print $dbdb " (name, description, nibPath, organism,\n"; | ||
| 184 | print $dbdb " defaultPos, active, orderKey, genome, scientificName,\n"; | ||
| 185 | print $dbdb " htmlPath, hgNearOk, hgPbOk, sourceName, taxId)\n"; | ||
| 186 | print $dbdb "VALUES\n"; | ||
| 187 | print $dbdb " (\"$db\", \"$assembly\", \"/gbdb/$db\", \"$genome\",\n"; | ||
| 188 | print $dbdb " \"$position\", 1, $orderKey, \"$genome\", \"$sname\",\n"; | ||
| 189 | print $dbdb " \"/gbdb/$db/html/description.html\", 0, 0, \"$source\", $taxid);\n"; | ||
| 190 | close $dbdb; | ||
| 191 | |||
| 192 | # load dbDbInsert.sql into database | ||
| 193 | $cmd = "hgsql hgcentral < dbDbInsert.sql\n"; | ||
| 194 | system($cmd); | ||
| 195 | push @log, $cmd; push @log, "\n"; | ||
| 196 | |||
| 197 | # create defaultDb entry | ||
| 198 | $cmd = "hgsql hgcentral -e \'INSERT INTO defaultDb (genome, name) VALUES (\"$genome\",\"$db\");\'\n"; | ||
| 199 | system($cmd); | ||
| 200 | push @log, $cmd; push @log, "\n"; | ||
| 201 | |||
| 202 | # create genomeClade entry | ||
| 203 | $cmd = "hgsql hgcentral -e 'INSERT INTO genomeClade (genome, clade, priority) VALUES (\"$genome\", \"$clade\", $orderKey);\'\n"; | ||
| 204 | system($cmd); | ||
| 205 | push @log, $cmd; push @log, "\n"; | ||
| 206 | |||
| 207 | # log footer | ||
| 208 | push @log, "*** END $timestamp\n"; | ||
| 209 | |||
| 210 | print $logfile @log; | ||
| 211 | close $log; | ||
| 212 | |||
| 213 | } | ||
| 214 | |||
| 215 | |||
| 216 | sub usage { | ||
| 217 | print <<EOF; | ||
| 218 | Depoly a new UCSC genome browser database | ||
| 219 | |||
| 220 | usage: $0 [options] | ||
| 221 | program specific options: default: | ||
| 222 | -db <string> new database name ($db) | ||
| 223 | -fa <string> (multi) fasta file holding sequence ($fa) | ||
| 224 | -tax <string> taxonomy ID ($taxid) | ||
| 225 | -genome <string> genome name ($genome) | ||
| 226 | -sname <string> scientific name ($sname) | ||
| 227 | -assembly <string> assembly ($assembly) | ||
| 228 | -pos <string> default position ($position) | ||
| 229 | -key <int> order key ($orderKey) | ||
| 230 | -source <string> genome/assembly/annotation source ($source) | ||
| 231 | -clade <string> genome clade (mammal,plants,bateria,...) ($clade) | ||
| 232 | -help print this information | ||
| 233 | EOF | ||
| 234 | exit; | ||
| 235 | } |