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

# This file describes browser build for the susScr11
# Pig -- Sus scrofa

# chrMT listed in assembly == NC_000845.1 == AF034253.1

#############################################################################
# fetch sequence from new style download directory (DONE - 2017-07-25 - Hiram)
    mkdir -p /hive/data/genomes/susScr11/refseq
    cd /hive/data/genomes/susScr11/refseq

    time rsync -L -a -P \
rsync://ftp.ncbi.nlm.nih.gov/genomes/refseq/vertebrate_mammalian/Sus_scrofa/all_assembly_versions/GCF_000003025.6_Sscrofa11.1/ ./
    # real    1m53.774s
    # sent 1483 bytes  received 3015662916 bytes  26569730.39 bytes/sec
    # total size is 3015288899  speedup is 1.00

    # measure what we have here:
    faSize GCF_000003025.6_Sscrofa11.1_genomic.fna.gz
# 2501912388 bases (29864684 N's 2472047704 real 1576544146 upper
#	895503558 lower) in 613 sequences in 1 files
# Total size: mean 4081423.1 sd 23780904.1 min 15096 (NW_018085360.1)
#	max 274330532 (NC_010443.5) median 49329
# %35.79 masked total, %36.23 masked real

    faCount GCF_000003025.6_Sscrofa11.1_genomic.fna.gz | tail
# #seq    len             A       C          G       T         N       cpg
# total   2501912388  717891230 517402066 517706165 719048243 29864684 30619972

#############################################################################
# fixup to UCSC naming scheme (DONE - 2017-07-25 - Hiram)
    mkdir /hive/data/genomes/susScr11/ucsc
    cd /hive/data/genomes/susScr11/ucsc

    # verify no duplicate sequences:
    time faToTwoBit ../refseq/*1_genomic.fna.gz refseq.2bit
    #	real    1m13.138s

    twoBitDup refseq.2bit
    # should be silent output, otherwise the duplicates need to be removed

    time ~/kent/src/hg/utils/automation/ucscCompositeAgp.pl \
     ../refseq/*1_genomic.fna.gz ../refseq/*_assembly_structure/Primary_Assembly

NC_010443.5 chr1
NC_010444.4 chr2
NC_010445.4 chr3
NC_010446.5 chr4
NC_010447.5 chr5
NC_010448.4 chr6
NC_010449.5 chr7
NC_010450.4 chr8
NC_010451.4 chr9
NC_010452.4 chr10
NC_010453.5 chr11
NC_010454.4 chr12
NC_010455.5 chr13
NC_010456.5 chr14
NC_010457.5 chr15
NC_010458.4 chr16
NC_010459.5 chr17
NC_010460.4 chr18
NC_010461.5 chrX
NC_010462.3 chrY

real    14m57.924s

    time ~/kent/src/hg/utils/automation/unplacedWithChroms.pl \
       ../refseq/*_assembly_structure/Primary_Assembly
    # processed 583 sequences into chrUn.fa.gz
    # real    0m21.149s

    time ~/kent/src/hg/utils/automation/unlocalizedWithChroms.pl \
	../refseq/*_assembly_structure/Primary_Assembly
    # Y
    # processed 9 sequences into chr*_random.gz 1 files
    # real    0m0.501s

    # bash syntax here
    mitoAcc=`grep MT ../refseq/GCF_000003025.6_Sscrofa11.1_assembly_report.txt | cut -f7`
    printf "# mitoAcc %s\n" "$mitoAcc"
# mitoAcc NC_000845.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 AGP match:
    time faToTwoBit chr*.fa.gz test.2bit
    # real    1m7.749s

    cat *.agp | checkAgpAndFa stdin test.2bit 2>&1 | tail
    #  All AGP and FASTA entries agree - both files are valid


    # verify nothing lost compared to genbank:
    time twoBitToFa test.2bit stdout | faSize stdin
# 2501912388 bases (29864684 N's 2472047704 real 2472047704 upper 0 lower)
#	in 613 sequences in 1 files
# Total size: mean 4081423.1 sd 23780904.1 min 15096 (chrUn_NW_018085360v1)
#	max 274330532 (chr1) median 49329

    # same totals as above:
# 2501912388 bases (29864684 N's 2472047704 real 1576544146 upper
#	895503558 lower) in 613 sequences in 1 files
# Total size: mean 4081423.1 sd 23780904.1 min 15096 (NW_018085360.1)
#	max 274330532 (NC_010443.5) median 49329

#############################################################################
#  Initial database build (DONE - 2017-07-25 - Hiram)

# same photoReference.txt as was used in susScr3:
    cd /hive/data/genomes/susScr11
    cat ../susScr3/photoReference.txt
photoCreditURL  http://www.ars.usda.gov/is/graphics/photos/mar98/k7974-18.htm
photoCreditName USDA Agricultural Research Service

    cp -p ../susScr3/photoReference.txt .

    # establish the config.ra file:
    ~/kent/src/hg/utils/automation/prepConfig.pl susScr11 mammal pig \
       refseq/*_assembly_report.txt > susScr11.config.ra
    
    # verify this looks OK:

    cat susScr11.config.ra
# config parameters for makeGenomeDb.pl:
db susScr11
clade mammal
scientificName Sus scrofa
commonName Pig
assemblyDate Feb. 2017
assemblyLabel The Swine Genome Sequencing Consortium (SGSC)
assemblyShortLabel Sscrofa11.1
orderKey 16312
# mitochondrial sequence included in refseq release
# mitoAcc NC_000845.1
mitoAcc none
fastaFiles /hive/data/genomes/susScr11/ucsc/*.fa.gz
agpFiles /hive/data/genomes/susScr11/ucsc/*.agp
# qualFiles none
dbDbSpeciesDir pig
photoCreditURL  http://www.ars.usda.gov/is/graphics/photos/mar98/k7974-18.htm
photoCreditName USDA Agricultural Research Service
ncbiGenomeId 84
ncbiAssemblyId 1004191
ncbiAssemblyName Sscrofa11.1
ncbiBioProject 13421
ncbiBioSample SAMN02953785
genBankAccessionID GCF_000003025.6
taxId 9823

    # verify sequence and AGP are OK:
    time (makeGenomeDb.pl -workhorse=hgwdev -dbHost=hgwdev -fileServer=hgwdev \
         -stop=agp susScr11.config.ra) > agp.log 2>&1
    # real    2m14.138s
    # verify it ran OK:
    #   *** All done!  (through the 'agp' step)

    # then finish it off:
    time (~/kent/src/hg/utils/automation/makeGenomeDb.pl -workhorse=hgwdev \
       -dbHost=hgwdev -fileServer=hgwdev -continue=db \
           susScr11.config.ra ) > db.log 2>&1
# real    20m1.184s
    # XXX failed:
# ssh -x -o 'StrictHostKeyChecking = no' -o 'BatchMode = yes' hgwdev hgsql -h localhost -A -N hgcentraltest < /cluster/data/susScr11/dbDbInsert.sql
# HgStepManager: executing step 'trackDb' Tue Jul 25 10:43:10 2017.
# makeLocalTrackDbRa: missing AGP gap type definition: repeat at /cluster/home/hiram/kent/src/hg/utils/automation/makeGenomeDb.pl line 1176, <GL> line 1.

    # new types of gap definitions:
    hgsql -e 'select type from gap;' susScr11 | sort | uniq -c
     93 contig
      2 repeat
    233 scaffold

     hgsql -e 'select * from gap where type like "repeat";' susScr11
+-----+-------+------------+----------+-----+---+------+--------+--------+
| bin | chrom | chromStart | chromEnd | ix  | n | size | type   | bridge |
+-----+-------+------------+----------+-----+---+------+--------+--------+
| 889 | chrY  |   39921294 | 39926294 | 641 | N | 5000 | repeat | yes    |
| 890 | chrY  |   40010306 | 40015306 | 647 | N | 5000 | repeat | yes    |
+-----+-------+------------+----------+-----+---+------+--------+--------+

    # fixing the makeGenomeDb.pl script:
    time (~/kent/src/hg/utils/automation/makeGenomeDb.pl -workhorse=hgwdev \
       -dbHost=hgwdev -fileServer=hgwdev -continue=trackDb \
           susScr11.config.ra ) > trackDb.log 2>&1

    # real    0m7.365s


    # check in the trackDb files created and add to trackDb/makefile
    # temporary symlink until after masking
    ln -s `pwd`/susScr11.unmasked.2bit /gbdb/susScr11/susScr11.2bit

#############################################################################
# cytoBandIdeo - (DONE - 2017-07-25 - Hiram)
    mkdir /hive/data/genomes/susScr11/bed/cytoBand
    cd /hive/data/genomes/susScr11/bed/cytoBand
    makeCytoBandIdeo.csh susScr11

##############################################################################
# cpgIslands on UNMASKED sequence (DONE - 2017-07-25 - Hiram)
    mkdir /hive/data/genomes/susScr11/bed/cpgIslandsUnmasked
    cd /hive/data/genomes/susScr11/bed/cpgIslandsUnmasked

    # run stepwise so the loading can be done in a different table
    time (doCpgIslands.pl -dbHost=hgwdev -bigClusterHub=ku -buildDir=`pwd` \
       -tableName=cpgIslandExtUnmasked \
          -maskedSeq=/hive/data/genomes/susScr11/susScr11.unmasked.2bit \
             -workhorse=hgwdev -smallClusterHub=ku susScr11) > do.log 2>&1
    # real    5m34.816s

    cat fb.susScr11.cpgIslandExtUnmasked.txt
    # 44748680 bases of 2472073034 (1.810%) in intersection

#############################################################################
# running repeat masker (DONE - 2017-07-25 - Hiram)
    mkdir /hive/data/genomes/susScr11/bed/repeatMasker
    cd /hive/data/genomes/susScr11/bed/repeatMasker
    time  (doRepeatMasker.pl -buildDir=`pwd` \
        -bigClusterHub=ku -dbHost=hgwdev -workhorse=hgwdev \
        -smallClusterHub=ku susScr11) > do.log 2>&1 &
    # real    781m8.842s

    cat faSize.rmsk.txt
# 2501912388 bases (29864684 N's 2472047704 real 1352527731 upper
#	1119519973 lower) in 613 sequences in 1 files
# Total size: mean 4081423.1 sd 23780904.1 min 15096 (chrUn_NW_018085360v1)
#	max 274330532 (chr1) median 49329
# %44.75 masked total, %45.29 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 susScr11 rmsk
    # 1119521852 bases of 2501912388 (44.747%) in intersection
    # real    0m42.868s

    # why is it different than the faSize above ?
    # because rmsk masks out some N's as well as bases, the 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:
    time hgsql -N -e 'select genoName,genoStart,genoEnd from rmsk;' susScr11 \
        | bedSingleCover.pl stdin | ave -col=4 stdin | grep "^total"
    # total 1119521852.000000
    #  real    0m35.755s

##########################################################################
# running simple repeat (DONE - 2017-07-25 - Hiram)

    mkdir /hive/data/genomes/susScr11/bed/simpleRepeat
    cd /hive/data/genomes/susScr11/bed/simpleRepeat
    time (doSimpleRepeat.pl -buildDir=`pwd` -bigClusterHub=ku \
        -dbHost=hgwdev -workhorse=hgwdev -smallClusterHub=ku \
        -trf409 5 susScr11) > do.log 2>&1 &
    # real    9m56.089s

    cat fb.simpleRepeat
    # 33056801 bases of 2472073034 (1.337%) in intersection

    # add to this simple repeat to rmsk:
    cd /hive/data/genomes/susScr11
    twoBitMask susScr11.rmsk.2bit \
        -add bed/simpleRepeat/trfMask.bed susScr11.2bit
    #   you can safely ignore the warning about fields >= 13
    twoBitToFa susScr11.2bit stdout | faSize stdin > faSize.susScr11.2bit.txt
    cat faSize.susScr11.2bit.txt
    # 2501912388 bases (29864684 N's 2472047704 real 1351491909 upper
    #	1120555795 lower) in 613 sequences in 1 files
    # Total size: mean 4081423.1 sd 23780904.1
    #	min 15096 (chrUn_NW_018085360v1) max 274330532 (chr1) median 49329
    # %44.79 masked total, %45.33 masked real

    rm /gbdb/susScr11/susScr11.2bit
    ln -s `pwd`/susScr11.2bit /gbdb/susScr11/susScr11.2bit

#############################################################################
# CREATE MICROSAT TRACK (DONE - 2017-07-27 - Hiram)
    ssh hgwdev
    mkdir /cluster/data/susScr11/bed/microsat
    cd /cluster/data/susScr11/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 susScr11 microsat microsat.bed

#############################################################################
# ucscToINSDC table/track (DONE - 2017-07-29 - Hiram)
    # the sequence here is working for a 'refseq' assembly with a chrM
    # situation may be specific depending upon what is available in the assembly

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

    # find accession for chrM
    grep chrM ../../susScr11.agp
# chrM    1       16613   1       O       NC_000845.1     1       16613   +

    # use that accession here:
    ~/kent/src/hg/utils/automation/ucscToINSDC.sh \
        ../../refseq/GCF_*structure/Primary_Assembly NC_000845.1
    # this is actually ucscToRefSeq

    sort -k2 ucscToINSDC.txt > ucscToRefSeq.txt
    rm -f ucscToINSDC.txt

    awk '{printf "%s\t%s\n", $2, $1}' ucscToRefSeq.txt | sort > refseqToUcsc.txt

    # there is no INSDC name in the assembly_report for chrM/NC_000845.1
    #  use the INSDC name from before: AF034253.1
    grep -v "^#" ../../refseq/GCF*_assembly_report.txt | cut -f5,7 \
      | sed -e 's/na\b/AF034253.1/;' | awk '{printf "%s\t%s\n", $2, $1}' \
         | sort > refseq.insdc.txt
    # the sed \b means to match word

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

    # the tr commands avoid the problem of trying to use the -t argument
    # to the join command which doesn't accept -t'\t' but instead has
    # to use the unseen/can not copy command ctrl-v i
    join -2 2 refseq.insdc.txt ucscToRefSeq.txt | tr '[ ]' '[\t]' | sort -k3 \
       | join -2 3 name.coordinate.tab - | tr '[ ]' '[\t]' | cut -f1-3,5 \
           > ucscToINSDC.bed
    join -2 2 refseq.insdc.txt ucscToRefSeq.txt | tr '[ ]' '[\t]' | sort -k3 \
       | join -2 3 name.coordinate.tab - | tr '[ ]' '[\t]' | cut -f1-4 \
           > ucscToRefSeq.bed

    # verify chrM is correct:
    grep chrM *.bed
# ucscToINSDC.bed:chrM    0       16613   AF034253.1
# ucscToRefSeq.bed:chrM   0       16613   NC_000845.1

    # should be same line counts throughout:
    wc -l *
    # 613 name.coordinate.tab
    # 613 refseq.insdc.txt
    # 613 ucscToINSDC.bed
    # 613 ucscToRefSeq.bed
    # 613 ucscToRefSeq.txt

    cut -f1 ucscToINSDC.bed | awk '{print length($0)}' | sort -n | tail -1
    # 26
    # use the 26 in this sed
    sed -e "s/21/26/" $HOME/kent/src/hg/lib/ucscToINSDC.sql \
         | hgLoadSqlTab susScr11 ucscToINSDC stdin ucscToINSDC.bed
    cut -f1 ucscToRefSeq.bed | awk '{print length($0)}' | sort -n | tail -1
    sed -e "s/21/26/" $HOME/kent/src/hg/lib/ucscToINSDC.sql \
         | hgLoadSqlTab susScr11 ucscToRefSeq stdin ucscToRefSeq.bed
    checkTableCoords susScr11
    # should cover %100 entirely:
    featureBits -countGaps susScr11 ucscToINSDC
    # 2501912388 bases of 2501912388 (100.000%) in intersection
    featureBits -countGaps susScr11 ucscToRefSeq
    # 2501912388 bases of 2501912388 (100.000%) in intersection

#########################################################################
# add chromAlias table (DONE - 2017-07-29 - Hiram)

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

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

    # verify chrM is correct:
    grep chrM *
# ucsc.genbank.tab:chrM   AF034253.1      genbank
# ucsc.refseq.tab:chrM    NC_000845.1     refseq

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

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

#########################################################################
# fixup search rule for assembly track/gold table (DONE - 2017-07-29 - Hiram)

    cd ~/kent/src/hg/makeDb/trackDb/pig/susScr11
    # preview prefixes and suffixes:
    hgsql -N -e "select frag from gold;" susScr11 \
      | sed -e 's/[0-9][0-9]*//;' | sort | uniq -c 
    705 AEMK.1
      2 AJKK.1
      2 CU.2
     18 FO.1
    234 FO.2
    105 FO.3
     52 FO.4
     12 FO.5
      3 FO.6
      1 FO.7
      1 FO.9
      1 FP.2
      1 FP.3
      7 FQ.1
     91 FQ.2
     55 FQ.3
      9 FQ.4
     10 FQ.5
      2 FQ.6
      1 FQ.9
      1 NC_.1

    # implies a search rule of: '[ACFN][CEJOPQU][MK0-9_]+(\.[0-9]+)?'

    # verify this rule will find them all or eliminate them all:
    hgsql -N -e "select frag from gold;" susScr11 | wc -l
    # 1313

    hgsql -N -e "select frag from gold;" susScr11 \
       | egrep -e '[ACFN][CEJOPQU][MK0-9_]+(\.[0-9]+)?' | wc -l
    # 1313

    hgsql -N -e "select frag from gold;" susScr11 \
       | egrep -v -e '[ACFN][CEJOPQU][MK0-9_]+(\.[0-9]+)?' | wc -l
    # 0

    # hence, add to trackDb/rhesus/susScr11/trackDb.ra
searchTable gold
shortCircuit 1
termRegex [ACFN][CEJOPQU][MK0-9_]+(\.[0-9]+)?
query select chrom,chromStart,chromEnd,frag from %s where frag like '%s%%'
searchPriority 8

    git commit -m 'add gold table search rule refs #19859' trackDb.ra

    # verify searches work in the position box

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

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

    # Masking statistics
    cat faSize.susScr11.cleanWMSdust.txt
# 2501912388 bases (29864684 N's 2472047704 real 1563211996 upper
#	908835708 lower) in 613 sequences in 1 files
# Total size: mean 4081423.1 sd 23780904.1 min 15096 (chrUn_NW_018085360v1)
#	max 274330532 (chr1) median 49329
# %36.33 masked total, %36.76 masked real

    cat fb.susScr11.rmsk.windowmaskerSdust.txt
    # 677163594 bases of 2501912388 (27.066%) in intersection

##########################################################################
# cpgIslands - (DONE - 2017-07-27 - Hiram)
    mkdir /hive/data/genomes/susScr11/bed/cpgIslands
    cd /hive/data/genomes/susScr11/bed/cpgIslands
    time (doCpgIslands.pl -dbHost=hgwdev -bigClusterHub=ku \
      -workhorse=hgwdev -smallClusterHub=ku susScr11) > do.log 2>&1
    # real    4m52.080s

    cat fb.susScr11.cpgIslandExt.txt
    # 32550865 bases of 2472073034 (1.317%) in intersection

##############################################################################
# ncbiRefSeq gene track (TBD - 2016-05-05 - Hiram)
    mkdir /hive/data/genomes/susScr11/bed/ncbiRefSeq
    cd /hive/data/genomes/susScr11/bed/ncbiRefSeq

    # working on this script, running step by step:
    time (/cluster/home/hiram/kent/src/hg/utils/automation/doNcbiRefSeq.pl \
  -stop=download -buildDir=`pwd` -bigClusterHub=ku \
  -fileServer=hgwdev -workhorse=hgwdev -smallClusterHub=ku -dbHost=hgwdev \
      refseq vertebrate_mammalian Pan_paniscus \
         GCF_000258655.2_panpan1.1 susScr11) > download.log 2>&1
    # real    12m36.320s

    time (/cluster/home/hiram/kent/src/hg/utils/automation/doNcbiRefSeq.pl \
  -continue=process -stop=process -buildDir=`pwd` -bigClusterHub=ku \
  -fileServer=hgwdev -workhorse=hgwdev -smallClusterHub=ku -dbHost=hgwdev \
      refseq vertebrate_mammalian Pan_paniscus \
         GCF_000258655.2_panpan1.1 susScr11) > process.log 2>&1
    # real    4m22.621s

    time (/cluster/home/hiram/kent/src/hg/utils/automation/doNcbiRefSeq.pl \
  -continue=load -stop=load -buildDir=`pwd` -bigClusterHub=ku \
  -fileServer=hgwdev -workhorse=hgwdev -smallClusterHub=ku -dbHost=hgwdev \
      refseq vertebrate_mammalian Pan_paniscus \
         GCF_000258655.2_panpan1.1 susScr11) > load.log 2>&1
    # real    0m21.690s

    cat fb.ncbiRefSeq.susScr11.txt
    # 74646536 bases of 2725937399 (2.738%) in intersection

##############################################################################
# genscan - (DONE - 2017-07-27 - Hiram)
    mkdir /hive/data/genomes/susScr11/bed/genscan
    cd /hive/data/genomes/susScr11/bed/genscan
    time (doGenscan.pl -buildDir=`pwd` -workhorse=hgwdev -dbHost=hgwdev \
      -bigClusterHub=ku susScr11) > do.log 2>&1
    # real    737m1.735s

    cat fb.susScr11.genscan.txt
    #   55049663 bases of 2472073034 (2.227%) in intersection

    cat fb.susScr11.genscanSubopt.txt
    #   48834412 bases of 2472073034 (1.975%) in intersection

#############################################################################
# augustus gene track (DONE - 2017-07-27 - Hiram)

    mkdir /hive/data/genomes/susScr11/bed/augustus
    cd /hive/data/genomes/susScr11/bed/augustus
    time (doAugustus.pl -buildDir=`pwd` -bigClusterHub=ku \
        -species=human -dbHost=hgwdev \
           -workhorse=hgwdev susScr11) > do.log 2>&1
    # real    207m36.088s

    cat fb.susScr11.augustusGene.txt
    # 47127389 bases of 2472073034 (1.906%) in intersection

#############################################################################
# Create kluster run files (DONE - 2017-07-27 - Hiram)

    # numerator is susScr11 gapless bases "real" as reported by:
    featureBits -noRandom -noHap susScr11 gap
    # 29824354 bases of 2405454322 (1.240%) 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 \( 2405454322 / 2861349177 \) \* 1024
    # ( 2405454322 / 2861349177 ) * 1024 = 860.847479

    # ==> use -repMatch=800 according to size scaled down from 1024 for human.
    #   and rounded down to nearest 100
    cd /hive/data/genomes/susScr11
    time blat susScr11.2bit \
         /dev/null /dev/null -tileSize=11 -makeOoc=jkStuff/susScr11.11.ooc \
        -repMatch=800
    #   Wrote 34181 overused 11-mers to jkStuff/susScr11.11.ooc
    #	real    0m44.366s

    #   check non-bridged gaps to see what the typical size is:
    hgsql -N \
        -e 'select * from gap where bridge="no" order by size;' susScr11 \
        | sort -k7,7nr
    #   minimum size is 50000
    gapToLift -verbose=2 -minGap=50000 susScr11 \
	jkStuff/susScr11.nonBridged.lft -bedFile=jkStuff/susScr11.nonBridged.bed

#########################################################################
# LIFTOVER TO susScr3 (DONE - 2017-07-27 - Hiram )
    mkdir /hive/data/genomes/susScr11/bed/blat.susScr3.2017-07-27
    cd /hive/data/genomes/susScr11/bed/blat.susScr3.2017-07-27
    # -debug run to create run dir, preview scripts...
    doSameSpeciesLiftOver.pl -debug \
        -ooc /hive/data/genomes/susScr11/jkStuff/susScr11.11.ooc \
	   susScr11 susScr3
    # Real run:
    time (doSameSpeciesLiftOver.pl -verbose=2 \
        -ooc /hive/data/genomes/susScr11/jkStuff/susScr11.11.ooc \
	-bigClusterHub=ku -dbHost=hgwdev -workhorse=hgwdev \
	 susScr11 susScr3) > do.log 2>&1 &
    #   real    252m20.235s

    # verify browser can run a view in other genomes to susScr3 from susScr11

#########################################################################
# lastz/chain/net swap from hg38 (DONE - 2017-08-02 - Hiram)
    # alignment to hg38
    cd /hive/data/genomes/hg38/bed/lastzSusScr11.2017-07-31

    cat fb.hg38.chainSusScr11Link.txt
    # 1463754915 bases of 3049335806 (48.002%) in intersection

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

    time (doBlastzChainNet.pl -verbose=2 \
      /hive/data/genomes/hg38/bed/lastzSusScr11.2017-07-31/DEF \
        -swap -chainMinScore=3000 -chainLinearGap=medium \
          -workhorse=hgwdev -smallClusterHub=ku -bigClusterHub=ku \
            -syntenicNet) > swap.log 2>&1
    #  real    107m7.364s

    cat fb.susScr11.chainHg38Link.txt
    # 1387973267 bases of 2472073034 (56.146%) in intersection

    time (doRecipBest.pl -workhorse=hgwdev -buildDir=`pwd` susScr11 hg38) \
        > rbest.log 2>&1
    # real    598m8.675s

#########################################################################
# lastz/chain/net swap from mm10 (DONE - 2017-08-02 - Hiram)
    # alignment on mm10
    cd /hive/data/genomes/mm10/bed/lastzSusScr11.2017-07-31
    cat fb.mm10.chainSusScr11Link.txt
    #	731012356 bases of 2652783500 (27.556%) in intersection

    mkdir /hive/data/genomes/susScr11/bed/blastz.mm10.swap
    cd /hive/data/genomes/susScr11/bed/blastz.mm10.swap
    time (doBlastzChainNet.pl -verbose=2 \
	/hive/data/genomes/mm10/bed/lastzSusScr11.2017-07-31/DEF \
	-swap -syntenicNet \
	-workhorse=hgwdev -smallClusterHub=ku -bigClusterHub=ku \
	-chainMinScore=3000 -chainLinearGap=medium) > swap.log 2>&1
    #	real    61m6.153s

    cat fb.susScr11.chainMm10Link.txt
    #	715277290 bases of 2472073034 (28.934%) in intersection

    time (doRecipBest.pl -workhorse=hgwdev susScr11 mm10 \
      -buildDir=`pwd` -workhorse=hgwdev) > rbest.log 2>&1 &
    # real    358m15.340s

##############################################################################
# GENBANK AUTO UPDATE (DONE - 2017-07-29 - Hiram)
    ssh hgwdev
    cd $HOME/kent/src/hg/makeDb/genbank
    git pull
    # /cluster/data/genbank/data/organism.lst shows:
    # organism       mrnaCnt estCnt  refSeqCnt
    # Sus scrofa      41240   1676416 4173

    # edit etc/genbank.conf to add susScr11 just before susScr3

# susScr11 (pig)
susScr11.serverGenome = /hive/data/genomes/susScr11/susScr11.2bit
susScr11.clusterGenome = /hive/data/genomes/susScr11/susScr11.2bit
susScr11.ooc = /hive/data/genomes/susScr11/jkStuff/susScr11.11.ooc
susScr11.lift = /hive/data/genomes/susScr11/jkStuff/susScr11.nonBridged.lft
susScr11.perChromTables = no
susScr11.refseq.mrna.native.pslCDnaFilter  = ${finished.refseq.mrna.native.pslCDnaFilter}
susScr11.refseq.mrna.xeno.pslCDnaFilter    = ${finished.refseq.mrna.xeno.pslCDnaFilter}
susScr11.genbank.mrna.native.pslCDnaFilter = ${finished.genbank.mrna.native.pslCDnaFilter}
susScr11.genbank.mrna.xeno.pslCDnaFilter   = ${finished.genbank.mrna.xeno.pslCDnaFilter}
susScr11.genbank.est.native.pslCDnaFilter  = ${finished.genbank.est.native.pslCDnaFilter}
susScr11.genbank.est.xeno.pslCDnaFilter    = ${finished.genbank.est.xeno.pslCDnaFilter}
susScr11.downloadDir = susScr11
susScr11.refseq.mrna.native.load  = yes
susScr11.refseq.mrna.xeno.load  = yes
# DO NOT NEED genbank.mrna.xeno except for human, mouse
susScr11.genbank.mrna.xeno.load = yes
susScr11.genbank.mrna.xeno.loadDesc = no
susScr11.genbank.est.native.load = yes
# susScr11.upstreamGeneTbl = ensGene
# susScr11.upstreamMaf = multiz12way
# /hive/data/genomes/susScr11/bed/multiz12way/species.list

    git commit -m "Added susScr11; refs #19879" etc/genbank.conf
    git push
    # update /cluster/data/genbank/:
    make etc-update

# Edit src/lib/gbGenome.c to add new species.  Skipped

    screen      #  control this business with a screen since it takes a while
    cd /cluster/data/genbank

    time ./bin/gbAlignStep -initial susScr11
    #  logFile: var/build/logs/2017.07.27-20:49:34.susScr11.initalign.log

    #   real    1486m19.233s

    tail -2 var/build/logs/2017.07.27-20:49:34.susScr11.initalign.log
# hgwdev 2017.07.28-21:20:42 susScr11.initalign: Succeeded: susScr11
# hgwdev 2017.07.28-21:35:53 susScr11.initalign: finish

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

    # load database when finished
    ssh hgwdev
    cd /cluster/data/genbank
    time ./bin/gbDbLoadStep -drop -initialLoad susScr11
    # logFile: var/dbload/hgwdev/logs/2017.07.29-07:05:26.susScr11.dbload.log
    # real    81m41.107s

    tail -1 var/dbload/hgwdev/logs/2017.07.29-07:05:26.susScr11.dbload.log
    # hgwdev 2017.07.29-08:27:07 susScr11.dbload: finish

    # enable daily alignment and update of hgwdev
    cd ~/kent/src/hg/makeDb/genbank
    git pull
    # add susScr11 to:
    #   etc/align.dbs etc/hgwdev.dbs
    git add etc/align.dbs etc/hgwdev.dbs
    git commit -m "Added susScr11 - pig refs #19859" etc/align.dbs etc/hgwdev.dbs
    git push
    make etc-update

##############################################################################
#  BLATSERVERS ENTRY (DONE - 2017-08-03 - 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 ("susScr11", "blat1b", "17880", "1", "0"); \
	INSERT INTO blatServers (db, host, port, isTrans, canPcr) \
	VALUES ("susScr11", "blat1b", "17881", "0", "1");' \
	    hgcentraltest
    #	test it with some sequence

##############################################################################
# set default position to OPN1SW gene displays  (DONE - 2017-08-03 - Hiram)
# same as susScr3:
    hgsql -e \
'update dbDb set defaultPos="chr18:19836295-19840119" where name="susScr11";' \
	hgcentraltest

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

    cd /hive/data/genomes/susScr11
    time (makeDownloads.pl susScr11) > downloads.log 2>&1
    # real    17m8.716s

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

    #   check for errors in stderr.out, some are OK, e.g.:
    # writing redmine listings to
    # redmine.susScr11.file.list
    # redmine.susScr11.table.list
    # redmine.susScr11.releaseLog.txt
    # WARNING: susScr11 does not have seq
    # WARNING: susScr11 does not have extFile

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