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

# This file describes browser build for the ficAlb1

#########################################################################
# reuse photograph obtained for ficAlb2 experiment
#    (DONE - 2018-01-04 - Hiram)

mkdir /hive/data/genomes/ficAlb1
cd /hive/data/genomes/ficAlb1
cp -p ../ficAlb2/photoReference.txt .

cat -A photoReference.txt

photoCreditURL^Ihttps://commons.wikimedia.org/wiki/File:Collared_flycatcher_(Ficedula_albicollis).jpg$
photoCreditName^IWikiMedia Commons: Andrej Chud&yacute;$

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

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

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

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

mkdir /hive/data/genomes/ficAlb1/refseq
cd /hive/data/genomes/ficAlb1/refseq

time rsync -L -a -P \
rsync://ftp.ncbi.nlm.nih.gov/genomes/refseq/vertebrate_other/Ficedula_albicollis/all_assembly_versions/GCF_000247815.1_FicAlb1.5/ ./

# sent 4417 bytes  received 1507883526 bytes  14429549.69 bytes/sec
# total size is 1507683242  speedup is 1.00
# real    1m44.830s

# check assembly size for later reference:

faSize G*5_genomic.fna.gz
# 1118343587 bases (16019705 N's 1102323882 real 846711548 upper
#	255612334 lower) in 21428 sequences in 1 files
# Total size: mean 52190.8 sd 1925906.4 min 200 (NW_004797270.1)
#	max 157563209 (NC_021673.1) median 460
# %22.86 masked total, %23.19 masked real

# this information is from the top of
#    ficAlb1/refseq/GCF_000247815.1_FicAlb1.5_assembly_report.txt

# Assembly name:  FicAlb1.5
# Organism name:  Ficedula albicollis (collared flycatcher)
# Isolate:  OC2
# Sex:  male
# Taxid:          59894
# BioSample:      SAMN02981387
# BioProject:     PRJNA75089
# Submitter:      Uppsala University
# Date:           2013-6-27
# Assembly type:  haploid
# Release type:   major
# Assembly level: Chromosome
# Genome representation: full
# WGS project:    AGTO02
# Assembly method: SOAPdenovo v. 1.05
# Expected final version: no
# Reference guided assembly: de-novo
# Genome coverage: 60.0x
# Sequencing technology: Illumina GAII; Illumina HiSeq
# RefSeq category: Representative Genome
# GenBank assembly accession: GCA_000247815.2
# RefSeq assembly accession: GCF_000247815.1
# RefSeq assembly and GenBank assemblies identical: yes
#
## Assembly-Units:
## GenBank Unit Accession       RefSeq Unit Accession   Assembly-Unit name
## GCA_000247825.2      GCF_000247825.1 Primary Assembly
## GCA_000416335.1      GCF_000416335.1 non-nuclear

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

    # verify it looks sane
    cat ficAlb1.config.ra
# config parameters for makeGenomeDb.pl:
db ficAlb1
clade vertebrate
scientificName Ficedula albicollis
commonName Collared flycatcher
assemblyDate Jun. 2013
assemblyLabel Uppsala University
assemblyShortLabel FicAlb1.5
orderKey 3614
# mitochondrial sequence included in refseq release
# mitoAcc NC_021621.1
mitoAcc none
fastaFiles /hive/data/genomes/ficAlb1/ucsc/*.fa.gz
agpFiles /hive/data/genomes/ficAlb1/ucsc/*.agp
# qualFiles none
dbDbSpeciesDir birds
photoCreditURL  https://commons.wikimedia.org/wiki/File:Collared_flycatcher_(Ficedula_albicollis).jpg
photoCreditName WikiMedia Commons: Andrej Chud&yacute;
ncbiGenomeId 11872
ncbiAssemblyId 37801
ncbiAssemblyName FicAlb1.5
ncbiBioProject 75089
ncbiBioSample SAMN02981387
genBankAccessionID GCF_000247815.1
taxId 59894

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

    mkdir /hive/data/genomes/ficAlb1/ucsc
    cd /hive/data/genomes/ficAlb1/ucsc

    # check for duplicate sequences:
    time faToTwoBit -noMask ../refseq/G*5_genomic.fna.gz refseq.2bit
    #  real    1m15.460s

    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

    time ~/kent/src/hg/utils/automation/ucscCompositeAgp.pl \
       ../refseq/G*5_genomic.fna.gz \
          ../refseq/G*5_assembly_structure/Primary_Assembly
    # real    5m45.505s
NC_021671.1 chr1
NC_021672.1 chr1A
NC_021673.1 chr2
NC_021674.1 chr3
NC_021675.1 chr4
NC_021676.1 chr4A
NC_021677.1 chr5
NC_021678.1 chr6
NC_021679.1 chr7
NC_021680.1 chr8
NC_021681.1 chr9
NC_021682.1 chr10
NC_021683.1 chr11
NC_021684.1 chr12
NC_021685.1 chr13
NC_021686.1 chr14
NC_021687.1 chr15
NC_021688.1 chr17
NC_021689.1 chr18
NC_021690.1 chr19
NC_021691.1 chr20
NC_021692.1 chr21
NC_021693.1 chr22
NC_021694.1 chr23
NC_021695.1 chr24
NC_021696.1 chr25
NC_021697.1 chr26
NC_021698.1 chr27
NC_021699.1 chr28
NC_021700.1 chrZ
NC_021701.1 chrLG34
NC_021702.1 chrLG35
NC_021703.1 chrLGE22

    time ~/kent/src/hg/utils/automation/unplacedWithChroms.pl \
       ../refseq/*_assembly_structure/Primary_Assembly
    # processed 21350 sequences into chrUn.fa.gz
    # real    4m8.699s

    # unlocalized sequences
    time ~/kent/src/hg/utils/automation/unlocalizedWithChroms.pl \
       ../refseq/*_assembly_structure/Primary_Assembly
    # real    0m5.031s
# 21
# LG34
# 26
# 2
# Z
# 22
# 1
# 1A
# 18
# 4A
# 25
# 27
# 28
# LG35
# 4
# LGE22
# processed 44 sequences into chr*_random.gz 16 files

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

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

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

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

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


    # and no sequence lost from orginal:
    twoBitToFa test.2bit stdout | faSize stdin
# 1118343587 bases (16019705 N's 1102323882 real 1102323882 upper 0 lower)
#	in 21428 sequences in 1 files
# Total size: mean 52190.8 sd 1925906.4 min 200 (chrUn_NW_004797270v1)
#	max 157563209 (chr2) median 460

    # same numbers as above
# 1118343587 bases (16019705 N's 1102323882 real 846711548 upper
#	255612334 lower) in 21428 sequences in 1 files

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

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

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

    # then finish it off:
    time (makeGenomeDb.pl -workhorse=hgwdev -dbHost=hgwdev \
       -fileServer=hgwdev -continue=db ficAlb1.config.ra) > db.log 2>&1
    # real    9m14.621s

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

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

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

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

    cat fb.ficAlb1.cpgIslandExtUnmasked.txt
    # 16741585 bases of 1102325870 (1.519%) in intersection

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

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

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

    cat ficAlb1.keySignature.txt
    #   ed755df88638d99bfa2de433f6f975d3

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

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

    # if there is a chrM, use its INSDC name as a second argument:
     grep chrM ../../*.agp
#    chrM    1       16787   1       O       NC_021621.1     1       16787   +


    # this is a RefSeq assembly, use the chrM refSeq name:
    ~/kent/src/hg/utils/automation/ucscToINSDC.sh \
      ../../refseq/GCF_*structure/Primary_Assembly NC_021621.1

    # this is actually ucscToRefSeq since this is a RefSeq assembly
    sort  ucscToINSDC.txt > ucscToRefSeq.txt

    rm -f ucscToINSDC.txt
    # there is also a genbank release, need to make idKeys to match it
    mkdir /hive/data/genomes/ficAlb1/bed/ucscToINSDC/genbank
    cd /hive/data/genomes/ficAlb1/bed/ucscToINSDC/genbank
    ln -s /hive/data/outside/ncbi/genomes/genbank/vertebrate_other/Ficedula_albicollis/all_assembly_versions/GCA_000247815.2_FicAlb1.5/GCA_000247815.2_FicAlb1.5_genomic.fna.gz .

    faToTwoBit G*.fna.gz genbank.ficAlb1.2bit

    time (doIdKeys.pl -buildDir=`pwd` \
	-twoBit=`pwd`/genbank.ficAlb1.2bit genbankFicAlb1) > do.log 2>&1 &
    # real    21m44.755s

    cd /hive/data/genomes/ficAlb1/bed/ucscToINSDC
    join -t$'\t' \
      ../idKeys/ficAlb1.idKeys.txt genbank/genbankFicAlb1.idKeys.txt \
	| cut -f2- | sort > ucscToINSDC.txt

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

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

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

    # should be same line counts throughout:
    wc -l *
    #	21428 refSeqToUcsc.txt
    #	21428 ucsc.coordinate.tab
    #	21428 ucscToINSDC.bed
    #	21428 ucscToINSDC.txt
    #	21428 ucscToRefSeq.bed
    #	21428 ucscToRefSeq.txt

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

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

    featureBits -countGaps ficAlb1 ucscToRefSeq
    # 1118343587 bases of 1118343587 (100.000%) in intersection

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

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

    # after ensembl idKeys have been made:
    join -t$'\t'  ../idKeys/ficAlb1.idKeys.txt \
	../../ensembl/ensemblFicAlb1.idKeys.txt | cut -f2- > ucsc.ensembl.tab

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

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

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

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

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

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

    # implies a rule: '[AN][GC][TO0-9_]+(\.[0-9]+)?'

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

    hgsql -N -e "select frag from gold;" ficAlb1 \
       | egrep -e '[AN][GC][TO0-9_]+(\.[0-9]+)?' | wc -l
    # 30843

    hgsql -N -e "select frag from gold;" ficAlb1 \
       | egrep -v -e '[AN][GC][TO0-9_]+(\.[0-9]+)?' | wc -l
    # 0

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

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

##########################################################################
# running repeat masker (DONE - 2018-01-04 - Hiram)
    mkdir /hive/data/genomes/ficAlb1/bed/repeatMasker
    cd /hive/data/genomes/ficAlb1/bed/repeatMasker
    time  (doRepeatMasker.pl -buildDir=`pwd` \
        -bigClusterHub=ku -dbHost=hgwdev -workhorse=hgwdev \
        -smallClusterHub=ku ficAlb1) > do.log 2>&1 &
    # real    358m9.085s

    egrep "bases|Total|masked" faSize.rmsk.txt \
	| fold -w 75 -s  | sed -e 's/^/# /;'
# 1118343587 bases (16019705 N's 1102323882 real 989514048 upper 112809834
# lower) in 21428 sequences in 1 files
# Total size: mean 52190.8 sd 1925906.4 min 200 (chrUn_NW_004797270v1) max
# 157563209 (chr2) median 460
# %10.09 masked total, %10.23 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 ficAlb1 rmsk
    # 112813145 bases of 1118343587 (10.088%) in intersection
    # real    0m11.238s

    # 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;' ficAlb1 \
        | bedSingleCover.pl stdin | ave -col=4 stdin | grep "^total"
    #	total 112813145.000000
    #	real    0m4.053s

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

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

    cat fb.simpleRepeat
    # 127427902 bases of 1102325870 (11.560%) in intersection

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

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

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

    twoBitToFa ficAlb1.2bit stdout | faSize stdin > faSize.ficAlb1.2bit.txt
    cat faSize.ficAlb1.2bit.txt
    egrep "bases|Total|masked" faSize.ficAlb1.2bit.txt \
	| fold -w 78 -s  | sed -e 's/^/# /;'
# 1118343587 bases (16019705 N's 1102323882 real 837369763 upper 264954119
# lower) in 21428 sequences in 1 files
# Total size: mean 52190.8 sd 1925906.4 min 200 (chrUn_NW_004797270v1) max
# 157563209 (chr2) median 460
# %23.69 masked total, %24.04 masked real

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

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

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

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

    # Masking statistics
    cat faSize.ficAlb1.cleanWMSdust.txt
    egrep "bases|Total|masked" faSize.ficAlb1.cleanWMSdust.txt \
	| fold -w 78 -s  | sed -e 's/^/# /;'
# 1118343587 bases (16019705 N's 1102323882 real 838249378 upper 264074504
# lower) in 21428 sequences in 1 files
# Total size: mean 52190.8 sd 1925906.4 min 200 (chrUn_NW_004797270v1) max
# 157563209 (chr2) median 460
# %23.61 masked total, %23.96 masked real

    cat fb.ficAlb1.rmsk.windowmaskerSdust.txt
    # 542455834 bases of 2236368823 (24.256%) in intersection

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

    cat fb.ficAlb1.cpgIslandExt.txt
    # 10735328 bases of 1102325870 (0.974%) in intersection

##############################################################################
# genscan - (DONE - 2018-01-05 - Hiram)
    mkdir /hive/data/genomes/ficAlb1/bed/genscan
    cd /hive/data/genomes/ficAlb1/bed/genscan
    time (doGenscan.pl -buildDir=`pwd` -workhorse=hgwdev -dbHost=hgwdev \
      -bigClusterHub=ku ficAlb1) > do.log 2>&1 &
    # real    22m10.156s
    # 2 jobs failed, running with 2000000 size window
    # real    3m24.459s

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

    cat fb.ficAlb1.genscan.txt
    # 19263301 bases of 1102325870 (1.748%) in intersection

    cat fb.ficAlb1.genscanSubopt.txt
    # 22956236 bases of 1102325870 (2.083%) in intersection

#############################################################################
# Ensembl genes (TBD - 2018-01-05 - Hiram)
    # the v91 release for Ensembl is for a previous assembly from this one
    # does not work on this assembly, the Ensembl genome is an older
    # obsolete assembly

    # after chromAlias work is done:

    cd /hive/data/genomes/ficAlb1/jkStuff
    join -t$'\t' <(sort -k1,1 ../chrom.sizes) \
      <(sort ../bed/chromAlias/ucsc.ensembl.tab) \
        | awk '{printf "0\t%s\t%d\t%s\t%d\n", $3,$2,$1,$2}' > ensToUcsc.lift

    cd /hive/data/genomes/ficAlb1
    printf "# required db variable
db ficAlb1
# specific lifting to translate names:
liftUp /hive/data/genomes/ficAlb1/jkStuff/ensToUcsc.lift
" > ficAlb1.ensGene.ra

    time (doEnsGeneUpdate.pl -ensVersion=91 ficAlb1.ensGene.ra) \
	> ensGene.91.log 2>&1
    # real    1m31.613s

    featureBits cavApe1 ensGene
    # 21842310 bases of 2716396567 (0.804%) in intersection

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

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

    time (doAugustus.pl -buildDir=`pwd` -bigClusterHub=ku \
     -species=chicken -dbHost=hgwdev -workhorse=hgwdev ficAlb1) > do.log 2>&1 &
    # real    51m31.630s

    cat fb.ficAlb1.augustusGene.txt
    # 23463182 bases of 1102325870 (2.129%) in intersection

##############################################################################
# lastz/chain/net swap human/hg38 (DONE - 2018-01-08 - Hiram)
    # original alignment
    cd /hive/data/genomes/hg38/bed/lastzFicAlb1.2018-01-08

    cat fb.hg38.chainFicAlb1Link.txt
    # 147048785 bases of 3049335806 (4.822%) in intersection
    cat fb.hg38.chainSynFicAlb1Link.txt
    # 88694039 bases of 3049335806 (2.909%) in intersection
    cat fb.hg38.chainRBestFicAlb1Link.txt
    # 103439671 bases of 3049335806 (3.392%) in intersection

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

    time (doBlastzChainNet.pl -verbose=2 \
      /hive/data/genomes/hg38/bed/lastzFicAlb1.2018-01-08/DEF \
        -swap -chainMinScore=5000 -chainLinearGap=loose \
          -workhorse=hgwdev -smallClusterHub=ku -bigClusterHub=ku \
            -syntenicNet) > swap.log 2>&1
    #  real    14m32.727s

    cat fb.ficAlb1.chainHg38Link.txt
    # 118476285 bases of 1102325870 (10.748%) in intersection
    cat fb.ficAlb1.chainSynHg38Link.txt
    # 86852014 bases of 1102325870 (7.879%) in intersection

    time (doRecipBest.pl -load  -workhorse=hgwdev -buildDir=`pwd` \
	ficAlb1 hg38) > rbest.log 2>&1
    # real    265m12.990s

    cat fb.ficAlb1.chainRBestHg38Link.txt
    # 103174487 bases of 1102325870 (9.360%) in intersection

##############################################################################
# lastz/chain/net swap mouse/mm10 (DONE - 2018-01-09 - Hiram)
    # original alignment to mm10
    cd /hive/data/genomes/mm10/bed/lastzFicAlb1.2018-01-09

    cat fb.mm10.chainFicAlb1Link.txt
    #   98177848 bases of 2652783500 (3.701%) in intersection
    cat fb.mm10.chainRBestFicAlb1Link.txt
    # 76370866 bases of 2652783500 (2.879%) in intersection

    #	and for the swap
    mkdir /hive/data/genomes/ficAlb1/bed/blastz.mm10.swap
    cd /hive/data/genomes/ficAlb1/bed/blastz.mm10.swap
    time (doBlastzChainNet.pl -verbose=2 \
	/hive/data/genomes/mm10/bed/lastzFicAlb1.2018-01-09/DEF \
        -workhorse=hgwdev -smallClusterHub=ku -bigClusterHub=ku \
        -swap -chainMinScore=5000 -chainLinearGap=loose) > swap.log 2>&1 &
    # real    8m5.637s

    cat  fb.ficAlb1.chainMm10Link.txt
    #   85384367 bases of 1102325870 (7.746%) in intersection

    time (doRecipBest.pl -load -workhorse=hgwdev ficAlb1 mm10 \
      -buildDir=`pwd` -workhorse=hgwdev) > rbest.log 2>&1 &
    # real    209m22.159s

    cat fb.ficAlb1.chainRBestMm10Link.txt
    # 76183235 bases of 1102325870 (6.911%) in intersection

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

    # numerator is ficAlb1 gapless bases "real" as reported by:
    featureBits -noRandom -noHap ficAlb1 gap
    # 9190038 bases of 1034892040 (0.888%) 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 \( 1034892040 / 2861349177 \) \* 1024
    #  ( 1034892040 / 2861349177 ) * 1024 = 370.360059

    # ==> use -repMatch=400 according to size scaled down from 1024 for human.
    #   and rounded up to nearest 50
    cd /hive/data/genomes/ficAlb1
    blat ficAlb1.2bit \
         /dev/null /dev/null -tileSize=11 -makeOoc=jkStuff/ficAlb1.11.ooc \
        -repMatch=400
    #   Wrote 14865 overused 11-mers to jkStuff/ficAlb1.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;' ficAlb1 \
        | sort -k7,7nr | ave -col=7 stdin

    # all these gap sizes are 5000
    gapToLift -verbose=2 -minGap=5000 ficAlb1 jkStuff/nonBridged.lft \
       -bedFile=jkStuff/nonBridged.bed

#########################################################################
# GENBANK AUTO UPDATE (DONE - 2018-01-08 - Hiram)
    ssh hgwdev
    cd $HOME/kent/src/hg/makeDb/genbank
    git pull
    # /cluster/data/genbank/data/organism.lst shows:
    # #organism             mrnaCnt   estCnt  refSeqCnt
    # Ficedula albicollis     0       0       8
    # Ficedula hypoleuca      8       0       0

    # edit etc/genbank.conf to add ficAlb1 just before geoFor1
# ficAlb1 (Ficedula albicollis - collard flycatcher) - taxId: 59894
ficAlb1.serverGenome = /hive/data/genomes/ficAlb1/ficAlb1.2bit
ficAlb1.clusterGenome = /hive/data/genomes/ficAlb1/ficAlb1.2bit
ficAlb1.ooc = /hive/data/genomes/ficAlb1/jkStuff/ficAlb1.11.ooc
ficAlb1.lift = /hive/data/genomes/ficAlb1/jkStuff/nonBridged.lft
ficAlb1.refseq.mrna.native.pslCDnaFilter  = ${finished.refseq.mrna.native.pslCDnaFilter}
ficAlb2.refseq.mrna.xeno.pslCDnaFilter    = ${finished.refseq.mrna.xeno.pslCDnaFilter}
ficAlb1.genbank.mrna.native.pslCDnaFilter = ${finished.genbank.mrna.native.pslCDnaFilter}
ficAlb1.genbank.mrna.xeno.pslCDnaFilter   = ${finished.genbank.mrna.xeno.pslCDnaFilter}
ficAlb1.genbank.est.native.pslCDnaFilter  = ${finished.genbank.est.native.pslCDnaFilter}
# defaults yes: genbank.mrna.native.load and loadDesc
# defaults yes: genbank.est.native.load
# defaults yes: refseq.mrna.native.load, refseq.mrna.native.loadDesc
# defaults yes: refseq.mrna.xeno.load, refseq.mrna.xeno.loadDesc
# defaults no: genbank.mrna.xeno.load and loadDesc
# defaults no: genbank.est.xeno.load, genbank.est.xeno.loadDesc
# DO NOT NEED genbank.mrna.xeno except for human, mouse
# there are no genbank.est natives:
ficAlb1.genbank.est.native.load = no
ficAlb1.downloadDir = ficAlb1
ficAlb1.perChromTables = no

    git commit -m \
     'adding ficAlb1 - Ficedula albicollis - collard flycatcher refs #9870' \
	 etc/genbank.conf src/lib/gbGenome.c
    git push
    git commit -m 'adding ficAlb1 Dipodomys ordii refs #20751' etc/genbank.conf
    git push
    # update /cluster/data/genbank/:
    make install-server
    make etc-update

    cd /cluster/data/genbank

    time ./bin/gbAlignStep -initial ficAlb1
    # logFile: var/build/logs/2018.01.08-18:45:24.ficAlb1.initalign.log
    #   real    184m19.477s

    tail -2 var/build/logs/2018.01.08-18:45:24.ficAlb1.initalign.log
    #	hgwdev 2018.01.08-21:49:03 ficAlb1.initalign: Succeeded: ficAlb1
    #	hgwdev 2018.01.08-21:49:44 ficAlb1.initalign: finish

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

    # load database when finished
    ssh hgwdev
    cd /cluster/data/genbank
    time ./bin/gbDbLoadStep -drop -initialLoad ficAlb1
    # logFile: var/dbload/hgwdev/logs/2018.01.08-22:05:29.ficAlb1.dbload.log
    #  real    7m1.853s


    tail -1 var/dbload/hgwdev/logs/2018.01.08-22:05:29.ficAlb1.dbload.log
    #  hgwdev 2018.01.08-22:12:31 ficAlb1.dbload: finish

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

#############################################################################
#  BLATSERVERS ENTRY (DONE - 2018-01-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 ("ficAlb1", "blat1b", "17892", "1", "0"); \
	INSERT INTO blatServers (db, host, port, isTrans, canPcr) \
	VALUES ("ficAlb1", "blat1b", "17893", "0", "1");' \
	    hgcentraltest
    #	test it with some sequence

############################################################################
## reset default position to same protein location as hg38
##  found by blat (DONE - 2018-01-09 - Hiram)

    ssh hgwdev
    hgsql -e 'update dbDb set defaultPos="chr21:2266579-2296356"
	where name="ficAlb1";' hgcentraltest

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

    cd /hive/data/genomes/ficAlb1
    time (makeDownloads.pl -workhorse=hgwdev ficAlb1) > downloads.log 2>&1
    #  real    11m8.483s

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

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

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

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

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

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

#########################################################################
# tandemDups (DONE - 2018-02-27 - 03-14 - Hiram)
    mkdir /hive/data/genomes/ficAlb1/bed/tandemDups
    cd /hive/data/genomes/ficAlb1/bed/tandemDups

    time (~/kent/src/hg/utils/automation/doTandemDup.pl ficAlb1) > do.log 2>&1
    # had immense trouble in the pairedEnds step, went on for weeks
    # made gigantic files:
# -rw-rw-r-- 1 27432151329 Mar  9 17:00 chr1A.bed.gz
# -rw-rw-r-- 1 30028043163 Mar 12 06:25 chr3.bed.gz
# -rw-rw-r-- 1 41116614429 Mar 13 06:51 chr2.bed.gz
# -rw-rw-r-- 1 36289190868 Mar 13 13:44 chr1.bed.gz

    # after recovery and getting that completed, continuing:
    time (~/kent/src/hg/utils/automation/doTandemDup.pl \
	-continue=collapsePairedEnds ficAlb1) > collapsePairedEnds.log 2>&1 &

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