# for emacs: -*- mode: sh; -*-

# This file describes browser build for the xenTro9

#########################################################################
# reuse photograph obtained for xenTro previous versions
#    (DONE - 2017-03-27 - Hiram)

mkdir /hive/data/genomes/xenTro9
cd /hive/data/genomes/xenTro9
cp -p ../xenTro7/photoReference.txt .

cat photoReference.txt

photoCreditURL  http://www.unc.edu/
photoCreditName UNC Chapel Hill, Chris Showell, all rights reserved

#########################################################################
#  Initial steps (DONE - 2017-03-27 - Hiram)

# To start this initialBuild.txt document, from a previous assembly document:

mkdir ~/kent/src/hg/makeDb/doc/xenTro9
cd ~/kent/src/hg/makeDb/doc/xenTro9

# best to use a most recent document since it has the latest features and
# procedures:
sed -e 's/micMur3/xenTro9/g; s/MicMur3/XenTro9/g; s/DONE/TBD/g;' \
    ../micMur3/initialBuild.txt > initialBuild.txt

mkdir /hive/data/genomes/xenTro9/refseq
cd /hive/data/genomes/xenTro9/refseq

rsync -L -a -P \
rsync://ftp.ncbi.nlm.nih.gov/genomes/refseq/vertebrate_other/Xenopus_tropicalis/all_assembly_versions/GCF_000004195.3_Xenopus_tropicalis_v9.1/ ./

# sent 1555 bytes  received 2014571885 bytes  16858355.15 bytes/sec
# total size is 2014319744  speedup is 1.00
# real    1m58.966s

# check assembly size for later reference:

faSize G*v9.1_genomic.fna.gz

# 1440398454 bases (70533089 N's 1369865365 real 840265201 upper 529600164
#    lower) in 6822 sequences in 1 files
# Total size: mean 211140.2 sd 5118536.3 min 226 (NW_016690147.1)
#    max 194866763 (NC_030677.1) median 4812
# %36.77 masked total, %38.66 masked real

# this information is from the top of 
#    xenTro9/refseq/GCF_000004195.3_Xenopus_tropicalis_v9.1_assembly_report.txt

# Assembly name:  Xenopus_tropicalis_v9.1
# Organism name:  Xenopus tropicalis (tropical clawed frog)
# Infraspecific name:  strain=Nigerian
# Sex:  female
# Taxid:          8364
# BioSample:      SAMN00000117
# BioProject:     PRJNA205740
# Submitter:      DOE Joint Genome Institute
# Date:           2016-7-13
# Assembly type:  haploid
# Release type:   major
# Assembly level: Chromosome
# Genome representation: full
# WGS project:    AAMC03
# Assembly method: Meraculous v. May-2013
# Genome coverage: 7.6x
# Sequencing technology: Illumina HiSeq 2000
# RefSeq category: Representative Genome
# GenBank assembly accession: GCA_000004195.3
# RefSeq assembly accession: GCF_000004195.3
# RefSeq assembly and GenBank assemblies identical: no
#
## Assembly-Units:
## GenBank Unit Accession       RefSeq Unit Accession   Assembly-Unit name
## GCA_000004205.3      GCF_000004205.3 Primary Assembly
##      GCF_000005075.1 non-nuclear

#############################################################################
# establish config.ra file (DONE - Hiram - 2017-03-27)
    # arguments here are: <db> <clade> <trackDbDir> <assembly_report.txt>
    cd /hive/data/genomes/xenTro9
    $HOME/kent/src/hg/utils/automation/prepConfig.pl xenTro9 vertebrate \
        xenTro ./refseq/*_assembly_report.txt > xenTro9.config.ra

    # fixup common name to remain compatible with previous xenTro versions:
    # and orderKey wasn't correct

    # verify it looks sane
    cat xenTro9.config.ra
# config parameters for makeGenomeDb.pl:
db xenTro9
clade vertebrate
# genomeCladePriority 70
scientificName Xenopus tropicalis
commonName X. tropicalis
assemblyDate Jul. 2016
assemblyLabel DOE Joint Genome Institute
assemblyShortLabel Xenopus_tropicalis_v9.1
orderKey 24036
# mitochondrial sequence included in refseq release
# mitoAcc NC_006839.1
mitoAcc none
fastaFiles /hive/data/genomes/xenTro9/ucsc/*.fa.gz
agpFiles /hive/data/genomes/xenTro9/ucsc/*.agp
# qualFiles none
dbDbSpeciesDir xenTro
photoCreditURL  http://www.unc.edu/
photoCreditName UNC Chapel Hill, Chris Showell, all rights reserved
ncbiGenomeId 80
ncbiAssemblyId 768701
ncbiAssemblyName Xenopus_tropicalis_v9.1
ncbiBioProject 205740
ncbiBioSample SAMN00000117
genBankAccessionID GCF_000004195.3
taxId 8364

#############################################################################
# setup UCSC named files (DONE - 2017-03-27 - Hiram)

    mkdir /hive/data/genomes/xenTro9/ucsc
    cd /hive/data/genomes/xenTro9/ucsc

    # check for duplicate sequences:
    time faToTwoBit -noMask ../refseq/G*v9.1_genomic.fna.gz refseq.2bit
    #  real    0m44.569s

    twoBitDup refseq.2bit
    # no output is a good result, otherwise, would have to eliminate duplicates
    # the scripts creating the fasta here will be using this refseq.2bit file

    ~/kent/src/hg/utils/automation/ucscCompositeAgp.pl \
       ../refseq/G*v9.1_genomic.fna.gz \
          ../refseq/G*v9.1_assembly_structure/Primary_Assembly
# NC_030677.1 chr1
# NC_030678.1 chr2
# NC_030679.1 chr3
# NC_030680.1 chr4
# NC_030681.1 chr5
# NC_030682.1 chr6
# NC_030683.1 chr7
# NC_030684.1 chr8
# NC_030685.1 chr9
# NC_030686.1 chr10

    time ~/kent/src/hg/utils/automation/unplacedWithChroms.pl \
       ../refseq/*_assembly_structure/Primary_Assembly
# processed 6811 sequences into chrUn.fa.gz
# real    2m25.232s

# there are no unlocalized sequences
#    time ~/kent/src/hg/utils/automation/unlocalizedWithChroms.pl \
#       ../refseq/*_assembly_structure/Primary_Assembly

    # bash syntax here
    mitoAcc=`grep "^# mitoAcc" ../xenTro9.config.ra | awk '{print $NF}'`
    printf "# mitoAcc %s\n" "$mitoAcc"
# mitoAcc NC_006839.1

    zcat \
  ../refseq/*_assembly_structure/non-nuclear/assem*/AGP/chrMT.comp.agp.gz \
     | grep -v "^#" | sed -e "s/^$mitoAcc/chrM/;" > chrM.agp

    printf ">chrM\n" > chrM.fa
    twoBitToFa -noMask refseq.2bit:$mitoAcc stdout | grep -v "^>" >> chrM.fa
    gzip chrM.fa

    # verify fasta and AGPs agree
    time faToTwoBit *.fa.gz test.2bit
    # real    1m13.235s

    time cat *.agp | checkAgpAndFa stdin test.2bit 2>&1 | tail -4
    # All AGP and FASTA entries agree - both files are valid
    # real    0m9.944s

    # and no sequence lost from orginal:
    twoBitToFa test.2bit stdout | faSize stdin
# 1440398454 bases (70533089 N's 1369865365 real 1369865365 upper 0 lower)
#	in 6822 sequences in 1 files
# Total size: mean 211140.2 sd 5118536.3 min 226 (chrUn_NW_016690147v1)
#	max 194866763 (chr1) median 4812

    # same numbers as above
# 1440398454 bases (70533089 N's 1369865365 real 840265201 upper 529600164
#    lower) in 6822 sequences in 1 files
# Total size: mean 211140.2 sd 5118536.3 min 226 (NW_016690147.1)
#    max 194866763 (NC_030677.1) median 4812

    # no longer need these temporary 2bit files
    rm refseq.2bit test.2bit

#############################################################################
#  Initial database build (DONE - 2017-03-27 - Hiram)

    cd /hive/data/genomes/xenTro9
    # verify sequence and AGP are OK:
    time (makeGenomeDb.pl -workhorse=hgwdev -dbHost=hgwdev -fileServer=hgwdev \
         -stop=agp xenTro9.config.ra) > agp.log 2>&1
    # real    2m43.431s

    # then finish it off:
    time (makeGenomeDb.pl -workhorse=hgwdev -dbHost=hgwdev \
       -fileServer=hgwdev -continue=db xenTro9.config.ra) > db.log 2>&1
    # real    20m35.233s

    # was missing some business in the script for trackDb:
    time (makeGenomeDb.pl -workhorse=hgwdev -dbHost=hgwdev \
     -fileServer=hgwdev -continue=trackDb xenTro9.config.ra) > trackDb.log 2>&1

    # check in the trackDb files created in TemporaryTrackDbCheckout/
    #    and add xenTro9 to trackDb/makefile

    # temporary symlink until masked sequence is available
    cd /hive/data/genomes/xenTro9
    ln -s `pwd`/xenTro9.unmasked.2bit /gbdb/xenTro9/xenTro9.2bit

##############################################################################
# cpgIslands on UNMASKED sequence (DONE - 2017-03-27 - Hiram)
    mkdir /hive/data/genomes/xenTro9/bed/cpgIslandsUnmasked
    cd /hive/data/genomes/xenTro9/bed/cpgIslandsUnmasked

    time (doCpgIslands.pl -dbHost=hgwdev -bigClusterHub=ku -buildDir=`pwd` \
       -tableName=cpgIslandExtUnmasked \
          -maskedSeq=/hive/data/genomes/xenTro9/xenTro9.unmasked.2bit \
             -workhorse=hgwdev -smallClusterHub=ku xenTro9) > do.log 2>&1
    # real    41m47.778s

    cat fb.xenTro9.cpgIslandExtUnmasked.txt
    # 14956579 bases of 1369865365 (1.092%) in intersection

#############################################################################
# cytoBandIdeo - (DONE - 2017-03-27 - Hiram)
    mkdir /hive/data/genomes/xenTro9/bed/cytoBand
    cd /hive/data/genomes/xenTro9/bed/cytoBand
    makeCytoBandIdeo.csh xenTro9

#########################################################################
# ucscToINSDC and ucscToRefSeq table/track (DONE - 2017-03-27 - Hiram)
    # the sequence here is working for a 'refseq' assembly
    # beware of a chrM situation may be specific depending upon what is
    # available in the assembly

    mkdir /hive/data/genomes/xenTro9/bed/ucscToINSDC
    cd /hive/data/genomes/xenTro9/bed/ucscToINSDC

    # find accession for chrM
    grep chrM ../../xenTro9.agp
# chrM    1       17610   1       O       NC_006839.1     1       17610   +

    # find the genbank accession for NC_006839.1 at Entrez nucleotide
    # The NC_006839.1 name is the RefSeq name, the genbank name is: AY789013.1
    # the assembly_report does not have this AY name since the chrM sequence
    # is not in the genbank assembly:
    grep NC_006839.1 ../../refseq/GCF*v9.1_assembly_report.txt
# MT      assembled-molecule      MT      Mitochondrion   na      <>      NC_006839.1     non-nuclear     17610   chrM

    # if there is a chrM, use its INSDC name as a second argument:
    # this is a RefSeq assembly, use the chrM refSeq name:
    ~/kent/src/hg/utils/automation/ucscToINSDC.sh \
      ../../refseq/GCF_*structure/Primary_Assembly NC_028718.1

    # this is actually ucscToRefSeq since this is a RefSeq assembly
    sort -k2 ucscToINSDC.txt > ucscToRefSeq.txt
    rm -f ucscToINSDC.txt
    awk '{printf "%s\t%s\n", $2, $1}' ucscToRefSeq.txt \
       | sort > refSeqToUcsc.txt

    # chrM processing needs special help, fixup with the sed
    # extract the refseq vs. genbank names from the assembly_report
    # columns 5 and 7 are the INSDC and RefSeq names
    grep -v "^#" ../../refseq/GCF*_assembly_report.txt | cut -f5,7 \
      | awk '{printf "%s\t%s\n", $2, $1}' | sed -e 's/na/AY789013.1/' \
          | sort > refseq.insdc.txt

    awk '{printf "%s\t0\t%d\n", $1,$2}' ../../chrom.sizes \
         | sort > ucsc.coordinate.tab
    join -2 2 refseq.insdc.txt ucscToRefSeq.txt | tr '[ ]' '[\t]' | sort -k3 \
       | join -2 3 ucsc.coordinate.tab - | tr '[ ]' '[\t]' | cut -f1-4 \
           > ucscToRefSeq.bed
    printf "chrM\t0\t17610\tNC_006839.1\n" >> ucscToRefSeq.bed

    join -2 2 refseq.insdc.txt ucscToRefSeq.txt | tr '[ ]' '[\t]' | sort -k3 \
       | join -2 3 ucsc.coordinate.tab - | tr '[ ]' '[\t]' | cut -f1-3,5 \
           > ucscToINSDC.bed
    printf "chrM\t0\t17610\tAY789013.1\n" >> ucscToINSDC.bed

    # verify chrM is correct:
    grep chrM *.bed
    # ucscToINSDC.bed:chrM    AY789013.1
    # ucscToRefSeq.bed:chrM   NC_006839.1


    # should be same line counts throughout:
    # in this case one is missing in the final result due to the duplicate
    # contig being removed
    wc -l *
    #	6822 refSeqToUcsc.txt
    #	6822 refseq.insdc.txt
    #	6822 ucsc.coordinate.tab
    #	6822 ucscToINSDC.bed
    #	6822 ucscToRefSeq.bed
    #	6822 ucscToRefSeq.txt

    export chrSize=`cut -f1 ucscToINSDC.bed | awk '{print length($0)}' | sort -n | tail -1`
    echo $chrSize
    # 20
    # use the 25 in this sed
    sed -e "s/21/$chrSize/" $HOME/kent/src/hg/lib/ucscToINSDC.sql \
         | hgLoadSqlTab xenTro9 ucscToINSDC stdin ucscToINSDC.bed
    # should be the same for ucscToRefSeq:
    export chrSize=`cut -f1 ucscToRefSeq.bed | awk '{print length($0)}' | sort -n | tail -1`
    echo $chrSize
    #  20
    sed -e "s/21/$chrSize/" $HOME/kent/src/hg/lib/ucscToINSDC.sql \
       | sed -e 's/INSDC/RefSeq/g;' > ucscToRefSeq.sql
    hgLoadSqlTab xenTro9 ucscToRefSeq ./ucscToRefSeq.sql ucscToRefSeq.bed

    # checkTableCoords should be silent
    checkTableCoords xenTro9
    # each should cover %100 entirely:
    featureBits -countGaps xenTro9 ucscToINSDC
    # 1440398454 bases of 1440398454 (100.000%) in intersection

    featureBits -countGaps xenTro9 ucscToRefSeq
    # 1440398454 bases of 1440398454 (100.000%) in intersection

#########################################################################
# add chromAlias table (DONE - 2017-03-27 - Hiram)

    mkdir /hive/data/genomes/xenTro9/bed/chromAlias
    cd /hive/data/genomes/xenTro9/bed/chromAlias

    hgsql -N -e 'select chrom,name,"refseq" from ucscToRefSeq;' xenTro9 \
        > ucsc.refseq.tab
    hgsql -N -e 'select chrom,name,"genbank" from ucscToINSDC;' xenTro9 \
        > ucsc.genbank.tab

    awk '{printf "%s\t%s\t%s\n", $2,$1,$3}' ucsc.genbank.tab ucsc.refseq.tab \
        | sort > xenTro9.chromAlias.tab

    hgLoadSqlTab xenTro9 chromAlias ~/kent/src/hg/lib/chromAlias.sql \
        xenTro9.chromAlias.tab

#########################################################################
# fixup search rule for assembly track/gold table (DONE - 2017-03-28 - Hiram)
    cd ~/kent/src/hg/makeDb/trackDb/xenTro/xenTro9

    # preview prefixes and suffixes:
    hgsql -N -e "select frag from gold;" xenTro9 \
      | sed -e 's/[0-9][0-9]*//;' | sort | uniq -c 
#  61329 AAMC.1
#      1 NC_.1

    # implies a rule: '[AN][AC][M_][C0-9]+(\.[0-9]+)?'

    # verify this rule will find them all and eliminate them all:
    hgsql -N -e "select frag from gold;" xenTro9 | wc -l
    # 61330

    hgsql -N -e "select frag from gold;" xenTro9 \
       | egrep -e '[AN][AC][M_][C0-9]+(\.[0-9]+)?' | wc -l
    # 61330

    hgsql -N -e "select frag from gold;" xenTro9 \
       | egrep -v -e '[AN][AC][M_][C0-9]+(\.[0-9]+)?' | wc -l
    # 0

    # hence, add to trackDb/chicken/xenTro9/trackDb.ra
searchTable gold
shortCircuit 1
termRegex [AN][AC][M_][C0-9]+(\.[0-9]+)?
query select chrom,chromStart,chromEnd,frag from %s where frag like '%s%%'
searchPriority 8

    # verify searches work in the position box

##########################################################################
# running repeat masker (DONE - 2017-03-27 - Hiram)
    mkdir /hive/data/genomes/xenTro9/bed/repeatMasker
    cd /hive/data/genomes/xenTro9/bed/repeatMasker
    time  (doRepeatMasker.pl -buildDir=`pwd` \
        -bigClusterHub=ku -dbHost=hgwdev -workhorse=hgwdev \
        -smallClusterHub=ku xenTro9) > do.log 2>&1 &
    # real    462m52.098s

    cat faSize.rmsk.txt
# 1440398454 bases (70533089 N's 1369865365 real 899399454 upper
#	470465911 lower) in 6822 sequences in 1 files
# Total size: mean 211140.2 sd 5118536.3 min 226 (chrUn_NW_016690147v1)
#	max 194866763 (chr1) median 4812
# %32.66 masked total, %34.34 masked real

    egrep -i "versi|relea" do.log
    # RepeatMasker version open-4.0.5
    #    January 31 2015 (open-4-0-5) version of RepeatMasker
    # CC   RELEASE 20140131;                                            *

    time featureBits -countGaps xenTro9 rmsk
    #	471365620 bases of 1440398454 (32.725%) in intersection
    #	real    0m20.783s

    # why is it different than the faSize above ?
    # because rmsk masks out some N's as well as bases, the faSize count above
    #   separates out the N's from the bases, it doesn't show lower case N's

    # faster way to get the same result on high contig count assemblies:
    time hgsql -N -e 'select genoName,genoStart,genoEnd from rmsk;' xenTro9 \
        | bedSingleCover.pl stdin | ave -col=4 stdin | grep "^total"
    #	total 471365620.000000
    #	real    0m16.203s

##########################################################################
# running simple repeat (DONE - 2017-03-27 - Hiram)

    mkdir /hive/data/genomes/xenTro9/bed/simpleRepeat
    cd /hive/data/genomes/xenTro9/bed/simpleRepeat
    # using trf409 3 here guessing smaller genome (human == 6)
    time (doSimpleRepeat.pl -buildDir=`pwd` -bigClusterHub=ku \
        -dbHost=hgwdev -workhorse=hgwdev -smallClusterHub=ku \
        -trf409 3 xenTro9) > do.log 2>&1 &
    # real    53m53.100s

    cat fb.simpleRepeat
    # 118499828 bases of 1369865365 (8.650%) in intersection

    # adding this trfMask to the other masking
    cd /hive/data/genomes/xenTro9

    # when using the Window Masker result:
#    twoBitMask bed/windowMasker/xenTro9.cleanWMSdust.2bit \
#       -add bed/simpleRepeat/trfMask.bed  xenTro9.2bit
    #   you can safely ignore the warning about fields >= 13

    # when using Rmsk results, add to rmsk after it is done:
    twoBitMask xenTro9.rmsk.2bit \
        -add bed/simpleRepeat/trfMask.bed xenTro9.2bit
    #   you can safely ignore the warning about fields >= 13

    twoBitToFa xenTro9.2bit stdout | faSize stdin > faSize.xenTro9.2bit.txt
    cat faSize.xenTro9.2bit.txt
# 1440398454 bases (70533089 N's 1369865365 real 898405355 upper
#	471460010 lower) in 6822 sequences in 1 files
# Total size: mean 211140.2 sd 5118536.3 min 226 (chrUn_NW_016690147v1)
#	max 194866763 (chr1) median 4812
# %32.73 masked total, %34.42 masked real

    # reset the symlink
    rm /gbdb/xenTro9/xenTro9.2bit
    ln -s `pwd`/xenTro9.2bit /gbdb/xenTro9/xenTro9.2bit

#########################################################################
# CREATE MICROSAT TRACK (DONE - 2017-03-27 - Hiram)
    ssh hgwdev
    mkdir /cluster/data/xenTro9/bed/microsat
    cd /cluster/data/xenTro9/bed/microsat

    awk '($5==2 || $5==3) && $6 >= 15 && $8 == 100 && $9 == 0 {printf("%s\t%s\t%s\t%dx%s\n", $1, $2, $3, $6, $16);}' \
       ../simpleRepeat/simpleRepeat.bed > microsat.bed

    hgLoadBed xenTro9 microsat microsat.bed
    # Read 13167 elements of size 4 from microsat.bed

##########################################################################
## WINDOWMASKER (DONE - 2017-03-27 - Hiram)

    mkdir /hive/data/genomes/xenTro9/bed/windowMasker
    cd /hive/data/genomes/xenTro9/bed/windowMasker
    time (doWindowMasker.pl -buildDir=`pwd` -workhorse=hgwdev \
        -dbHost=hgwdev xenTro9) > do.log 2>&1
    # real    129m31.454s

    # Masking statistics
    cat faSize.xenTro9.cleanWMSdust.txt
# 1440398454 bases (70533089 N's 1369865365 real 830396076 upper
#	539469289 lower) in 6822 sequences in 1 files
# Total size: mean 211140.2 sd 5118536.3 min 226 (chrUn_NW_016690147v1)
#	max 194866763 (chr1) median 4812
# %37.45 masked total, %39.38 masked real


    cat fb.xenTro9.rmsk.windowmaskerSdust.txt
    # 361644174 bases of 1440398454 (25.107%) in intersection

##########################################################################
# run up idKeys files for ncbiRefSeq (DONE - 2017-03-28 - Hiram)
    mkdir /hive/data/genomes/xenTro9/bed/idKeys
    cd /hive/data/genomes/xenTro9/bed/idKeys

    time (doIdKeys.pl -buildDir=`pwd`  xenTro9) > do.log 2>&1 &
    # real    12m20.939s

    cat xenTro9.keySignature.txt
    #   8ae219619932349bebc17364408bf9d0

##########################################################################
# cpgIslands - (DONE - 2017-03-28 - Hiram)
    mkdir /hive/data/genomes/xenTro9/bed/cpgIslands
    cd /hive/data/genomes/xenTro9/bed/cpgIslands
    time (doCpgIslands.pl -dbHost=hgwdev -bigClusterHub=ku \
      -workhorse=hgwdev -smallClusterHub=ku xenTro9) > do.log 2>&1 &
    # real    3m28.088s

    cat fb.xenTro9.cpgIslandExt.txt
    # 4735284 bases of 1369865365 (0.346%) in intersection

##############################################################################
# genscan - (DONE - 2017-03-28 - Hiram)
    mkdir /hive/data/genomes/xenTro9/bed/genscan
    cd /hive/data/genomes/xenTro9/bed/genscan
    time (doGenscan.pl -buildDir=`pwd` -workhorse=hgwdev -dbHost=hgwdev \
      -bigClusterHub=ku xenTro9) > do.log 2>&1 &
    # real    105m32.304s

    cat fb.xenTro9.genscan.txt
    # 49227165 bases of 1369865365 (3.594%) in intersection

    cat fb.xenTro9.genscanSubopt.txt
    # 37665356 bases of 1369865365 (2.750%) in intersection


#############################################################################
# augustus gene track (DONE - 2017-03-28 - Hiram)

    mkdir /hive/data/genomes/xenTro9/bed/augustus
    cd /hive/data/genomes/xenTro9/bed/augustus
    time (doAugustus.pl -buildDir=`pwd` -bigClusterHub=ku \
     -species=chicken -dbHost=hgwdev -workhorse=hgwdev xenTro9) > do.log 2>&1 &
    # real    79m25.311s

    cat fb.xenTro9.augustusGene.txt
    # 38938672 bases of 1369865365 (2.843%) in intersection


#############################################################################
# lastz/chain/net swap human/hg38 (DONE - 2017-03-30 - Hiram)
    # original alignment
    cd /hive/data/genomes/hg38/bed/lastzXenTro9.2017-03-29

    cat fb.hg38.chainXenTro9Link.txt
    # 117720401 bases of 3049335806 (3.861%) in intersection

    # and for the swap:
    mkdir /hive/data/genomes/xenTro9/bed/blastz.hg38.swap
    cd /hive/data/genomes/xenTro9/bed/blastz.hg38.swap

    time (doBlastzChainNet.pl -verbose=2 \
      /hive/data/genomes/hg38/bed/lastzXenTro9.2017-03-29/DEF \
        -swap -chainMinScore=5000 -chainLinearGap=loose \
          -workhorse=hgwdev -smallClusterHub=ku -bigClusterHub=ku \
            -syntenicNet) > swap.log 2>&1
    #  real    61m0.707s

    cat fb.xenTro9.chainHg38Link.txt
    # 108900066 bases of 1369865365 (7.950%) in intersection

    time (doRecipBest.pl -workhorse=hgwdev -buildDir=`pwd` xenTro9 hg38) \
       > rbest.log 2>&1
    # real    749m24.229s

#############################################################################
# lastz/chain/net swap mouse/mm10 (DONE - 2017-03-30 - Hiram)

    cd /hive/data/genomes/mm10/bed/lastzXenTro9.2017-03-29
    cat fb.mm10.chainXenTro9Link.txt
    #	87053836 bases of 2652783500 (3.282%) in intersection

    #	and for the swap
    mkdir /hive/data/genomes/xenTro9/bed/blastz.mm10.swap
    cd /hive/data/genomes/xenTro9/bed/blastz.mm10.swap
    time (doBlastzChainNet.pl -verbose=2 \
	/hive/data/genomes/mm10/bed/lastzXenTro9.2017-03-29/DEF \
        -workhorse=hgwdev -smallClusterHub=ku -bigClusterHub=ku \
        -syntenicNet -swap -chainMinScore=5000 -chainLinearGap=loose) \
	> swap.log 2>&1 &
    #	real    53m19.485s

    cat  fb.xenTro9.chainMm10Link.txt
    #	90150612 bases of 1369865365 (6.581%) in intersection

    time (doRecipBest.pl -workhorse=hgwdev -buildDir=`pwd` xenTro9 mm10) \
         > rbest.log 2>&1 &
    #	real    597m52.740s

#############################################################################
# Create kluster run files (DONE - 2017-03-28 - Hiram)

    # numerator is xenTro9 gapless bases "real" as reported by:
    featureBits -noRandom -noHap xenTro9 gap
    # 45444714 bases of 1227082306 (3.703%) in intersection
    #                   ^^^

    # denominator is hg19 gapless bases as reported by:
    #   featureBits -noRandom -noHap hg19 gap
    #     234344806 bases of 2861349177 (8.190%) in intersection
    # 1024 is threshold used for human -repMatch:
    calc \( 1227082306 / 2861349177 \) \* 1024
    #  ( 1227082306 / 2861349177 ) * 1024 = 439.139792

    # ==> use -repMatch=500 according to size scaled down from 1024 for human.
    #   and rounded up to nearest 100
    cd /hive/data/genomes/xenTro9
    blat xenTro9.2bit \
         /dev/null /dev/null -tileSize=11 -makeOoc=jkStuff/xenTro9.11.ooc \
        -repMatch=500
    #   Wrote 31375 overused 11-mers to jkStuff/xenTro9.11.ooc
    # xenTro7 was:
    #   Wrote 31229 overused 11-mers to jkStuff/xenTro7.11.ooc

    #   check non-bridged gaps to see what the typical size is:
    hgsql -N \
        -e 'select * from gap where bridge="no" order by size;' xenTro9 \
        | sort -k7,7nr | ave -col=7 stdin
    # there are no non-bridged gaps in this assembly

    # # all these gap sizes are 100
    # # minimum gap size is 100 and produces a reasonable number of lifts
    # gapToLift -verbose=2 -minGap=10 xenTro9 jkStuff/nonBridged.lft \
    #    -bedFile=jkStuff/nonBridged.bed

#########################################################################
# LIFTOVER TO xenTro7 (DONE - 2017-03-28 - Hiram)
    ssh hgwdev
    mkdir /hive/data/genomes/xenTro9/bed/blat.xenTro7.2017-03-27
    cd /hive/data/genomes/xenTro9/bed/blat.xenTro7.2017-03-27
    time (doSameSpeciesLiftOver.pl -verbose=2 -buildDir=`pwd` \
	-ooc=/hive/data/genomes/xenTro9/jkStuff/xenTro9.11.ooc \
        -bigClusterHub=ku -dbHost=hgwdev -workhorse=hgwdev \
         xenTro9 xenTro7) > do.log 2>&1
    # real    407m17.792s

    # verify the convert link on the test browser is now active from xenTro9 to
    # xenTro7

#########################################################################
# LIFTOVER TO xenTro3 (DONE - 2017-03-28 - Hiram)
    ssh hgwdev
    mkdir /hive/data/genomes/xenTro9/bed/blat.xenTro3.2017-03-28
    cd /hive/data/genomes/xenTro9/bed/blat.xenTro3.2017-03-28
    time (doSameSpeciesLiftOver.pl -verbose=2 -buildDir=`pwd` \
	-ooc=/hive/data/genomes/xenTro9/jkStuff/xenTro9.11.ooc \
        -bigClusterHub=ku -dbHost=hgwdev -workhorse=hgwdev \
         xenTro9 xenTro3) > do.log 2>&1
    # real    422m44.841s

    # verify the convert link on the test browser is now active from xenTro9 to
    # xenTro3

########################################################################
# GENBANK AUTO UPDATE (DONE - 2017-03-28 - Hiram)
    ssh hgwdev
    cd $HOME/kent/src/hg/makeDb/genbank
    git pull
    # /cluster/data/genbank/data/organism.lst shows:
    # #organism             mrnaCnt   estCnt  refSeqCnt
    # Xenopus tropicalis      18478   1271480 8612

    # edit etc/genbank.conf to add xenTro9 just before macFas5
# xenTro9 'Xenopus tropicalis' 11 chroms + 6811 contigs
xenTro9.serverGenome = /hive/data/genomes/xenTro9/xenTro9.2bit
xenTro9.clusterGenome = /hive/data/genomes/xenTro9/xenTro9.2bit
xenTro9.ooc = /hive/data/genomes/xenTro9/jkStuff/xenTro9.11.ooc
xenTro9.lift = no
xenTro9.perChromTables = no
xenTro9.downloadDir = xenTro9
# xenTro9.mgc = yes
xenTro9.refseq.mrna.native.pslCDnaFilter  = ${finished.refseq.mrna.native.pslCDnaFilter}
xenTro9.refseq.mrna.xeno.pslCDnaFilter    = ${finished.refseq.mrna.xeno.pslCDnaFilter}
xenTro9.genbank.mrna.native.pslCDnaFilter = ${finished.genbank.mrna.native.pslCDnaFilter}
xenTro9.genbank.mrna.xeno.pslCDnaFilter   = ${finished.genbank.mrna.xeno.pslCDnaFilter}
xenTro9.genbank.est.native.pslCDnaFilter  = ${finished.genbank.est.native.pslCDnaFilter}
# DO NOT NEED genbank.mrna.xeno except for human, mouse
# defaults yes: genbank.mrna.native.load, genbank.mrna.native.loadDesc,
# genbank.est.native.load, refseq.mrna.native.load, refseq.mrna.native.loadDesc,
# refseq.mrna.xeno.load , refseq.mrna.xeno.loadDesc
# xenTro7.upstreamGeneTbl = ensGene
# xenTro7.upstreamMaf = multiz9way /hive/data/genomes/xenTro7/bed/multiz9way/species.list

    git commit -m 'adding xenTro9 X, tropicalis refs #19151' etc/genbank.conf
    git push
    # update /cluster/data/genbank/:
    make etc-update

    cd /cluster/data/genbank

    time ./bin/gbAlignStep -initial xenTro9
    # logFile: var/build/logs/2017.03.28-10:19:21.xenTro9.initalign.log
    #   real    424m59.280s

    tail -2 var/build/logs/2017.03.28-10:19:21.xenTro9.initalign.log
    #	hgwdev 2017.03.28-17:22:09 xenTro9.initalign: Succeeded: xenTro9
    #   hgwdev 2017.03.28-17:24:20 xenTro9.initalign: finish

    #   To re-do, rm the dir first:
    #     /cluster/data/genbank/work/initial.xenTro9

    # load database when finished
    ssh hgwdev
    cd /cluster/data/genbank
    time ./bin/gbDbLoadStep -drop -initialLoad xenTro9
    # logFile: var/dbload/hgwdev/logs/2017.03.28-18:08:41.xenTro9.dbload.log
    #  real    30m42.283s

    tail -1 var/dbload/hgwdev/logs/2017.03.28-18:08:41.xenTro9.dbload.log
    #  hgwdev 2017.03.28-18:39:24 xenTro9.dbload: finish

    # enable daily alignment and update of hgwdev
    cd ~/kent/src/hg/makeDb/genbank
    git pull
    # add xenTro9 to:
    #   etc/align.dbs etc/hgwdev.dbs
    git add etc/align.dbs etc/hgwdev.dbs
    git commit -m 'adding xenTro9 to the update alignments refs #19151' etc/align.dbs etc/hgwdev.dbs
    git push
    make etc-update

#############################################################################
# ncbiRefSeq (TBD - 2016-05-13 - Hiram)

    mkdir /hive/data/genomes/xenTro9/bed/ncbiRefSeq
    cd /hive/data/genomes/xenTro9/bed/ncbiRefSeq
    # running step wise as this script is still under development
    time (~/kent/src/hg/utils/automation/doNcbiRefSeq.pl -buildDir=`pwd` \
      -bigClusterHub=ku -dbHost=hgwdev \
      -stop=download -fileServer=hgwdev -smallClusterHub=ku -workhorse=hgwdev \
      refseq vertebrate_other Gallus_gallus \
      GCF_000002315.4_Gallus_gallus-5.0 xenTro9) > download.log 2>&1
    # real    16m29.536s

    time (~/kent/src/hg/utils/automation/doNcbiRefSeq.pl -buildDir=`pwd` \
      -continue=process -bigClusterHub=ku -dbHost=hgwdev \
      -stop=process -fileServer=hgwdev -smallClusterHub=ku -workhorse=hgwdev \
      refseq vertebrate_other Gallus_gallus \
      GCF_000002315.4_Gallus_gallus-5.0 xenTro9) > process.log 2>&1
    # real    3m58.858s

    time (~/kent/src/hg/utils/automation/doNcbiRefSeq.pl -buildDir=`pwd` \
      -continue=load -bigClusterHub=ku -dbHost=hgwdev \
      -stop=load -fileServer=hgwdev -smallClusterHub=ku -workhorse=hgwdev \
      refseq vertebrate_other Gallus_gallus \
      GCF_000002315.4_Gallus_gallus-5.0 xenTro9) > load.log 2>&1
    # real    0m33.205s

    cat fb.ncbiRefSeq.xenTro9.txt
    #  82563006 bases of 1218501075 (6.776%) in intersection

    featureBits -enrichment xenTro9 refGene ncbiRefSeq 
    # refGene 1.181%, ncbiRefSeq 6.776%, both 1.175%, cover 99.49%,
    #    enrich 14.68x

#########################################################################
#  BLATSERVERS ENTRY (DONE - 2017-03-29 - Hiram)
#	After getting a blat server assigned by the Blat Server Gods,
    ssh hgwdev

    hgsql -e 'INSERT INTO blatServers (db, host, port, isTrans, canPcr) \
	VALUES ("xenTro9", "blat1d", "17884", "1", "0"); \
	INSERT INTO blatServers (db, host, port, isTrans, canPcr) \
	VALUES ("xenTro9", "blat1d", "17885", "0", "1");' \
	    hgcentraltest
    #	test it with some sequence

############################################################################
## reset default position to same as xenTro7
##  (DONE - 2017-03-29 - Hiram)

    ssh hgwdev
    hgsql -e 'update dbDb set defaultPos="chr9:15945953-15959305"
	where name="xenTro9";' hgcentraltest

#########################################################################
# all.joiner update, downloads and in pushQ - (TBD - 2017-03-06 - Hiram)
    cd $HOME/kent/src/hg/makeDb/schema
    # fixup all.joiner until this is a clean output
    joinerCheck -database=xenTro9 -tableCoverage all.joiner
    joinerCheck -database=xenTro9 -times all.joiner
    joinerCheck -database=xenTro9 -keys all.joiner

    cd /hive/data/genomes/xenTro9
    time (makeDownloads.pl -workhorse=hgwdev xenTro9) > downloads.log 2>&1
    #  real    14m41.395s


    #   now ready for pushQ entry
    mkdir /hive/data/genomes/xenTro9/pushQ
    cd /hive/data/genomes/xenTro9/pushQ
    time (makePushQSql.pl -redmineList xenTro9) > xenTro9.pushQ.sql 2> stderr.out
    #  real    7m21.629s

    #   check for errors in stderr.out, some are OK, e.g.:
    # WARNING: hgwdev does not have /gbdb/xenTro9/wib/gc5Base.wib
    # WARNING: hgwdev does not have /gbdb/xenTro9/wib/quality.wib
    # WARNING: hgwdev does not have /gbdb/xenTro9/bbi/quality.bw
    # WARNING: xenTro9 does not have seq
    # WARNING: xenTro9 does not have extFile

    #   copy it to hgwbeta
    scp -p xenTro9.pushQ.sql qateam@hgwbeta:/tmp/
    ssh qateam@hgwbeta "./bin/x86_64/hgsql qapushq < /tmp/xenTro9.pushQ.sql"

    #   in that pushQ entry walk through each entry and see if the
    #   sizes will set properly

#########################################################################
# LIFTOVER TO xenTro10 (DONE - 2021-02-22 - Hiram)
    ssh hgwdev
    mkdir /hive/data/genomes/xenTro9/bed/blat.xenTro10.2021-02-22
    cd /hive/data/genomes/xenTro9/bed/blat.xenTro10.2021-02-22
    doSameSpeciesLiftOver.pl -debug -verbose=2 -buildDir=`pwd` \
	-ooc=/hive/data/genomes/xenTro9/jkStuff/xenTro9.11.ooc \
        -bigClusterHub=ku -dbHost=hgwdev -workhorse=hgwdev \
         xenTro9 xenTro10
    time (doSameSpeciesLiftOver.pl -verbose=2 -buildDir=`pwd` \
	-ooc=/hive/data/genomes/xenTro9/jkStuff/xenTro9.11.ooc \
        -bigClusterHub=ku -dbHost=hgwdev -workhorse=hgwdev \
         xenTro9 xenTro10) > do.log 2>&1 &
    # real    575m48.416s

    # verify the convert link on the test browser is now active from xenTro9 to
    # xenTro10

#########################################################################
