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

# This file describes browser build for the neoSch1
# Neomonachus schauinslandi - Hawaiian monk seal

# Assembly name:  ASM220157v1
# Organism name:  Neomonachus schauinslandi (Hawaiian monk seal)
# Sex:  male
# Taxid:          29088
# BioSample:      SAMN06841387
# BioProject:     PRJNA384558
# Submitter:      Johns Hopkins University
# Date:           2017-6-20
# Assembly type:  haploid
# Release type:   major
# Assembly level: Scaffold
# Genome representation: full
# WGS project:    NINY01
# Assembly method: RefAligner v. 5122; Supernova v. 1.1
# Expected final version: No
# Genome coverage: 61.0x
# Sequencing technology: Illumina HiSeq
# RefSeq category: Representative Genome
# GenBank assembly accession: GCA_002201575.1
# RefSeq assembly accession: GCF_002201575.1
# RefSeq assembly and GenBank assemblies identical: no
#
## Assembly-Units:
## GenBank Unit Accession       RefSeq Unit Accession   Assembly-Unit name
## GCA_002201765.1      GCF_002201765.1 Primary Assembly
##      GCF_000059335.1 non-nuclear

#############################################################################
# obtain photograph (DONE - 2017-09-21 - Hiram)
    mkdir -p /hive/data/genomes/neoSch1/photo
    cd /hive/data/genomes/neoSch1/photo

    wget -O photoFile "https://upload.wikimedia.org/wikipedia/commons/8/83/Monachus_schauinslandi.jpg"

    convert -sharpen 0 -normalize -geometry 400x400 -quality 80 photoFile \
       Neomonachus_schauinsland.jpg

    printf "photoCreditURL\thttps://www.linkedin.com/in/kentbackman/
photoCreditName\tKent Backman via WikiMedia Commons\n" > ../photoReference.txt

#############################################################################
# fetch sequence from new style download directory (DONE - 2017-09-21 - Hiram)
    mkdir -p /hive/data/genomes/neoSch1/refseq
    cd /hive/data/genomes/neoSch1/refseq

    time rsync -L -a -P \
rsync://ftp.ncbi.nlm.nih.gov/genomes/refseq/vertebrate_mammalian/Neomonachus_schauinslandi/all_assembly_versions/GCF_002201575.1_ASM220157v1/ ./

    # real    1m44.075s

    # sent 631 bytes  received 2674936273 bytes  25597482.33 bytes/sec
    # total size is 2674606651  speedup is 1.00

    # measure what we have here:
    faSize GCF_002201575.1_ASM220157v1_genomic.fna.gz

# 2400934290 bases (53716973 N's 2347217317 real 1565001274 upper
#	782216043 lower) in 7873 sequences in 1 files
# Total size: mean 304958.0 sd 3152511.1 min 1000 (NW_018732762.1)
#	max 84771923 (NW_018734349.1) median 1998
# %32.58 masked total, %33.33 masked real

    faCount *0_genomic.fna.gz | tail
    faCount GCF_002201575.1_ASM220157v1_genomic.fna.gz | tail
# #seq    len             A       C          G       T         N       cpg
# total 2400934290 687432582 485951339 485984591 687848805 53716973 27275876

#############################################################################
# fixup to UCSC naming scheme (DONE - 2017-09-22 - Hiram)
    mkdir /hive/data/genomes/neoSch1/ucsc
    cd /hive/data/genomes/neoSch1/ucsc

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

    twoBitDup refseq.2bit
    # should be silent output, otherwise the duplicates need to be removed
# NW_018729921.1 and NW_018729920.1 are identical

    echo "NW_018729921.1" > remove.list

    # since this is an unplaced contig assembly, verify all names are .1:
    twoBitInfo refseq.2bit  stdout | awk '{print $1}' \
	| sed -e 's/_[0-9]\+//;' | sort | uniq -c
    #       1 NC.1
    #    7872 NW.1

    # in this case, all the .1's can be changed to: v1

    # this one has unlocalized scaffolds *WITHOUT* a chrom assembly ?
    $HOME/kent/src/hg/utils/automation/unlocalizedWithChroms.pl \
        ../refseq/*_assembly_structure/Primary_Assembly
    # processed 22 sequences into chr*_random.gz 1 files

    zcat ../refseq/*1_assembly_structure/Primary_Assembly/unplaced_scaffolds/AGP/unplaced.scaf.agp.gz \
       | grep -v "^#" | grep -v `cat remove.list` | sed -e 's/\.1/v1/;' \
          > chrUn.neoSch1.agp

    zcat ../refseq/*1_assembly_structure/Primary_Assembly/unplaced_scaffolds/FASTA/unplaced.scaf.fna.gz \
       | faSomeRecords -exclude stdin remove.list stdout \
         | sed -e 's/.1 Neomonachus .*/v1/;' > chrUn.neoSch1.fa

    zcat ../refseq/*1_assembly_structure/non-nuclear/assembled_chromosomes/AGP/chrMT.comp.agp.gz \
	| grep -v "^#" | sed -e 's/^NC_[0-9.]\+/chrM/;' > chrM.agp

    zcat ../refseq/*1_assembly_structure/non-nuclear/assembled_chromosomes/FASTA/chrMT.fna.gz \
	| sed -e 's/NC_.*/chrM/;' > chrM.fa

    time gzip chr*.fa
    # real    12m14.210s

    # verify fasta and AGP match:
    time faToTwoBit chr*.fa.gz test.2bit
    # real    1m4.793s

    # verify still silent:
    twoBitDup test.2bit

    # and check AGP vs. fasta correspondence:
    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

# 2400839308 bases (53716773 N's 2347122535 real 2347122535 upper 0 lower)
#	in 7872 sequences in 1 files
# Total size: mean 304984.7 sd 3152710.5 min 1000 (NW_018732762v1)
#	max 84771923 (NW_018734349v1) median 1998
# %0.00 masked total, %0.00 masked real

# real    0m41.314s

    # the original has the one extra duplicate contig that was removed:
# 2400934290 bases (53716973 N's 2347217317 real 1565001274 upper
#	782216043 lower) in 7873 sequences in 1 files

    # the missing contig size:

    calc 2400934290 - 2400839308
2400934290 - 2400839308 = 94982.000000

    twoBitInfo test.2bit stdout | grep NW_018729920
# NW_018729920v1  94982

#############################################################################
#  Initial database build (DONE - 2017-09-22 - Hiram)

    cd /hive/data/genomes/neoSch1

    # establish the config.ra file:
    ~/kent/src/hg/utils/automation/prepConfig.pl neoSch1 mammal seal \
       refseq/*_assembly_report.txt > neoSch1.config.ra

    # verify this looks OK:

    cat neoSch1.config.ra

# config parameters for makeGenomeDb.pl:
db neoSch1
clade mammal
genomeCladePriority 35
scientificName Neomonachus schauinslandi
commonName Hawaiian monk seal
assemblyDate Jun. 2017
assemblyLabel Johns Hopkins University
assemblyShortLabel ASM220157v1
orderKey 8055
# mitochondrial sequence included in refseq release
# mitoAcc NC_008421.1
mitoAcc none
fastaFiles /hive/data/genomes/neoSch1/ucsc/*.fa.gz
agpFiles /hive/data/genomes/neoSch1/ucsc/*.agp
# qualFiles none
dbDbSpeciesDir seal
photoCreditURL  https://www.linkedin.com/in/kentbackman/
photoCreditName Kent Backman via WikiMedia Commons
ncbiGenomeId 8042
ncbiAssemblyId 1137021
ncbiAssemblyName ASM220157v1
ncbiBioProject 384558
ncbiBioSample SAMN06841387
genBankAccessionID GCF_002201575.1
taxId 29088

    # verify sequence and AGP are OK:
    time (makeGenomeDb.pl -workhorse=hgwdev -dbHost=hgwdev -fileServer=hgwdev \
         -stop=agp neoSch1.config.ra) > agp.log 2>&1
    # real    2m9.177s

    # verify it ran OK:
    #   *** All done!  (through the 'agp' step)

    # then finish it off:
    time (makeGenomeDb.pl -workhorse=hgwdev \
       -dbHost=hgwdev -fileServer=hgwdev -continue=db \
           neoSch1.config.ra ) > db.log 2>&1
    # real    18m51.114s

    # incorrect name on the photo file, continuing:
    time (makeGenomeDb.pl -workhorse=hgwdev \
       -dbHost=hgwdev -fileServer=hgwdev -continue=trackDb \
           neoSch1.config.ra ) > trackDb.log 2>&1
    # real    0m7.484s

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

#############################################################################
# cytoBandIdeo - (DONE - 2017-09-22 - Hiram)
    mkdir /hive/data/genomes/neoSch1/bed/cytoBand
    cd /hive/data/genomes/neoSch1/bed/cytoBand
    makeCytoBandIdeo.csh neoSch1

##############################################################################
# cpgIslands on UNMASKED sequence (DONE - 2017-09-22 - Hiram)
    mkdir /hive/data/genomes/neoSch1/bed/cpgIslandsUnmasked
    cd /hive/data/genomes/neoSch1/bed/cpgIslandsUnmasked

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

    cat fb.neoSch1.cpgIslandExtUnmasked.txt
    # 34820932 bases of 2400839308 (1.450%) in intersection

#############################################################################
# running repeat masker (DONE - 2017-09-22 - Hiram)
    mkdir /hive/data/genomes/neoSch1/bed/repeatMasker
    cd /hive/data/genomes/neoSch1/bed/repeatMasker
    time  (doRepeatMasker.pl -buildDir=`pwd` \
        -bigClusterHub=ku -dbHost=hgwdev -workhorse=hgwdev \
        -smallClusterHub=ku neoSch1) > do.log 2>&1 &
    # RepeatMasker does not recognize the name: Neomonachus schauinslandi

    # try using the "Phocidae" name, the 'true seals' taxon:
    time  (doRepeatMasker.pl -buildDir=`pwd` -species="Phocidae" \
        -bigClusterHub=ku -dbHost=hgwdev -workhorse=hgwdev \
        -smallClusterHub=ku neoSch1) > do.log 2>&1 &
    # real    255m45.612s

    cat faSize.rmsk.txt
# 2400839308 bases (53716773 N's 2347122535 real 1331243472 upper
#	1015879063 lower) in 7872 sequences in 1 files
# Total size: mean 304984.7 sd 3152710.5 min 1000 (NW_018732762v1)
#	max 84771923 (NW_018734349v1) median 1998
# %42.31 masked total, %43.28 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 neoSch1 rmsk
    # 1016100984 bases of 2400839308 (42.323%) in intersection
    # real    0m44.536s

    # 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 on high contig count assemblies::
    time hgsql -N -e 'select genoName,genoStart,genoEnd from rmsk;' neoSch1 \
        | bedSingleCover.pl stdin | ave -col=4 stdin | grep "^total"
    # total 1016100984.000000
    # real    0m36.665s

##########################################################################
# running simple repeat (DONE - 2017-09-22 - Hiram)

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

    cat fb.simpleRepeat
    # 26133283 bases of 2400839308 (1.089%) in intersection

    # add to this simple repeat to rmsk:
    cd /hive/data/genomes/neoSch1
    twoBitMask neoSch1.rmsk.2bit \
        -add bed/simpleRepeat/trfMask.bed neoSch1.2bit
    #   you can safely ignore the warning about fields >= 13
    twoBitToFa neoSch1.2bit stdout | faSize stdin > faSize.neoSch1.2bit.txt
    cat faSize.neoSch1.2bit.txt
# 2400839308 bases (53716773 N's 2347122535 real 1330493216 upper
#	1016629319 lower) in 7872 sequences in 1 files
# Total size: mean 304984.7 sd 3152710.5 min 1000 (NW_018732762v1)
#	max 84771923 (NW_018734349v1) median 1998
# %42.34 masked total, %43.31 masked real

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

#############################################################################
# CREATE MICROSAT TRACK (DONE - 2017-09-22 - Hiram)
    ssh hgwdev
    mkdir /cluster/data/neoSch1/bed/microsat
    cd /cluster/data/neoSch1/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 neoSch1 microsat microsat.bed
    # Read 23191 elements of size 4 from microsat.bed

#############################################################################
# ucscToINSDC table/track (DONE - 2017-09-22 - 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

    # refseq assembly came out after we ran this up.  Compute it ids:
    mkdir /hive/data/genomes/neoSch1/refseq
    cd /hive/data/genomes/neoSch1/refseq
    time rsync -L -a -P \
rsync://ftp.ncbi.nlm.nih.gov/genomes/refseq/vertebrate_mammalian/Neomonachus_schauinslandi/all_assembly_versions/GCF_002201575.1_ASM220157v1/ ./
   # sent 631 bytes  received 2674936273 bytes  25597482.33 bytes/sec
   # total size is 2674606651  speedup is 1.00
   # real    1m44.075s

    mkdir /hive/data/genomes/neoSch1/refseq/idKeys
    cd /hive/data/genomes/neoSch1/refseq/idKeys

    faToTwoBit ../GCF_002201575.1_ASM220157v1_genomic.fna.gz refseq.neoSch1.2bit
    time (doIdKeys.pl -twoBit=`pwd`/refseq.neoSch1.2bit -buildDir=`pwd` \
	refseqNeoSch1) > do.log 2>&1
    # real    12m15.428s
    cat refseqNeoSch1.keySignature.txt
    # 693347c3f7f03f2fde19c607fe7bb4ad

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

    # find accession for chrM
    grep chrM ../../neoSch1.agp
# chrM    1       16754   1       O       NC_008421.1     1       16754   +

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

    # remove the duplicate contig name:
    grep -v NW_018729921 ucscToINSDC.txt | sort -k2,2 > ucscToRefSeq.txt
    rm -f ucscToINSDC.txt

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

    # again, remove the duplicate contig name, and substitute
    #  the INSDC name for the mito sequence
    grep -v "^#" ../../refseq/GCF*_assembly_report.txt | cut -f5,7 \
      | grep -v NW_018729921 | sed -e 's/na\b/AM181022.1/;' \
        | awk '{printf "%s\t%s\n", $2, $1}' \
           | sort > refseq.insdc.txt
    # the sed \b means 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       16754   AM181022.1
ucscToRefSeq.bed:chrM   0       16754   NC_008421.1

    # should be same line counts throughout:
    wc -l *
    #	7872 name.coordinate.tab
    #	7872 refseq.insdc.txt
    #	7872 refseqToUcsc.txt
    #	7872 ucscToINSDC.bed
    #	7872 ucscToRefSeq.bed
    #	7872 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 neoSch1 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 neoSch1 ucscToRefSeq stdin ucscToRefSeq.bed
    checkTableCoords neoSch1
    # should cover %100 entirely:
    featureBits -countGaps neoSch1 ucscToINSDC
    # 2400839308 bases of 2400839308 (100.000%) in intersection
    featureBits -countGaps neoSch1 ucscToRefSeq
    # 2400839308 bases of 2400839308 (100.000%) in intersection

##########################################################################
# run up idKeys files for chromAlias (DONE - 2018-03-15 - Hiram)
    mkdir /hive/data/genomes/neoSch1/bed/idKeys
    cd /hive/data/genomes/neoSch1/bed/idKeys

    time (doIdKeys.pl -twoBit=/hive/data/genomes/neoSch1/neoSch1.unmasked.2bit -buildDir=`pwd` neoSch1) > do.log 2>&1 &
    # real    12m36.636s

    cat neoSch1.keySignature.txt
    #   cbeabb8faa38538267584374b47d8f3b

##########################################################################
# add chromAlias table (DONE - 2017-09-22 - Hiram)

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

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

    # verify chrM is correct:
    grep chrM *
# ucsc.genbank.tab:chrM   AM181022.1      genbank
# ucsc.refseq.tab:chrM    NC_008421.1     refseq

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

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

#########################################################################
# fixup search rule for assembly track/gold table (DONE - 2020-09-11 - Hiram)

    cd ~/kent/src/hg/makeDb/trackDb/seal/neoSch1
    # preview prefixes and suffixes:
    hgsql -N -e "select frag from gold;" neoSch1 | less
      
# Manually examine patterns in the new fakeAgp names:
NW_[0-9]+(v[0-9]+[0-9_]*)?
chrM
chrX_NW_[0-9]+(v[0-9]+_random[0-9_]*)?

    # implies a search rule of:

    '[chrMNWX_]+([0-9]+)?(v[0-9]+)?(_random)?([0-9_]*)?'

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

    hgsql -N -e "select frag from gold;" neoSch1 \
       | egrep -e '[chrMNWX_]+([0-9]+)?(v[0-9]+)?(_random)?([0-9_]*)?' | wc -l
    # 46705

    hgsql -N -e "select frag from gold;" neoSch1 \
     | egrep -v -e '[chrMNWX_]+([0-9]+)?(v[0-9]+)?(_random)?([0-9_]*)?' | wc -l
    # 0

    # hence, add to trackDb/rhesus/neoSch1/trackDb.ra
searchTable gold
shortCircuit 1
termRegex [chrMNWX_]+([0-9]+)?(v[0-9]+)?(_random)?([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 #19883' trackDb.ra

    # verify searches work in the position box

##########################################################################
## WINDOWMASKER (DONE - 2017-09-22 - Hiram)

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

    # Masking statistics
    cat faSize.neoSch1.cleanWMSdust.txt
# 2400839308 bases (53716773 N's 2347122535 real 1552520352 upper
#	794602183 lower) in 7872 sequences in 1 files
# Total size: mean 304984.7 sd 3152710.5 min 1000 (NW_018732762v1)
#	max 84771923 (NW_018734349v1) median 1998
# %33.10 masked total, %33.85 masked real

    cat fb.neoSch1.rmsk.windowmaskerSdust.txt
    # 545954348 bases of 2400839308 (22.740%) in intersection

##########################################################################
# cpgIslands - (DONE - 2017-09-25 - Hiram)
    mkdir /hive/data/genomes/neoSch1/bed/cpgIslands
    cd /hive/data/genomes/neoSch1/bed/cpgIslands
    time (doCpgIslands.pl -dbHost=hgwdev -bigClusterHub=ku \
      -workhorse=hgwdev -smallClusterHub=ku neoSch1) > do.log 2>&1
    # real    4m51.761s

    cat fb.neoSch1.cpgIslandExt.txt
    # 30655113 bases of 2400839308 (1.277%) in intersection

##############################################################################
# ncbiRefSeq gene track (DONE - 2018-03-15 - Hiram)
    mkdir /hive/data/genomes/neoSch1/bed/ncbiRefSeq
    cd /hive/data/genomes/neoSch1/bed/ncbiRefSeq

    time (~/kent/src/hg/utils/automation/doNcbiRefSeq.pl \
  -buildDir=`pwd` -bigClusterHub=ku \
  -fileServer=hgwdev -workhorse=hgwdev -smallClusterHub=ku -dbHost=hgwdev \
      refseq vertebrate_mammalian Neomonachus_schauinslandi \
         GCF_002201575.1_ASM220157v1 neoSch1) > do.log 2>&1
    # real    4m37.395s

    cat fb.ncbiRefSeq.neoSch1.txt
    # 45781082 bases of 2400839308 (1.907%) in intersection

##############################################################################
# genscan - (DONE - 2017-09-25 - Hiram)
    mkdir /hive/data/genomes/neoSch1/bed/genscan
    cd /hive/data/genomes/neoSch1/bed/genscan
    time (doGenscan.pl -buildDir=`pwd` -workhorse=hgwdev -dbHost=hgwdev \
      -bigClusterHub=ku neoSch1) > do.log 2>&1
    # five were broken at window size of 2400000, they all worked at 2000000
    # real    18m8.224s

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

    cat fb.neoSch1.genscan.txt
    #   53956919 bases of 2400839308 (2.247%) in intersection

    cat fb.neoSch1.genscanSubopt.txt
    #   50754244 bases of 2400839308 (2.114%) in intersection

#############################################################################
# augustus gene track (DONE - 2017-09-25 - Hiram)

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

    cat fb.neoSch1.augustusGene.txt
    # 47279177 bases of 2400839308 (1.969%) in intersection

#############################################################################
# Create kluster run files (DONE - 2017-09-25 - Hiram)

    # obtain 'real' numbers of this assembly:
    cd /hive/data/genomes/neoSch1
    head -1 faSize.neoSch1.2bit.txt
# 2400839308 bases (53716773 N's 2347122535 real 1330493216 upper
#	1016629319 lower) in 7872 sequences in 1 files

    # numerator is neoSch1 gapless bases "real"

    # 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 \( 2347122535 / 2861349177 \) \* 1024
    # ( 2347122535 / 2861349177 ) * 1024 = 839.972100

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

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

#########################################################################
# lastz/chain/net swap from hg38 (DONE - 2018-03-16 - Hiram)
    # alignment to hg38

    cd /hive/data/genomes/hg38/bed/lastzNeoSch1.2017-09-25

    cat fb.hg38.chainNeoSch1Link.txt
    # 1586260720 bases of 3049335806 (52.020%) in intersection
    cat fb.hg38.chainSynNeoSch1Link.txt
    # 1515015853 bases of 3049335806 (49.683%) in intersection
    cat fb.hg38.chainRBestNeoSch1Link.txt
    # 1455520429 bases of 3049335806 (47.732%) in intersection

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

    time (doBlastzChainNet.pl -verbose=2 \
      /hive/data/genomes/hg38/bed/lastzNeoSch1.2017-09-25/DEF \
        -swap -chainMinScore=3000 -chainLinearGap=medium \
          -workhorse=hgwdev -smallClusterHub=ku -bigClusterHub=ku \
            -syntenicNet) > swap.log 2>&1
    #  real    108m21.523s

    cat fb.neoSch1.chainHg38Link.txt
    # 1483929997 bases of 2400839308 (61.809%) in intersection
    cat fb.neoSch1.chainSynHg38Link.txt
    # 1446564285 bases of 2400839308 (60.252%) in intersection

    time (doRecipBest.pl -load -workhorse=hgwdev -buildDir=`pwd` neoSch1 hg38) \
        > rbest.log 2>&1
    # real    404m14.530s

    cat fb.neoSch1.chainRBestHg38Link.txt
    # 1455431042 bases of 2400839308 (60.622%) in intersection

#########################################################################
# lastz/chain/net swap from mm10 (DONE - 2018-03-16 - Hiram)
    # alignment on mm10

    cd /hive/data/genomes/mm10/bed/lastzNeoSch1.2017-09-25
    cat fb.mm10.chainNeoSch1Link.txt
    #	827926012 bases of 2652783500 (31.210%) in intersection
    cat fb.mm10.chainRBestNeoSch1Link.txt
    #   788489846 bases of 2652783500 (29.723%) in intersection

    mkdir /hive/data/genomes/neoSch1/bed/blastz.mm10.swap
    cd /hive/data/genomes/neoSch1/bed/blastz.mm10.swap
    time (doBlastzChainNet.pl -verbose=2 \
	/hive/data/genomes/mm10/bed/lastzNeoSch1.2017-09-25/DEF \
	-swap -syntenicNet \
	-workhorse=hgwdev -smallClusterHub=ku -bigClusterHub=ku \
	-chainMinScore=3000 -chainLinearGap=medium) > swap.log 2>&1
    #	real    59m27.809s

    cat fb.neoSch1.chainMm10Link.txt
    #	804021579 bases of 2400839308 (33.489%) in intersection
    cat fb.neoSch1.chainSynMm10Link.txt
    #   776155245 bases of 2400839308 (32.328%) in intersection

    time (doRecipBest.pl -load -workhorse=hgwdev neoSch1 mm10 \
      -buildDir=`pwd` -workhorse=hgwdev) > rbest.log 2>&1 &
    # real    242m31.157s

    cat fb.neoSch1.chainRBestMm10Link.txt
    # 787537751 bases of 2400839308 (32.803%) in intersection

##############################################################################
# GENBANK AUTO UPDATE (DONE - 2017-09-25 - Hiram)
    ssh hgwdev
    cd $HOME/kent/src/hg/makeDb/genbank
    git pull
    # /cluster/data/genbank/data/organism.lst shows:
    # organism       mrnaCnt estCnt  refSeqCnt
    # Neomonachus schauinslandi   (there are no entries for this name)

    # edit etc/genbank.conf to add neoSch1 just after manLeu1

# neoSch1 (Hawaiian monk seal - Neomonachus schauinslandi) taxId 29088
neoSch1.serverGenome = /hive/data/genomes/neoSch1/neoSch1.2bit
neoSch1.clusterGenome = /hive/data/genomes/neoSch1/neoSch1.2bit
neoSch1.ooc = /hive/data/genomes/neoSch1/jkStuff/neoSch1.11.ooc
neoSch1.lift = no
neoSch1.refseq.mrna.native.pslCDnaFilter  = ${lowCover.refseq.mrna.native.pslCDnaFilter}
neoSch1.refseq.mrna.xeno.pslCDnaFilter    = ${lowCover.refseq.mrna.xeno.pslCDnaFilter}
neoSch1.genbank.mrna.native.pslCDnaFilter = ${lowCover.genbank.mrna.native.pslCDnaFilter}
neoSch1.genbank.mrna.xeno.pslCDnaFilter   = ${lowCover.genbank.mrna.xeno.pslCDnaFilter}
neoSch1.genbank.est.native.pslCDnaFilter  = ${lowCover.genbank.est.native.pslCDnaFilter}
neoSch1.genbank.est.xeno.pslCDnaFilter    = ${lowCover.genbank.est.xeno.pslCDnaFilter}
neoSch1.downloadDir = neoSch1
neoSch1.refseq.mrna.native.load  = no
neoSch1.refseq.mrna.xeno.load = yes
# DO NOT NEED genbank.mrna.xeno except for human, mouse
neoSch1.genbank.mrna.xeno.load = no
neoSch1.genbank.mrna.native.load = no
neoSch1.genbank.est.native.load = no
neoSch1.perChromTables = no

# And edit src/lib/gbGenome.c to add new species
#	static char *neoSchNames[] = {"Neomonachus schauinslandi", NULL};
#	    {"neoSch", neoSchNames},

    git commit -m \
  "Added neoSch1 Hawaiian monk seal/Neomonachus schauinslandi; refs #19883" \
       etc/genbank.conf src/lib/gbGenome.c
    git push
    # update /cluster/data/genbank/:
    make etc-update
    make install-server

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

    time ./bin/gbAlignStep -initial neoSch1
    #  logFile: var/build/logs/2017.09.25-10:31:15.neoSch1.initalign.log
    #   real    112m27.766s


    tail -2 var/build/logs/2017.09.25-10:31:15.neoSch1.initalign.log
# hgwdev 2017.09.25-12:22:10 neoSch1.initalign: Succeeded: neoSch1
# hgwdev 2017.09.25-12:23:43 neoSch1.initalign: finish

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

    # load database when finished
    ssh hgwdev
    cd /cluster/data/genbank
    time ./bin/gbDbLoadStep -drop -initialLoad neoSch1
    # logFile: var/dbload/hgwdev/logs/2017.09.25-13:09:18.neoSch1.dbload.log
    # real    5m1.448s

    tail -1 var/dbload/hgwdev/logs/2017.09.25-13:09:18.neoSch1.dbload.log
    # hgwdev 2017.09.25-13:14:20 neoSch1.dbload: finish

    # enable daily alignment and update of hgwdev
    cd ~/kent/src/hg/makeDb/genbank
    git pull
    # add neoSch1 to:
    #   etc/align.dbs etc/hgwdev.dbs
    git add etc/align.dbs etc/hgwdev.dbs
    git commit -m \
"Added neoSch1 - Hawaiian monk seal - Neomonachus schauinslandi refs #19883" \
etc/align.dbs etc/hgwdev.dbs
    git push
    make etc-update

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

##############################################################################
# set default position to similar location as hg38 (DONE - 2017-10-06 - Hiram)
    hgsql -e \
'update dbDb set defaultPos="NW_018734295v1:8131879-8151536" where name="neoSch1";' \
	hgcentraltest

##############################################################################
# all.joiner update, downloads and in pushQ - (DONE - 2018-03-22 - Hiram)
    cd $HOME/kent/src/hg/makeDb/schema

    ~/kent/src/hg/utils/automation/verifyBrowser.pl neoSch1
# 61 tables in database neoSch1 - Hawaiian monk seal, Neomonachus schauinslandi
# verified 58 tables in database neoSch1, 3 extra tables, 24 optional tables
# NCBI RefSeq genes     10 optional tables
# chainNetRBestHg38     3 optional tables
# chainNetRBestMm10     3 optional tables
# chainNetSynHg38       3 optional tables
# chainNetSynMm10       3 optional tables
# gapOverlap    1 optional tables
# tandemDups    1 optional tables
# 1     chainLepWed1    - extra table
# 2     chainLepWed1Link        - extra table
# 3     netLepWed1      - extra table
# 5 genbank tables found
# verified 29 required tables, 0 missing tables

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

    cd /hive/data/genomes/neoSch1
    time (makeDownloads.pl neoSch1) > downloads.log 2>&1
    # real    16m28.321s

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

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

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

    # verify the file listings are valid, should be no output to stderr:
    cat redmine.neoSch1.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.neoSch1.table.list
    # 53 redmine.neoSch1.table.list
    awk -F'.' '{
printf "hgsql -N -e \"show table status like '"'"'%s'"'"';\" %s\n", $2, $1
}' redmine.neoSch1.table.list | while read L; do eval $L; done | wc -l
    # 53

    # add the path names to the listing files in the redmine issue
    # in the three appropriate entry boxes:
    ls `pwd`/redmine*

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

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