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

# This file describes browser build for the petMar3

#########################################################################
# reuse photograph obtained for petMar1/petMar2
#    (DONE - 2018-01-12 - Hiram)

mkdir /hive/data/genomes/petMar3
cd /hive/data/genomes/petMar3

printf 'photoCreditURL\thttp://www.epa.gov/greatlakes/image/viz_iss4.html
photoCreditName\tPhoto courtesy of U.S. Environmental Protection Agency
' > photoReference.txt

   cat photoReference.txt
photoCreditURL  http://www.epa.gov/greatlakes/image/viz_iss4.html
photoCreditName Photo courtesy of U.S. Environmental Protection Agency

#########################################################################
#  Initial steps (DONE - 2018-01-12 - Hiram)

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

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

# best to use a most recent document with similar assembly structure
# since it has the latest features and procedures:
sed -e 's/ficAlb1/petMar3/g; s/FicAlb1/PetMar3/g; s/DONE/TBD/g;' \
    ../ficAlb1/initialBuild.txt > initialBuild.txt

mkdir /hive/data/genomes/petMar3/refseq
cd /hive/data/genomes/petMar3/refseq

rsync -L -a -P \
rsync://ftp.ncbi.nlm.nih.gov/genomes/genbank/vertebrate_other/Petromyzon_marinus/all_assembly_versions/GCA_002833325.1_Pmar_germline_1.0/ ./

# sent 260 bytes  received 808917275 bytes  17778407.36 bytes/sec
# total size is 808817053  speedup is 1.00
# real    0m44.581s

# check assembly size for later reference:

faSize G*0_genomic.fna.gz
# 1130417696 bases (87252299 N's 1043165397 real 591541430 upper
#	451623967 lower) in 12061 sequences in 1 files
# Total size: mean 93725.0 sd 1028390.9 min 208 (PIZI01012061.1)
#	max 27620446 (PIZI01000001.1) median 10237
# %39.95 masked total, %43.29 masked real

# this information is from the top of
#    petMar3/genbank/GCA_002833325.1_Pmar_germline_1.0_assembly_report.txt

# Assembly name:  Pmar_germline 1.0
# Description:    Petromyzon marinus germline 1.0
# Organism name:  Petromyzon marinus (sea lamprey)
# Infraspecific name:  ecotype=Ocean Run
# Isolate:  W1
# Sex:  male
# Taxid:          7757
# BioSample:      SAMN06131802
# BioProject:     PRJNA357048
# Submitter:      University of Kentucky
# Date:           2017-12-11
# Assembly type:  haploid
# Release type:   major
# Assembly level: Scaffold
# Genome representation: full
# WGS project:    PIZI01
# Assembly method: DBG2OLC v. February-2015
# Expected final version: yes
# Reference guided assembly: de-novo
# Genome coverage: 100.0x
# Sequencing technology: Illumina; PacBio
# RefSeq category: Representative Genome
# GenBank assembly accession: GCA_002833325.1
#
## Assembly-Units:
## GenBank Unit Accession       RefSeq Unit Accession   Assembly-Unit name
## GCA_002833335.1              Primary Assembly

#############################################################################
# establish config.ra file (DONE - Hiram - 2018-01-12)
    # arguments here are: <db> <clade> <trackDbDir> <assembly_report.txt>
    cd /hive/data/genomes/petMar3
    $HOME/kent/src/hg/utils/automation/prepConfig.pl petMar3 vertebrate \
         lamprey ./genbank/*_assembly_report.txt > petMar3.config.ra
    # going to need a mitoAcc ?

    # fixup commonName to be identical to petMar2
    # set mitoAcc to none, will be including that in the ucsc files
    # reset orderKey to work with existing petMar1, petMar2

    # verify it looks sane
    cat petMar3.config.ra
# config parameters for makeGenomeDb.pl:
db petMar3
clade vertebrate
genomeCladePriority 70
scientificName Petromyzon marinus
commonName Lamprey
assemblyDate Dec. 2017
assemblyLabel University of Kentucky
assemblyShortLabel Pmar_germline 1.0
orderKey 12018
mitoAcc none
fastaFiles /hive/data/genomes/petMar3/ucsc/*.fa.gz
agpFiles /hive/data/genomes/petMar3/ucsc/*.agp
# qualFiles none
dbDbSpeciesDir lamprey
photoCreditURL  http://www.epa.gov/greatlakes/image/viz_iss4.html
photoCreditName Photo courtesy of U.S. Environmental Protection Agency
ncbiGenomeId 287
ncbiAssemblyId 1474531
ncbiAssemblyName Pmar_germline 1.0
ncbiBioProject 357048
ncbiBioSample SAMN06131802
genBankAccessionID GCA_002833325.1
taxId 7757

#############################################################################
# setup UCSC named files (DONE - 2018-01-12 - Hiram)

    mkdir /hive/data/genomes/petMar3/ucsc
    cd /hive/data/genomes/petMar3/ucsc

    # check for duplicate sequences:
    time faToTwoBit -noMask ../genbank/G*0_genomic.fna.gz genbank.2bit
    #  real    0m26.340s

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

    # there is no assembly structure directory in the genbank release
    # simply need to fix the contig names from .1 to v1

    time zcat ../genbank/G*0_genomic.fna.gz | sed -e 's/.1 Petromyzon.*/v1/;' \
	| gzip -c > chrUn.fa.gz
    # real    5m23.814s

    # survey the gap structure:
    twoBitInfo -nBed genbank.2bit stdout | gzip -c > genbank.N.bed.gz

    zcat genbank.N.bed.gz | awk '{print $3-$2}' | ave stdin
Q1 100.000000
median 100.000000
Q3 100.000000
average 6900.142270
min 1.000000
max 500000.000000
count 12645
total 87252299.000000
standard deviation 47540.326929

    # there is no AGP definition supplied, make a fake AGP:
    # call gaps 50,000 as scaffolds, all others contig:
    zcat chrUn.fa.gz | hgFakeAgp -minContigGap=1 \
	-minScaffoldGap=50000 stdin chrUn.agp

    # should be same gap structure as the N.bed file:
    grep -v "_" chrUn.agp | ave -col=6 stdin
Q1 100.000000
median 100.000000
Q3 100.000000
average 6900.142270
min 1.000000
max 500000.000000
count 12645
total 87252299.000000
standard deviation 47540.326929


    # bash syntax here
    mitoAcc="NC_001626.1"
    printf "# mitoAcc %s\n" "$mitoAcc"
# mitoAcc NC_021621.1

    wget -qO- \
     "http://www.ncbi.nlm.nih.gov/sviewer/viewer.fcgi?db=nuccore&dopt=fasta&sendto=on&id=$mitoAcc" \
	| sed -e 's/NC_001626.1 Petromyzon.*/chrM/;' | gzip > chrM.fa.gz
    faCount chrM.fa.gz
#seq    len     A       C       G       T       N       cpg
chrM    16201   5227    3863    2182    4929    0       296

    printf "chrM\t1\t16201\t1\tF\t$mitoAcc\t1\t16201\t+\n" > chrM.agp

    # verify fasta and AGPs agree
    time faToTwoBit chr*.fa.gz test.2bit
    # real    0m32.002s

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

    # and no sequence lost from orginal:
    twoBitToFa test.2bit stdout | faSize stdin
# 1130433897 bases (87252299 N's 1043181598 real 591557631 upper
#	451623967 lower) in 12062 sequences in 1 files
# Total size: mean 93718.6 sd 1028348.5 min 208 (PIZI01012061v1)
#	max 27620446 (PIZI01000001v1) median 10237
# %39.95 masked total, %43.29 masked real

    # the difference is the size of chrM added:
# 1130417696 bases (87252299 N's 1043165397 real 591541430 upper
    calc 1130433897 - 1130417696
    # 1130433897 - 1130417696 = 16201.000000

    # no longer need these temporary 2bit files
    rm genbank.2bit test.2bit genbank.N.bed.gz

#############################################################################
#  Initial database build (DONE - 2018-01-04 - Hiram)

    cd /hive/data/genomes/petMar3
    # verify sequence and AGP are OK:
    time (makeGenomeDb.pl -workhorse=hgwdev -dbHost=hgwdev -fileServer=hgwdev \
         -stop=agp petMar3.config.ra) > agp.log 2>&1
    #  *** All done!  (through the 'agp' step)
    # real    1m8.637s

    # then finish it off:
    time (makeGenomeDb.pl -workhorse=hgwdev -dbHost=hgwdev \
       -fileServer=hgwdev -continue=db petMar3.config.ra) > db.log 2>&1
    # real    9m8.383s
    # had some trackDb issues, continuing with fixed script:
    time (~/kent/src/hg/utils/automation/makeGenomeDb.pl -workhorse=hgwdev \
	-continue=trackDb -dbHost=hgwdev \
       -fileServer=hgwdev petMar3.config.ra) > trackDb.log 2>&1
    # real    0m7.511s

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

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

##############################################################################
# cpgIslands on UNMASKED sequence (DONE - 2018-01-12 - Hiram)
    mkdir /hive/data/genomes/petMar3/bed/cpgIslandsUnmasked
    cd /hive/data/genomes/petMar3/bed/cpgIslandsUnmasked

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

    cat fb.petMar3.cpgIslandExtUnmasked.txt
    # 327039539 bases of 1043181598 (31.350%) in intersection

#############################################################################
# cytoBandIdeo - (DONE - 2018-01-12 - Hiram)
    mkdir /hive/data/genomes/petMar3/bed/cytoBand
    cd /hive/data/genomes/petMar3/bed/cytoBand
    makeCytoBandIdeo.csh petMar3

##########################################################################
# run up idKeys files for chromAlias (DONE - 2018-01-12 - Hiram)
    mkdir /hive/data/genomes/petMar3/bed/idKeys
    cd /hive/data/genomes/petMar3/bed/idKeys

    time (doIdKeys.pl -twoBit=/hive/data/genomes/petMar3/petMar3.unmasked.2bit -buildDir=`pwd` petMar3) > do.log 2>&1 &
    # real    38m46.994s

    cat petMar3.keySignature.txt
    #   3ad05caaa18e7014f168866e22d4137a

##########################################################################
# ucscToINSDC and ucscToRefSeq table/track (DONE - 2018-01-12 - 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/petMar3/bed/ucscToINSDC
    cd /hive/data/genomes/petMar3/bed/ucscToINSDC

    # if there is a chrM, use its INSDC name as a second argument:
     grep chrM ../../*.agp
# chrM    1       16201   1       F       NC_001626.1     1       16201   +

    # there is no assembly structure hierarchy for this assembly.
    awk '{printf "%s\t%s\n", $1, $1}' ../../chrom.sizes | sed -e 's/v1/.1/;' \
       | awk '{printf "%s\t%s\n", $2, $1}' | grep -v chrM > ucscToINSDC.txt
    # look up RefSeq name NC_001626.1 at Entrez to find INSDC name:
    printf "chrM\tU11880.1\n" >> ucscToINSDC.txt
    sort -o ucscToINSDC.txt ucscToINSDC.txt
    # one single sequence for RefSeq names
    printf "chrM\tNC_001626.1\n" > ucscToRefSeq.txt

    awk '{printf "%s\t0\t%d\n", $1,$2}' ../../chrom.sizes \
         | sort > ucsc.coordinate.tab

    join -t$'\t' ucsc.coordinate.tab ucscToRefSeq.txt > ucscToRefSeq.bed
    join -t$'\t' ucsc.coordinate.tab ucscToINSDC.txt > ucscToINSDC.bed

    # should be same line counts throughout, only one for the RefSeq:
    wc -l *
    # 12062 ucsc.coordinate.tab
    # 12062 ucscToINSDC.bed
    # 12062 ucscToINSDC.txt
    #     1 ucscToRefSeq.bed
    #     1 ucscToRefSeq.txt

    export chrSize=`cut -f1 ucscToINSDC.bed | awk '{print length($0)}' | sort -n | tail -1`
    echo $chrSize
    # 14
    # use the 14 in this sed
    sed -e "s/21/$chrSize/" $HOME/kent/src/hg/lib/ucscToINSDC.sql \
         | hgLoadSqlTab petMar3 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
    #  4
    sed -e "s/21/$chrSize/" $HOME/kent/src/hg/lib/ucscToINSDC.sql \
       | sed -e 's/INSDC/RefSeq/g;' > ucscToRefSeq.sql
    hgLoadSqlTab petMar3 ucscToRefSeq ./ucscToRefSeq.sql ucscToRefSeq.bed

    # checkTableCoords should be silent
    checkTableCoords petMar3
    # each should cover %100 entirely:
    featureBits -countGaps petMar3 ucscToINSDC

    # 1130433897 bases of 1130433897 (100.000%) in intersection

    featureBits -countGaps petMar3 ucscToRefSeq
    # 16201 bases of 1130433897 (0.001%) in intersection

#########################################################################
# add chromAlias table (DONE - 2018-01-05 - Hiram)

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

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

    ~/kent/src/hg/utils/automation/chromAlias.pl ucsc.*.tab \
	> petMar3.chromAlias.tab

for t in refseq genbank
do
  c0=`cat ucsc.$t.tab | wc -l`
  c1=`grep $t petMar3.chromAlias.tab | wc -l`
  ok="OK"
  if [ "$c0" -ne "$c1" ]; then
     ok="ERROR"
  fi
  printf "# checking $t: $c0 =? $c1 $ok\n"
done
# checking refseq: 1 =? 1 OK
# checking genbank: 12062 =? 12062 OK

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

#########################################################################
# fixup search rule for assembly track/gold table (DONE - 2018-01-12 - Hiram)
    cd ~/kent/src/hg/makeDb/trackDb/lamprey/petMar3

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

    # implies a rule: '[NP][CI][ZI_]+[0-9]{6}[0-9]*(v1_[0-9]+)?'

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

    hgsql -N -e "select frag from gold;" petMar3 \
       | egrep -e '[NP][CIZ_]+[0-9]{6}[0-9]*(v1_[0-9]+)?' | wc -l
    # 24707

    hgsql -N -e "select frag from gold;" petMar3 \
       | egrep -v -e '[NP][CIZ_]+[0-9]{6}[0-9]*(v1_[0-9]+)?' | wc -l
    # 0

    # hence, add to trackDb/chicken/petMar3/trackDb.ra
searchTable gold
shortCircuit 1
termRegex [NP][CIZ_]+[0-9]{6}[0-9]*(v1_[0-9]+)?
query select chrom,chromStart,chromEnd,frag from %s where frag like '%s%%'
searchPriority 8

    # verify searches work in the position box for these name patterns

##########################################################################
# running repeat masker (DONE - 2018-01-12 - Hiram)
    mkdir /hive/data/genomes/petMar3/bed/repeatMasker
    cd /hive/data/genomes/petMar3/bed/repeatMasker
    time  (doRepeatMasker.pl -buildDir=`pwd` \
        -bigClusterHub=ku -dbHost=hgwdev -workhorse=hgwdev \
        -smallClusterHub=ku petMar3) > do.log 2>&1 &
    # a number of entries failed the id check, had to clean them up
    # to pass the nestedRepeats function
    # real    184m37.102s
    time  (doRepeatMasker.pl -buildDir=`pwd` \
        -bigClusterHub=ku -dbHost=hgwdev -workhorse=hgwdev \
        -continue=mask -smallClusterHub=ku petMar3) > mask.log 2>&1 &
    # real    7m54.620s

    egrep "bases|Total|masked" faSize.rmsk.txt \
	| fold -w 75 -s  | sed -e 's/^/# /;'
# 1130433897 bases (87252299 N's 1043181598 real 664546395 upper 378635203 
# lower) in 12062 sequences in 1 files
# Total size: mean 93718.6 sd 1028348.5 min 208 (PIZI01012061v1) max 
# 27620446 (PIZI01000001v1) median 10237
# %33.49 masked total, %36.30 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 petMar3 rmsk
    # 378649764 bases of 1130433897 (33.496%) in intersection
    # real    0m16.992s

    # 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;' petMar3 \
        | bedSingleCover.pl stdin | ave -col=4 stdin | grep "^total"
    # total 378649764.000000
    # real    0m14.424s

##########################################################################
# running simple repeat (DONE - 2018-01-12 - Hiram)

    mkdir /hive/data/genomes/petMar3/bed/simpleRepeat
    cd /hive/data/genomes/petMar3/bed/simpleRepeat
    # using trf409 3 here a bit smaller genome (human == 6)
    time (doSimpleRepeat.pl -buildDir=`pwd` -bigClusterHub=ku \
        -dbHost=hgwdev -workhorse=hgwdev -smallClusterHub=ku \
        -trf409 3 petMar3) > do.log 2>&1 &
    # real    94m6.371s

    cat fb.simpleRepeat
    # 140166712 bases of 1043181598 (13.436%) in intersection

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

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

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

    twoBitToFa petMar3.2bit stdout | faSize stdin > faSize.petMar3.2bit.txt
    egrep "bases|Total|masked" faSize.petMar3.2bit.txt \
	| fold -w 78 -s  | sed -e 's/^/# /;'
# 1130433897 bases (87252299 N's 1043181598 real 572629855 upper 470551743 
# lower) in 12062 sequences in 1 files
# Total size: mean 93718.6 sd 1028348.5 min 208 (PIZI01012061v1) max 27620446 
# (PIZI01000001v1) median 10237
# %41.63 masked total, %45.11 masked real

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

#########################################################################
# CREATE MICROSAT TRACK (DONE - 2018-01-12 - Hiram)
    ssh hgwdev
    mkdir /cluster/data/petMar3/bed/microsat
    cd /cluster/data/petMar3/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 petMar3 microsat microsat.bed
    # Read 90 elements of size 4 from microsat.bed

##########################################################################
## WINDOWMASKER (DONE - 2018-01-12 - Hiram)

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

    # Masking statistics
    egrep "bases|Total|masked" faSize.petMar3.cleanWMSdust.txt \
	| fold -w 78 -s  | sed -e 's/^/# /;'
# 1130433897 bases (87252299 N's 1043181598 real 573838529 upper 469343069 
# lower) in 12062 sequences in 1 files
# Total size: mean 93718.6 sd 1028348.5 min 208 (PIZI01012061v1) max 27620446 
# (PIZI01000001v1) median 10237
# %41.52 masked total, %44.99 masked real

    cat fb.petMar3.rmsk.windowmaskerSdust.txt
    # 300337516 bases of 1130433897 (26.568%) in intersection

##########################################################################
# cpgIslands - (DONE - 2018-01-12 - Hiram)
    mkdir /hive/data/genomes/petMar3/bed/cpgIslands
    cd /hive/data/genomes/petMar3/bed/cpgIslands
    time (doCpgIslands.pl -dbHost=hgwdev -bigClusterHub=ku \
      -workhorse=hgwdev -smallClusterHub=ku petMar3) > do.log 2>&1 &
    # real    3m56.146s

    cat fb.petMar3.cpgIslandExt.txt
    # 63830156 bases of 1043181598 (6.119%) in intersection

##############################################################################
# genscan - (DONE - 2018-01-12 - Hiram)
    mkdir /hive/data/genomes/petMar3/bed/genscan
    cd /hive/data/genomes/petMar3/bed/genscan
    time (doGenscan.pl -buildDir=`pwd` -workhorse=hgwdev -dbHost=hgwdev \
      -bigClusterHub=ku petMar3) > do.log 2>&1 &
    # real    89m3.857s

    # 2 jobs failed, running with 2000000 size window
    # real    37m54.704s

    # continuing
    time (doGenscan.pl -buildDir=`pwd` -workhorse=hgwdev -dbHost=hgwdev \
      -continue=makeBed -bigClusterHub=ku petMar3) > makeBed.log 2>&1 &
    # real    2m48.899s

    cat fb.petMar3.genscan.txt
    # 35050947 bases of 1043181598 (3.360%) in intersection

    cat fb.petMar3.genscanSubopt.txt
    # 27257669 bases of 1043181598 (2.613%) in intersection

#############################################################################
# augustus gene track (DONE - 2018-01-12 - Hiram)

    mkdir /hive/data/genomes/petMar3/bed/augustus
    cd /hive/data/genomes/petMar3/bed/augustus

    time (doAugustus.pl -buildDir=`pwd` -bigClusterHub=ku \
     -species=zebrafish -dbHost=hgwdev -workhorse=hgwdev petMar3) \
	> do.log 2>&1 &
    # real    127m8.330s

    cat fb.petMar3.augustusGene.txt
    # 46980983 bases of 1043181598 (4.504%) in intersection

##############################################################################
# lastz/chain/net swap human/hg38 (DONE - 2018-02-01 - Hiram)

    # original alignment
    cd /hive/data/genomes/hg38/bed/lastzPetMar3.2018-01-25

    cat fb.hg38.chainPetMar3Link.txt
    # 52661859 bases of 3049335806 (1.727%) in intersection

    cat fb.hg38.chainSynPetMar3Link.txt
    # 1508338 bases of 3049335806 (0.049%) in intersection

    cat fb.hg38.chainRBestPetMar3Link.txt
    # 25411380 bases of 3049335806 (0.833%) in intersection

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

    time (doBlastzChainNet.pl -verbose=2 \
      /hive/data/genomes/hg38/bed/lastzPetMar3.2018-01-25/DEF \
        -swap -chainMinScore=5000 -chainLinearGap=loose \
          -workhorse=hgwdev -smallClusterHub=ku -bigClusterHub=ku \
            -syntenicNet) > swap.log 2>&1
    #  real    12m51.092s

    cat fb.petMar3.chainHg38Link.txt
    # 51300303 bases of 1043181598 (4.918%) in intersection

    cat fb.petMar3.chainSynHg38Link.txt
    # 1425757 bases of 1043181598 (0.137%) in intersection

    time (doRecipBest.pl -workhorse=hgwdev -load -buildDir=`pwd` petMar3 hg38) \
	> rbest.log 2>&1 &
    # real    264m26.880s

    cat fb.petMar3.chainRBestHg38Link.txt
    # 25154743 bases of 1043181598 (2.411%) in intersection

##############################################################################
# lastz/chain/net swap mouse/mm10 (DONE - 2018-01-28 - Hiram)
    # original alignment to mm10

    cd /hive/data/genomes/mm10/bed/lastzPetMar3.2018-01-25

    cat fb.mm10.chainPetMar3Link.txt
    #   36835173 bases of 2652783500 (1.389%) in intersection

    cat fb.mm10.chainRBestPetMar3Link.txt
    # 21623456 bases of 2652783500 (0.815%) in intersection

    #	and for the swap
    mkdir /hive/data/genomes/petMar3/bed/blastz.mm10.swap
    cd /hive/data/genomes/petMar3/bed/blastz.mm10.swap
    time (doBlastzChainNet.pl -verbose=2 \
	/hive/data/genomes/mm10/bed/lastzPetMar3.2018-01-25/DEF \
        -syntenicNet -workhorse=hgwdev -smallClusterHub=ku -bigClusterHub=ku \
        -swap -chainMinScore=5000 -chainLinearGap=loose) > swap.log 2>&1 &
    # real    7m57.582s

    #   real    7m2.754s
    cat  fb.petMar3.chainMm10Link.txt
    #	39217857 bases of 1043181598 (3.759%) in intersection

    cat fb.petMar3.chainSynMm10Link.txt
    # 1381239 bases of 1043181598 (0.132%) in intersection

    time (doRecipBest.pl -workhorse=hgwdev -load -buildDir=`pwd` petMar3 mm10) \
	> rbest.log 2>&1 &
    # real    206m59.727s

    cat fb.petMar3.chainRBestMm10Link.txt
    # 21335101 bases of 1043181598 (2.045%) in intersection

##############################################################################
# Create kluster run files (DONE - 2018-01-12 - Hiram)

    # numerator is petMar3 gapless bases "real" as reported by:
    featureBits -noRandom -noHap petMar3 gap
    # 87252299 bases of 1043181598 (8.364%) 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 \( 1043181598 / 2861349177 \) \* 1024
    #  ( 1043181598 / 2861349177 ) * 1024 = 373.326669

    # ==> use -repMatch=400 according to size scaled down from 1024 for human.
    #   and rounded up to nearest 50
    cd /hive/data/genomes/petMar3
    blat petMar3.2bit \
         /dev/null /dev/null -tileSize=11 -makeOoc=jkStuff/petMar3.11.ooc \
        -repMatch=400
    #   Wrote 28255 overused 11-mers to jkStuff/petMar3.11.ooc
    # petMar2 was -repMatch=350:
    #   Wrote 15692 overused 11-mers to jkStuff/petMar2.11.ooc
    # petMar1 was -repMatch=200
    #	Wrote 49155 overused 11-mers to petMar1.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;' petMar3 \
        | sort -k7,7nr | ave -col=7 stdin
# Q1 127644.000000
# median 231535.000000
# Q3 500000.000000
# average 280957.457875
# min 50429.000000
# max 500000.000000
# count 273

    # minimum non-bridged gap is  50000
    gapToLift -verbose=2 -minGap=50000 petMar3 jkStuff/nonBridged.lft \
       -bedFile=jkStuff/nonBridged.bed

#########################################################################
# GENBANK AUTO UPDATE (DONE - 2018-01-13 - Hiram)
    ssh hgwdev
    cd $HOME/kent/src/hg/makeDb/genbank
    git pull
    # /cluster/data/genbank/data/organism.lst shows:
    # #organism             mrnaCnt   estCnt  refSeqCnt
    # Petromyzon marinus      1548    120740  0

    # edit etc/genbank.conf to add petMar3 just before petMar2

# petMar3 (Petromyzon marinus - Sea Lamprey - taxId 7757)
petMar3.serverGenome = /hive/data/genomes/petMar3/petMar3.2bit
petMar3.clusterGenome = /hive/data/genomes/petMar3/petMar3.2bit
petMar3.ooc = /hive/data/genomes/petMar3/jkStuff/petMar3.11.ooc
petMar3.lift = no
petMar3.refseq.mrna.native.pslCDnaFilter  = ${ordered.refseq.mrna.native.pslCDnaFilter}
petMar3.refseq.mrna.xeno.pslCDnaFilter    = ${ordered.refseq.mrna.xeno.pslCDnaFilter}
petMar3.genbank.mrna.native.pslCDnaFilter = ${ordered.genbank.mrna.native.pslCDnaFilter}
petMar3.genbank.mrna.xeno.pslCDnaFilter   = ${ordered.genbank.mrna.xeno.pslCDnaFilter}
petMar3.genbank.est.native.pslCDnaFilter  = ${ordered.genbank.est.native.pslCDnaFilter}
# defaults yes: genbank.mrna.native.load and loadDesc
# defaults yes: genbank.est.native.load
# defaults yes: refseq.mrna.native.load, refseq.mrna.native.loadDesc
# defaults yes: refseq.mrna.xeno.load, refseq.mrna.xeno.loadDesc
# defaults no: genbank.mrna.xeno.load and loadDesc
# defaults no: genbank.est.xeno.load, genbank.est.xeno.loadDesc
# DO NOT NEED genbank.mrna.xeno except for human, mouse
petMar3.downloadDir = petMar3
petMar3.perChromTables = no

    git commit -m \
     'adding petMar3 - Petromyzon marinus - lamprey refs #20819' \
	 etc/genbank.conf
    git push
    # update /cluster/data/genbank/:
    make etc-update

    cd /cluster/data/genbank

    time ./bin/gbAlignStep -initial petMar3
    # logFile: var/build/logs/2018.01.12-21:18:31.petMar3.initalign.log
    #   approximately 3 hours

    tail -2 var/build/logs/2018.01.12-21:18:31.petMar3.initalign.log
    # hgwdev 2018.01.13-00:16:58 petMar3.initalign: Succeeded: petMar3
    # hgwdev 2018.01.13-00:18:27 petMar3.initalign: finish

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

    # load database when finished
    ssh hgwdev
    cd /cluster/data/genbank
    time ./bin/gbDbLoadStep -drop -initialLoad petMar3
    # logFile: var/dbload/hgwdev/logs/2018.01.14-00:30:59.petMar3.dbload.log
    #  real    18m50.768s

    tail -1 var/dbload/hgwdev/logs/2018.01.14-00:30:59.petMar3.dbload.log
    #  hgwdev 2018.01.14-00:49:50 petMar3.dbload: finish

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

#############################################################################
# LIFTOVER TO petMar2 (DONE - 2018-01-12 - Hiram)
    ssh hgwdev
    mkdir /hive/data/genomes/petMar3/bed/blat.petMar2.2018-01-12
    cd /hive/data/genomes/petMar3/bed/blat.petMar2.2018-01-12
    time (doSameSpeciesLiftOver.pl -verbose=2 -buildDir=`pwd` \
	-ooc=/hive/data/genomes/petMar3/jkStuff/petMar3.11.ooc \
        -bigClusterHub=ku -dbHost=hgwdev -workhorse=hgwdev \
         petMar3 petMar2) > do.log 2>&1
    # real    266m54.816s

    # verify the convert link on the test browser is now active from petMar3 to
    # petMar2

#############################################################################
# LIFTOVER TO petMar1 (DONE - 2018-01-12 - Hiram)
    ssh hgwdev
    mkdir /hive/data/genomes/petMar3/bed/blat.petMar1.2018-01-12
    cd /hive/data/genomes/petMar3/bed/blat.petMar1.2018-01-12
    time (doSameSpeciesLiftOver.pl -verbose=2 -buildDir=`pwd` \
	-ooc=/hive/data/genomes/petMar3/jkStuff/petMar3.11.ooc \
        -bigClusterHub=ku -dbHost=hgwdev -workhorse=hgwdev \
         petMar3 petMar1) > do.log 2>&1
    # real    275m20.049s

    # verify the convert link on the test browser is now active from petMar3 to
    # petMar1

#############################################################################
#  BLATSERVERS ENTRY (DONE - 2018-02-13 - 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 ("petMar3", "blat1c", "17898", "1", "0"); \
	INSERT INTO blatServers (db, host, port, isTrans, canPcr) \
	VALUES ("petMar3", "blat1c", "17899", "0", "1");' \
	    hgcentraltest
    #	test it with some sequence

############################################################################
## reset default position to location as petMar2 as found from liftOver
##  (DONE - 2018-02-13 - Hiram)

    ssh hgwdev
    hgsql -e 'update dbDb set defaultPos="PIZI01000016v1:9749699-10085980"
	where name="petMar3";' hgcentraltest

#########################################################################
# all.joiner update, downloads and in pushQ - (DONE - 2018-02-13 - Hiram)
    cd $HOME/kent/src/hg/makeDb/schema
    # fixup all.joiner until this is a clean output
    joinerCheck -database=petMar3 -tableCoverage all.joiner
    joinerCheck -database=petMar3 -times all.joiner
    joinerCheck -database=petMar3 -keys all.joiner

    cd /hive/data/genomes/petMar3
    time (makeDownloads.pl -workhorse=hgwdev petMar3) > downloads.log 2>&1
    #  real    9m36.528s

    #   now ready for pushQ entry
    mkdir /hive/data/genomes/petMar3/pushQ
    cd /hive/data/genomes/petMar3/pushQ
  time (makePushQSql.pl -redmineList petMar3) > petMar3.pushQ.sql 2> stderr.out
    #  real    3m59.567s

    #   check for errors in stderr.out, some are OK, e.g.:
    # WARNING: petMar3 does not have seq
    # WARNING: petMar3 does not have extFile

    ## there are warnings about the RBest and Syn chainNet tables, which we
    ## are not interested in at this time.  They can be left out.

    # verify the file listings are valid, should be no output to stderr:
    cat redmine.petMar3.file.list \
        | while read L; do ls -ogL $L; done  > /dev/null

    # to verify the database.table list is correct, should be the same
    # line count for these two commands:
    wc -l redmine.petMar3.table.list
    # 67 redmine.petMar3.table.list
    awk -F'.' '{
printf "hgsql -N -e \"show table status like '"'"'%s'"'"';\" %s\n", $2, $1
}' redmine.petMar3.table.list | while read L; do eval $L; done | wc -l
    # 67

    # enter the path names to these files in the redmine issue to
    # make QA Ready:
    ls `pwd`/redmine*

/hive/data/genomes/petMar3/pushQ/redmine.petMar3.file.list
/hive/data/genomes/petMar3/pushQ/redmine.petMar3.releaseLog.txt
/hive/data/genomes/petMar3/pushQ/redmine.petMar3.table.list

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