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

# This file describes browser build for the rheMac10

#  Can use existing photograph (otherwise find one before starting here)

#########################################################################
#  Initial steps, reuse existing photograph (DONE - 2019-07-01 - Hiram)

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

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

sed -e 's/galGal6/rheMac10/g; s/GalGal6/RheMac10/g; s/DONE/TBD/g;' \
  ../galGal6/initialBuild.txt > initialBuild.txt

mkdir -p /hive/data/genomes/rheMac10/refseq
cd /hive/data/genomes/rheMac10

#  Can use existing photograph
cp -p ../rn6/photoReference.txt ./
cat photoReference.txt
photoCreditURL  http://www.genome.gov/dmd/img.cfm?node=Photos/Animals/Rat%20(brown%20norway)&id=79111
photoCreditName NHGRI press photos

## download from NCBI
cd /hive/data/genomes/rheMac10/refseq

time rsync -L -a -P --stats \
rsync://ftp.ncbi.nlm.nih.gov/genomes/refseq/vertebrate_mammalian/Macaca_mulatta/all_assembly_versions/GCF_003339765.1_Mmul_10/ ./

# sent 3,937 bytes  received 6,369,691,679 bytes  56,619,516.59 bytes/sec
# total size is 6,368,121,788  speedup is 1.00

# real    1m51.641s

# this information is from the top of 
#    rheMac10/refseq/*_assembly_report.txt
#    (aka: rheMac10/refseq/GCF_003339765.1_Mmul_10_assembly_report.txt

# Assembly name:  Mmul_10
# Organism name:  Macaca mulatta (Rhesus monkey)
# Taxid:          9544
# BioSample:      SAMN09435472
# BioProject:     PRJNA476474
# Submitter:      The Genome Institute at Washington University School of Medicine
# Date:           2019-02-13
# Assembly type:  haploid
# Release type:   major
# Assembly level: Chromosome
# Genome representation: full
# WGS project:    QNVO02
# GenBank assembly accession: GCA_003339765.3
# RefSeq assembly accession: GCF_003339765.1
# RefSeq assembly and GenBank assemblies identical: no
#
## Assembly-Units:
## GenBank Unit Accession       RefSeq Unit Accession   Assembly-Unit name
## GCA_003339795.3      GCF_003339795.1 Primary Assembly
##      GCF_000005875.1 non-nuclear
#

# check assembly size for later reference:

faSize G*10_genomic.fna.gz
# 2971331530 bases (34438805 N's 2936892725 real 1810047126 upper
#	1126845599 lower) in 2939 sequences in 1 files
# Total size: mean 1011000.9 sd 12006707.4 min 217 (NW_021162293.1)
#	max 223616942 (NC_041754.1) median 26547
# %37.92 masked total, %38.37 masked real

#############################################################################
# establish config.ra file (DONE - Hiram - 2019-07-01)
    cd /hive/data/genomes/rheMac10
    ~/kent/src/hg/utils/automation/prepConfig.pl rheMac10 primate rhesus \
       refseq/*_assembly_report.txt > rheMac10.config.ra

    # compare with previous version to see if it is sane:
    diff rheMac10.config.ra ../rheMac8/rheMac8.config.ra

    # verify it really does look sane
    cat rheMac10.config.ra
# config parameters for makeGenomeDb.pl:
db rheMac10
clade primate
genomeCladePriority 35
scientificName Macaca mulatta
commonName rhesus
assemblyDate Feb. 2019
assemblyLabel The Genome Institute at Washington University School of Medicine
assemblyShortLabel Mmul_10
orderKey 18417
# mitochondrial sequence included in refseq release
# mitoAcc NC_005943.1
mitoAcc none
fastaFiles /hive/data/genomes/rheMac10/ucsc/*.fa.gz
agpFiles /hive/data/genomes/rheMac10/ucsc/*.agp
# qualFiles none
dbDbSpeciesDir rhesus
photoCreditURL  http://www.genome.gov/dmd/img.cfm?node=Photos/Animals/Rat%20(brown%20norway)&id=79111
photoCreditName NHGRI press photos
ncbiGenomeId 215
ncbiAssemblyId 2345051
ncbiAssemblyName Mmul_10
ncbiBioProject 476474
ncbiBioSample SAMN09435472
genBankAccessionID GCF_003339765.1
taxId 9544

#############################################################################
# setup UCSC named files (DONE - 2019-07-01 - Hiram)

    mkdir /hive/data/genomes/rheMac10/ucsc
    cd /hive/data/genomes/rheMac10/ucsc

    # check for duplicate sequences:
    time faToTwoBit -noMask ../refseq/G*10_genomic.fna.gz refseq.2bit
    #  real    0m38.595s

    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
    # remove it later

    # new option required to ucscCompositeAgp.pl 2016-04-13
    time ~/kent/src/hg/utils/automation/ucscCompositeAgp.pl \
      ../refseq/G*10_genomic.fna.gz \
	../refseq/*_assembly_structure/Primary_Assembly
# constructing refseq.2bit from ../refseq/GCF_000002315.4_Gallus_gallus-5.0_genomic.fna.gz
NC_027914.1 chrY
NC_041754.1 chr1
NC_041755.1 chr2
NC_041756.1 chr3
NC_041757.1 chr4
NC_041758.1 chr5
NC_041759.1 chr6
NC_041760.1 chr7
NC_041761.1 chr8
NC_041762.1 chr9
NC_041763.1 chr10
NC_041764.1 chr11
NC_041765.1 chr12
NC_041766.1 chr13
NC_041767.1 chr14
NC_041768.1 chr15
NC_041769.1 chr16
NC_041770.1 chr17
NC_041771.1 chr18
NC_041772.1 chr19
NC_041773.1 chr20
NC_041774.1 chrX

real    10m33.076s

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

    time ~/kent/src/hg/utils/automation/unlocalizedWithChroms.pl \
       ../refseq/*_assembly_structure/Primary_Assembly
# 11
# 7
# 17
# 2
# 1
# 18
# 16
# 13
# 6
# X
# 3
# 9
# 12
# 20
# 14
# 15
# 8
# 4
# 19
# 10
# 5
# processed 291 sequences into chr*_random.gz 21 files

real    0m7.023s

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

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

    cat chrM.agp
# chrM    1       16564   1       O       NC_005943.1     1       16564   +

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

    faSize chrM.fa.gz
# 16564 bases (0 N's 16564 real 16564 upper 0 lower) in 1 sequences in 1 files

    # verify fasta and AGPs agree
    time faToTwoBit *.fa.gz test.2bit
    # real    0m56.726s

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

    # and no sequence lost from orginal:
    twoBitToFa test.2bit stdout | faSize stdin
# 2971331530 bases (34438805 N's 2936892725 real 2936892725 upper 0 lower)
#	in 2939 sequences in 1 files
# Total size: mean 1011000.9 sd 12006707.4 min 217 (chrUn_NW_021162293v1)
#	max 223616942 (chr1) median 26547

    # same numbers as above (except for upper/lower masking)
# 2971331530 bases (34438805 N's 2936892725 real 1810047126 upper
#	1126845599 lower) in 2939 sequences in 1 files
# Total size: mean 1011000.9 sd 12006707.4 min 217 (NW_021162293.1)
#	max 223616942 (NC_041754.1) median 26547

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

#############################################################################
#  Initial database build (DONE - 2019-07-01 - Hiram)

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

    # then finish it off:
    time (makeGenomeDb.pl -workhorse=hgwdev -dbHost=hgwdev \
       -fileServer=hgwdev -continue=db rheMac10.config.ra) > db.log 2>&1
    # real    15m24.340s

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

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

##############################################################################
# cpgIslands on UNMASKED sequence (DONE - 2019-07-01 - Hiram)
    mkdir /hive/data/genomes/rheMac10/bed/cpgIslandsUnmasked
    cd /hive/data/genomes/rheMac10/bed/cpgIslandsUnmasked

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

    cat fb.rheMac10.cpgIslandExtUnmasked.txt
    # 38819797 bases of 2936892733 (1.322%) in intersection

#############################################################################
# cytoBandIdeo - (DONE - 2019-07-01 - Hiram)
    mkdir /hive/data/genomes/rheMac10/bed/cytoBand
    cd /hive/data/genomes/rheMac10/bed/cytoBand
    makeCytoBandIdeo.csh rheMac10

#############################################################################
# run up idKeys files for chromAlias/ncbiRefSeq (DONE - 2019-07-01 - Hiram)
    mkdir /hive/data/genomes/rheMac10/bed/idKeys
    cd /hive/data/genomes/rheMac10/bed/idKeys

    time (doIdKeys.pl \
        -twoBit=/hive/data/genomes/rheMac10/rheMac10.unmasked.2bit \
        -buildDir=`pwd` rheMac10) > do.log 2>&1 &
    # real    4m9.394s

    cat rheMac10.keySignature.txt
    #  69c74f5fa18fe2421cabbcd0e80657e9

#############################################################################
# gapOverlap (DONE - 2019-07-01 - Hiram)
    mkdir /hive/data/genomes/rheMac10/bed/gapOverlap
    cd /hive/data/genomes/rheMac10/bed/gapOverlap
    time (doGapOverlap.pl \
        -twoBit=/hive/data/genomes/rheMac10/rheMac10.unmasked.2bit rheMac10 ) \
        > do.log 2>&1 &
    # real    1m3.768s

    # there is only one:
    wc -l bed.tab
    # 1 bed.tab
    cut -f2- bed.tab
chr4    51027118        51073686        chr4:51027119-51073686  1000    +      51027118 51073686        0       2       1000,1000       0,45568

    cat fb.rheMac10.gapOverlap.txt
    # 2000 bases of 2971331530 (0.000%) in intersection

#############################################################################
# tandemDups (DONE - 2019-07-01 - Hiram)
    mkdir /hive/data/genomes/rheMac10/bed/tandemDups
    cd /hive/data/genomes/rheMac10/bed/tandemDups
    time (~/kent/src/hg/utils/automation/doTandemDup.pl \
  -twoBit=/hive/data/genomes/rheMac10/rheMac10.unmasked.2bit rheMac10) \
        > do.log 2>&1 &
    # real    225m10.394s

    cat fb.rheMac10.tandemDups.txt
    # 81485851 bases of 2971331530 (2.742%) in intersection

    bigBedInfo rheMac10.tandemDups.bb | sed -e 's/^/#  /;'
#  version: 4
#  fieldCount: 13
#  hasHeaderExtension: yes
#  isCompressed: yes
#  isSwapped: 0
#  extraIndexCount: 0
#  itemCount: 1,431,488
#  primaryDataSize: 37,737,612
#  primaryIndexSize: 176,456
#  zoomLevels: 9
#  chromCount: 2756
#  basesCovered: 1,568,894,232
#  meanDepth (of bases covered): 8.122557
#  minDepth: 1.000000
#  maxDepth: 314.000000
#  std of depth: 13.438018

#########################################################################
# ucscToINSDC and ucscToRefSeq table/track (DONE - 2019-07-01 - Hiram)
    # construct idKeys for the refseq sequence
    mkdir /hive/data/genomes/rheMac10/refseq/idKeys
    cd /hive/data/genomes/rheMac10/refseq/idKeys
    faToTwoBit ../GCF_003339765.1_Mmul_10_genomic.fna.gz rheMac10.refSeq.2bit

    time (doIdKeys.pl -buildDir=`pwd` \
        -twoBit=`pwd`/rheMac10.refSeq.2bit refseqRheMac10)  > do.log 2>&1 &
    # real    4m8.863s

    cat refseqRheMac10.keySignature.txt
    #  69c74f5fa18fe2421cabbcd0e80657e9

    # and the genbank sequence needs keys too:
    mkdir /hive/data/genomes/rheMac10/refseq/idKeysGenbank
    cd /hive/data/genomes/rheMac10/refseq/idKeysGenbank
    faToTwoBit /hive/data/outside/ncbi/genomes/genbank/vertebrate_mammalian/Macaca_mulatta/all_assembly_versions/GCA_003339765.3_Mmul_10/GCA_003339765.3_Mmul_10_genomic.fna.gz rheMac10.genbank.2bit

    time (doIdKeys.pl -buildDir=`pwd` \
        -twoBit=`pwd`/rheMac10.genbank.2bit genbankRheMac10)  > do.log 2>&1 &
    # real    4m6.755s


    cat genbankRheMac10.keySignature.txt
    #  1eb34462822483c9262e98186f71a286

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

    join -t$'\t' ../idKeys/rheMac10.idKeys.txt \
        ../../refseq/idKeysGenbank/genbankRheMac10.idKeys.txt | cut -f2- \
          | sort -k1,1 | join -t$'\t' <(sort -k1,1 ../../chrom.sizes) - \
            | awk '{printf "%s\t0\t%d\t%s\n", $1, $2, $3}' \
               | sort -k1,1 -k2,2n > ucscToINSDC.bed

    join -t$'\t' ../idKeys/rheMac10.idKeys.txt \
        ../../refseq/idKeys/refseqRheMac10.idKeys.txt | cut -f2- \
          | sort -k1,1 | join -t$'\t' <(sort -k1,1 ../../chrom.sizes) - \
            | awk '{printf "%s\t0\t%d\t%s\n", $1, $2, $3}' \
               | sort -k1,1 -k2,2n > ucscToRefSeq.bed

    # should be same line counts throughout:
    wc -l * ../../chrom.sizes
    # 2938 ucscToINSDC.bed
    # 2939 ucscToRefSeq.bed
    # 2939 ../../chrom.sizes

    # need to find the accession for the INSDC equivalent to chrM:
    egrep chrM *
# ucscToRefSeq.bed:chrM   0       16564   NC_005943.1

    # lookup that accession at NCBI Entrez: AY612638.1
    # and add to ucscToINSDC.bed:
    printf "chrM\t0\t16564\tAY612638.1\n" >> ucscToINSDC.bed
    # verify:
    grep chrM *
ucscToINSDC.bed:chrM    0       16564   AY612638.1
ucscToRefSeq.bed:chrM   0       16564   NC_005943.1

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

    # should be quiet for all OK
    checkTableCoords rheMac10

    # should cover %100 entirely:
    featureBits -countGaps rheMac10 ucscToINSDC
    # 2971331530 bases of 2971331530 (100.000%) in intersection
    featureBits -countGaps rheMac10 ucscToRefSeq
    # 2971331530 bases of 2971331530 (100.000%) in intersection

#########################################################################
# add chromAlias table (DONE - 2019-07-01 - Hiram)

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

    hgsql -N -e 'select chrom,name from ucscToRefSeq;' rheMac10 \
        | sort -k1,1 > ucsc.refseq.tab
    hgsql -N -e 'select chrom,name from ucscToINSDC;' rheMac10 \
        | sort -k1,1 > ucsc.genbank.tab

    ### Adding Ensembl alias with v95 release, after idKeys made: 2019-01-16
    join -t$'\t' ../idKeys/rheMac10.idKeys.txt \
        ../../ens95/ensRheMac10.idKeys.txt | cut -f2- \
          | sort -k1,1 | join -t$'\t' <(sort -k1,1 ../../chrom.sizes) - \
            | awk '{printf "%s\t0\t%d\t%s\n", $1, $2, $3}' \
               | sort -k1,1 -k2,2n > ucscToEns.bed
    cut -f1,4 ucscToEns.bed | sort > ucsc.ensembl.tab
    wc -l *.bed
#   2210 ucscToEns.bed
#   2211 ucscToINSDC.bed
#   2211 ucscToRefSeq.bed

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

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

    # verify chrM is here properly:
    grep chrM rheMac10.chromAlias.tab 
# AY612638.1      chrM    genbank
# NC_005943.1     chrM    refseq

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

#########################################################################
# fixup search rule for assembly track/gold table (DONE - 2019-07-03 - Hiram)
    cd ~/kent/src/hg/makeDb/trackDb/rhesus/rheMac10
    # preview prefixes and suffixes:
    hgsql -N -e "select frag from gold;" rheMac10 \
      | sed -e 's/[0-9][0-9]*//;' | sort | uniq -c 
      2 AC.10
      2 AC.11
      1 AC.13
      1 AC.17
      4 AC.2
     36 AC.3
     22 AC.4
      8 AC.5
      2 AC.6
      5 AC.7
      4 AC.8
      2 AC.9
      1 NC_.1
   3178 QNVO.1

    # implies a rule: '[ANQ][CN][V0-9_][O0-9][0-9]+(\.[0-9]+)?'

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

    hgsql -N -e "select frag from gold;" rheMac10 \
       | egrep -e '[ANQ][CN][V0-9_][O0-9][0-9]+(\.[0-9]+)?' | wc -l
    # 3268

    hgsql -N -e "select frag from gold;" rheMac10 \
       | egrep -v -e '[ANQ][CN][V0-9_][O0-9][0-9]+(\.[0-9]+)?' | wc -l
    # 0

    # hence, add to trackDb/rhesus/rheMac10/trackDb.ra
searchTable gold
shortCircuit 1
termRegex [ANQ][CN][V0-9_][O0-9][0-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 - 2019-07-01 - Hiram)
    mkdir /hive/data/genomes/rheMac10/bed/repeatMasker
    cd /hive/data/genomes/rheMac10/bed/repeatMasker
    time  (doRepeatMasker.pl -buildDir=`pwd` \
        -bigClusterHub=ku -dbHost=hgwdev -workhorse=hgwdev \
        -smallClusterHub=ku rheMac10) > do.log 2>&1
    # real    500m43.380s

    cat faSize.rmsk.txt
# 2971331530 bases (34438805 N's 2936892725 real 1399379596 upper
#	1537513129 lower) in 2939 sequences in 1 files
# Total size: mean 1011000.9 sd 12006707.4 min 217 (chrUn_NW_021162293v1)
#	max 223616942 (chr1) median 26547
# %51.74 masked total, %52.35 masked real

    egrep -i "versi|relea" do.log
# RepeatMasker version development-$Id: RepeatMasker,v 1.332 2017/04/17 19:01:11 rhubley Exp $
#    February 01 2017 (open-4-0-8) 1.332 version of RepeatMasker
# CC    Dfam_Consensus RELEASE 20181026;                            *
# CC    RepBase RELEASE 20181026;         

    time featureBits -countGaps rheMac10 rmsk
    # 1537510421 bases of 2971331530 (51.745%) in intersection
    # real    0m40.763s

    # 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;' rheMac10 \
        | bedSingleCover.pl stdin | ave -col=4 stdin | grep "^total"
    # total 1537510421.000000
    # real    0m24.256s

##########################################################################
# running simple repeat (DONE - 2019-07-01 - Hiram)

    # The '-trf409 5' is a bit smaller than human which is 6

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

    cat fb.simpleRepeat
    # 158143361 bases of 2936892733 (5.385%) in intersection

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

    # add to rmsk after it is done:
    twoBitMask rheMac10.rmsk.2bit \
        -add bed/simpleRepeat/trfMask.bed rheMac10.2bit
    #   you can safely ignore the warning about fields >= 13
    twoBitToFa rheMac10.2bit stdout | faSize stdin > faSize.rheMac10.2bit.txt
    cat faSize.rheMac10.2bit.txt
# 2971331530 bases (34438805 N's 2936892725 real 1397548789 upper
#	1539343936 lower) in 2939 sequences in 1 files
# Total size: mean 1011000.9 sd 12006707.4 min 217 (chrUn_NW_021162293v1)
#	max 223616942 (chr1) median 26547
# %51.81 masked total, %52.41 masked real

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

#########################################################################
# CREATE MICROSAT TRACK (DONE - 2019-07-02 - Hiram)
    ssh hgwdev
    mkdir /cluster/data/rheMac10/bed/microsat
    cd /cluster/data/rheMac10/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 rheMac10 microsat microsat.bed
    # Read 32040 elements of size 4 from microsat.bed

##########################################################################
## WINDOWMASKER (DONE - 2019-07-01 - Hiram)

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

    # Masking statistics
    cat faSize.rheMac10.cleanWMSdust.txt
# 2971331530 bases (34438805 N's 2936892725 real 1792976685 upper
#	1143916040 lower) in 2939 sequences in 1 files
# Total size: mean 1011000.9 sd 12006707.4 min 217 (chrUn_NW_021162293v1)
#	max 223616942 (chr1) median 26547
# %38.50 masked total, %38.95 masked real

    cat fb.rheMac10.rmsk.windowmaskerSdust.txt
    # 909614480 bases of 2971331530 (30.613%) in intersection

##########################################################################
# cpgIslands - (DONE - 2019-07-02 - Hiram)
    mkdir /hive/data/genomes/rheMac10/bed/cpgIslands
    cd /hive/data/genomes/rheMac10/bed/cpgIslands
    time (doCpgIslands.pl -dbHost=hgwdev -bigClusterHub=ku \
      -workhorse=hgwdev -smallClusterHub=ku rheMac10) > do.log 2>&1
    # real    3m51.796s

    cat fb.rheMac10.cpgIslandExt.txt
    # 21622534 bases of 2936892733 (0.736%) in intersection

##############################################################################
# genscan - (DONE - 2019-07-02 - Hiram)
    mkdir /hive/data/genomes/rheMac10/bed/genscan
    cd /hive/data/genomes/rheMac10/bed/genscan
    time (doGenscan.pl -buildDir=`pwd` -workhorse=hgwdev -dbHost=hgwdev \
      -bigClusterHub=ku rheMac10) > do.log 2>&1
    # real    134m51.892s

    cat fb.rheMac10.genscan.txt
    # 53848328 bases of 2936892733 (1.834%) in intersection

    cat fb.rheMac10.genscanSubopt.txt
    # 52550217 bases of 2936892733 (1.789%) in intersection

#########################################################################
# Create kluster run files (DONE - 2019-07-01 - Hiram)

    # numerator is rheMac10 gapless bases "real" as reported by:
    featureBits -noRandom -noHap rheMac10 gap
    # 32449617 bases of 2821543288 (1.150%) 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 \( 2821543288 / 2861349177 \) \* 1024
    #  ( 2821543288 / 2861349177 ) * 1024 = 1009.754542

    # ==> use -repMatch=350 according to size scaled down from 1024 for human.
    #   and rounded down to nearest 50
    cd /hive/data/genomes/rheMac10
    blat rheMac10.2bit \
         /dev/null /dev/null -tileSize=11 -makeOoc=jkStuff/rheMac10.11.ooc \
        -repMatch=1000
    #   Wrote 31768 overused 11-mers to jkStuff/rheMac10.11.ooc
    # rheMac8 at repMatch=900:
    #   Wrote 43065 overused 11-mers to jkStuff/rheMac8.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;' rheMac10 \
        | sort -k7,7nr | ave -col=7 stdin
    # min 100.000000
    # max 1000000.000000

    # minimum gap size is 100 and produces a reasonable number of lifts
    gapToLift -verbose=2 -minGap=100 rheMac10 jkStuff/rheMac10.nonBridged.lft \
        -bedFile=jkStuff/rheMac10.nonBridged.bed
    wc -l jkStuff/rheMac10.nonBri*
    # 2979 jkStuff/rheMac10.nonBridged.bed
    # 2979 jkStuff/rheMac10.nonBridged.lft

########################################################################
# lastz/chain/net swap human/hg38 (DONE - 2019-07-03 - Hiram)
    # original alignment
    cd /hive/data/genomes/hg38/bed/lastzRheMac10.2019-07-03

    cat fb.hg38.chainRheMac10Link.txt
    # 2596157811 bases of 3095998939 (83.855%) in intersection
    cat fb.hg38.chainSynRheMac10Link.txt
    # 2564291936 bases of 3095998939 (82.826%) in intersection
    cat fb.hg38.chainRBest.RheMac10.txt
    # 2422981614 bases of 3095998939 (78.262%) in intersection

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

    time (doBlastzChainNet.pl -verbose=2 \
      /hive/data/genomes/hg38/bed/lastzRheMac10.2019-07-03/DEF \
        -swap -chainMinScore=5000 -chainLinearGap=medium \
          -workhorse=hgwdev -smallClusterHub=ku -bigClusterHub=ku \
            -syntenicNet) > swap.log 2>&1
    #  real    61m39.505s

    cat fb.rheMac10.chainHg38Link.txt
    # 2485840115 bases of 2936892733 (84.642%) in intersection
    cat fb.rheMac10.chainSynHg38Link.txt
    # 2465210442 bases of 2936892733 (83.939%) in intersection

    time (doRecipBest.pl -workhorse=hgwdev -load -buildDir=`pwd` rheMac10 hg38) > rbest.log 2>&1
    # real    126m29.919s

    cat fb.rheMac10.chainRBest.Hg38.txt
    # 2424028492 bases of 2936892733 (82.537%) in intersection

#########################################################################
# lastz/chain/net swap mouse/mm10 (DONE - 2019-07-09 - Hiram)

    # original alignment
    cd /hive/data/genomes/mm10/bed/lastzRheMac10.2019-07-03

    cat fb.mm10.chainRheMac10Link.txt
    #	923559693 bases of 2652783500 (34.815%) in intersection
    cat fb.mm10.chainSynRheMac10Link.txt
    #   878479553 bases of 2652783500 (33.115%) in intersection
    cat fb.mm10.chainRBest.RheMac10.txt
    # 879885863 bases of 2652783500 (33.168%) in intersection

    mkdir /hive/data/genomes/rheMac10/bed/blastz.mm10.swap
    cd /hive/data/genomes/rheMac10/bed/blastz.mm10.swap
    time (doBlastzChainNet.pl -verbose=2 \
	/hive/data/genomes/mm10/bed/lastzRheMac10.2019-07-03/DEF \
	-swap -syntenicNet \
	-workhorse=hgwdev -smallClusterHub=ku -bigClusterHub=ku \
	-chainMinScore=3000 -chainLinearGap=medium) > swap.log 2>&1
    #	real    52m48.045s

    cat fb.rheMac10.chainMm10Link.txt
    #	918551088 bases of 2936892733 (31.276%) in intersection
    cat fb.rheMac10.chainSynMm10Link.txt
    #   876230433 bases of 2936892733 (29.835%) in intersection

    time (doRecipBest.pl -workhorse=hgwdev -load -buildDir=`pwd` rheMac10 mm10) > rbest.log 2>&1
    # real    303m40.303s

    cat fb.rheMac10.chainRBest.Mm10.txt
    # 878542993 bases of 2936892733 (29.914%) in intersection


#########################################################################
# lastz/chain/net swap human/hg19 (DONE - 2019-07-09 - Hiram)

    # original alignment
    cd /hive/data/genomes/hg19/bed/lastzRheMac10.2019-07-09

    cat fb.hg19.chainRheMac10Link.txt
    # 2604364199 bases of 2991694177 (87.053%) in intersection
    cat fb.hg19.chainSynRheMac10Link.txt
    # 2570126144 bases of 2991694177 (85.909%) in intersection
    cat fb.hg19.chainRBest.RheMac10.txt
    # 2421810991 bases of 2991694177 (80.951%) in intersection

    # and for the swap:
    mkdir /hive/data/genomes/rheMac10/bed/blastz.hg19.swap
    cd /hive/data/genomes/rheMac10/bed/blastz.hg19.swap

    time (doBlastzChainNet.pl -verbose=2 \
      /hive/data/genomes/hg19/bed/lastzRheMac10.2019-07-09/DEF \
        -swap -chainMinScore=5000 -chainLinearGap=medium \
          -workhorse=hgwdev -smallClusterHub=ku -bigClusterHub=ku \
            -syntenicNet) > swap.log 2>&1
    #  real    56m29.163s

    cat fb.rheMac10.chainHg19Link.txt
    # 2483311773 bases of 2936892733 (84.556%) in intersection
    cat fb.rheMac10.chainSynHg19Link.txt
    # 2461925388 bases of 2936892733 (83.828%) in intersection

    time (doRecipBest.pl -workhorse=hgwdev -load -buildDir=`pwd` rheMac10 hg19) > rbest.log 2>&1
    # real    112m15.825s

    cat fb.rheMac10.chainRBest.Hg19.txt
    # 2423781773 bases of 2936892733 (82.529%) in intersection

##############################################################################
# GENBANK AUTO UPDATE (DONE - 2019-07-03 - Hiram)
    ssh hgwdev
    cd $HOME/kent/src/hg/makeDb/genbank
    git pull
    # /cluster/data/genbank/data/organism.lst shows:
    # #organism       mrnaCnt estCnt  refSeqCnt
    # Macaca mulatta  381495  60276   5820

    # edit etc/genbank.conf to add rheMac10 just before galGal5

# rheMac10 Macaca mulatta (rhesus) (refseq assembly GCF_003339765.1)
rheMac10.serverGenome = /hive/data/genomes/rheMac10/rheMac10.2bit
rheMac10.ooc = /hive/data/genomes/rheMac10/jkStuff/rheMac10.11.ooc
rheMac10.lift = /hive/data/genomes/rheMac10/jkStuff/rheMac10.nonBridged.lft
rheMac10.perChromTables = no
rheMac10.refseq.mrna.native.pslCDnaFilter  = ${finished.refseq.mrna.native.pslCDnaFilter}
rheMac10.refseq.mrna.xeno.pslCDnaFilter    = ${finished.refseq.mrna.xeno.pslCDnaFilter}
rheMac10.genbank.mrna.native.pslCDnaFilter = ${finished.genbank.mrna.native.pslCDnaFilter}
rheMac10.genbank.mrna.xeno.pslCDnaFilter   = ${finished.genbank.mrna.xeno.pslCDnaFilter}
rheMac10.genbank.est.native.pslCDnaFilter  = ${finished.genbank.est.native.pslCDnaFilter}
rheMac10.genbank.est.xeno.pslCDnaFilter    = ${finished.genbank.est.xeno.pslCDnaFilter}
rheMac10.downloadDir = rheMac10
# DO NOT NEED genbank.mrna.xeno except for human, mouse(and in this case rhesus)
rheMac10.refseq.mrna.native.load  = yes
rheMac10.refseq.mrna.xeno.load  = yes
rheMac10.upstreamGeneTbl = refGene

    # verify the files specified exist before checking in the file:
  grep ^rheMac10 etc/genbank.conf | grep hive | awk '{print $NF}' | xargs ls -og
# -rw-rw-r-- 1    127080 Jul  1 11:03 /hive/data/genomes/rheMac10/jkStuff/rheMac10.11.ooc
# -rw-rw-r-- 1    177516 Jul  1 13:16 /hive/data/genomes/rheMac10/jkStuff/rheMac10.nonBridged.lft
# -rw-rw-r-- 1 775334369 Jul  2 07:48 /hive/data/genomes/rheMac10/rheMac10.2bit

    git commit -m "Added rheMac10 rhesus/Macaca mulatta; refs #23771" etc/genbank.conf
    git push
    # update /cluster/data/genbank/:
    make etc-update

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

    # wait a few days for genbank magic to take place, the tracks will
    # appear

#############################################################################
# augustus gene track (DONE - 2019-07-02 - Hiram)

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

    cat fb.rheMac10.augustusGene.txt
    # 50002729 bases of 2936892733 (1.703%) in intersection

#########################################################################
# ncbiRefSeq (DONE - 2019-07-02 - Hiram)

    mkdir /hive/data/genomes/rheMac10/bed/ncbiRefSeq
    cd /hive/data/genomes/rheMac10/bed/ncbiRefSeq
    # running step wise just to be careful
    time (~/kent/src/hg/utils/automation/doNcbiRefSeq.pl -buildDir=`pwd` \
      -bigClusterHub=ku -dbHost=hgwdev \
      -stop=download -fileServer=hgwdev -smallClusterHub=ku -workhorse=hgwdev \
      refseq vertebrate_mammalian Macaca_mulatta \
      GCF_003339765.1_Mmul_10 rheMac10) > download.log 2>&1
    # real    2m3.842s

    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_mammalian Macaca_mulatta \
      GCF_003339765.1_Mmul_10 rheMac10) > process.log 2>&1
    # real    2m6.030s

    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_mammalian Macaca_mulatta \
      GCF_003339765.1_Mmul_10 rheMac10) > load.log 2>&1
    # real    0m27.175s

    cat fb.ncbiRefSeq.rheMac10.txt
    #  92462016 bases of 2936892733 (3.148%) in intersection

    # add: include ../../refSeqComposite.ra alpha
    # to the rhesus/rheMac10/trackDb.ra to turn on the track in the browser

    featureBits -enrichment rheMac10 refGene ncbiRefSeq 
 # refGene 0.402%, ncbiRefSeq 3.148%, both 0.402%, cover 99.90%, enrich 31.73x
    featureBits -enrichment rheMac10 ncbiRefSeq refGene
 # ncbiRefSeq 3.148%, refGene 0.402%, both 0.402%, cover 12.76%, enrich 31.73x

    featureBits -enrichment rheMac10 ncbiRefSeqCurated refGene
 # ncbiRefSeqCurated 0.401%, refGene 0.402%, both 0.400%, cover 99.66%, enrich 247.79x

    featureBits -enrichment rheMac10 refGene ncbiRefSeqCurated
 # refGene 0.402%, ncbiRefSeqCurated 0.401%, both 0.400%, cover 99.33%, enrich 247.79x

#########################################################################
# LIFTOVER TO rheMac8 (DONE - 2019-07-02 - Hiram)
    ssh hgwdev
    mkdir /hive/data/genomes/rheMac10/bed/blat.rheMac8.2019-07-02
    cd /hive/data/genomes/rheMac10/bed/blat.rheMac8.2019-07-02
    doSameSpeciesLiftOver.pl -verbose=2 \
        -debug -bigClusterHub=ku -dbHost=hgwdev -workhorse=hgwdev \
        -ooc=/hive/data/genomes/rheMac10/jkStuff/rheMac10.11.ooc \
         rheMac10 rheMac8
    time (doSameSpeciesLiftOver.pl -verbose=2 \
        -bigClusterHub=ku -dbHost=hgwdev -workhorse=hgwdev \
        -ooc=/hive/data/genomes/rheMac10/jkStuff/rheMac10.11.ooc \
         rheMac10 rheMac8) > doLiftOverToRheMac8.log 2>&1
    # real    268m29.927s

    # see if the liftOver menus function in the browser from rheMac10 to rheMac8

#########################################################################
#  BLATSERVERS ENTRY (DONE - 2019-07-09 - 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 ("rheMac10", "blat1a", "17898", "1", "0"); \
	INSERT INTO blatServers (db, host, port, isTrans, canPcr) \
	VALUES ("rheMac10", "blat1a", "17899", "0", "1");' \
	    hgcentraltest
    #	test it with some sequence

############################################################################
## reset default position to same as rheMac8
##  (DONE - 2019-07-09 - Hiram)

    # as found from the galGal5 to rheMac10 liftOver
    ssh hgwdev
    hgsql -e 'update dbDb set defaultPos="chr3:70672926-70708108"
	where name="rheMac10";' hgcentraltest

#########################################################################
# crispr 10K shoulders (TBD - 2018-10-16 - Hiram)
    # working on this script, adding the indexFa step:
    time (~/kent/src/hg/utils/automation/doCrispr.pl \
	-stop=indexFa -buildDir=`pwd` -smallClusterHub=ku rheMac10 ncbiRefSeq) \
	> indexFa.log 2>&1
    # real    23m26.694s

    time (~/kent/src/hg/utils/automation/doCrispr.pl \
       -continue=ranges -stop=guides -buildDir=`pwd` -smallClusterHub=ku \
           rheMac10 ncbiRefSeq) > guides.log 2>&1
    # real    2m50.758s

    # adding the /dev/shm/ setup rsync for the indexed Fa
    # performed manually to work out the procedure
    time (~/kent/src/hg/utils/automation/doCrispr.pl \
        -continue=specScores -stop=specScores -buildDir=`pwd` \
           -smallClusterHub=ku rheMac10 ncbiRefSeq) > specScores.log

    # had about half of ku for about half this time:
# Completed: 884922 of 884922 jobs
# CPU time in finished jobs:  35872791s  597879.85m  9964.66h  415.19d  1.138 y
# IO & Wait Time:               899261s   14987.69m   249.79h   10.41d  0.029 y
# Average job time:                 42s       0.69m     0.01h    0.00d
# Longest finished job:             88s       1.47m     0.02h    0.00d
# Submission to last job:        48045s     800.75m    13.35h    0.56d


    time find tmp/outGuides -type f | xargs cut -f3-6 > ../specScores.tab
    # real    236m17.220s
    wc -l specScores.tab
    # 66451712 specScores.tab

    time (~/kent/src/hg/utils/automation/doCrispr.pl \
	-continue=effScores -stop=load \
	    -buildDir=`pwd` -smallClusterHub=ku rheMac10 ncbiRefSeq) \
	> load.log
    # real    307m41.143s

#########################################################################
# all.joiner update, downloads and in pushQ - (DONE - 2019-07-03,08-30 - Hiram)
xyz
    cd $HOME/kent/src/hg/makeDb/schema
    # verify all the business is done for release
    ~/kent/src/hg/utils/automation/verifyBrowser.pl rheMac10

    # fixup all.joiner until this is a clean output
    joinerCheck -database=rheMac10 -tableCoverage all.joiner
    joinerCheck -database=rheMac10 -times all.joiner
    joinerCheck -database=rheMac10 -keys all.joiner

    # when clean, check in:
    git commit -m 'adding rules for rheMac10 and regenRn0 refs #23771 #21578' all.joiner
    git push
    # run up a 'make alpha' in hg/hgTables to get this all.joiner file
    # into the hgwdev/genome-test system

    cd /hive/data/genomes/rheMac10
    time (makeDownloads.pl rheMac10) > downloads.log 2>&1
    #  real    17m47.024s

    #   now ready for pushQ entry
    mkdir /hive/data/genomes/rheMac10/pushQ
    cd /hive/data/genomes/rheMac10/pushQ
 time ($HOME/kent/src/hg/utils/automation/makePushQSql.pl -redmineList rheMac10) > rheMac10.pushQ.sql 2> stderr.out
    # real    15m52.548s

    # remove the tandemDups and gapOverlap from the file list:
    sed -i -e "/tandemDups/d" redmine.rheMac10.table.list
    sed -i -e "/Tandem Dups/d" redmine.rheMac10.releaseLog.txt
    sed -i -e "/gapOverlap/d" redmine.rheMac10.table.list
    sed -i -e "/Gap Overlaps/d" redmine.rheMac10.releaseLog.txt

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


    # verify the file list does correctly match to files
    cat redmine.rheMac10.file.list | while read L
do
  eval ls $L > /dev/null
done
    # should be silent, missing files will show as errors

    # verify database tables, how many to expect:
    wc -l redmine.rheMac10.table.list
    # 63 redmine.rheMac10.table.list

    # how many actual:
    awk -F'.' '{printf "hgsql -N %s -e '"'"'show table status like \"%s\";'"'"'\n", $1, $2}' redmine.rheMac10.table.list | sh | wc -l
    # 63

    # would be a smaller number actual if some were missing

    # add the path names to the listing files in the redmine issue
    # in the three appropriate entry boxes:

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

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