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

# rock hyrax ( Procavia capensis )
#########################################################################
# DOWNLOAD SEQUENCE (DONE braney 2008-10-07)
    ssh kkstore05
    mkdir /cluster/store12/proCap1
    ln -s /cluster/store12/proCap1 /cluster/data
    mkdir /cluster/data/proCap1/broad
    cd /cluster/data/proCap1/broad

    wget --timestamping \
ftp://ftp.broad.mit.edu/pub/assemblies/mammals/rockHyrax/Procap1.0/assembly.agp \
ftp://ftp.broad.mit.edu/pub/assemblies/mammals/rockHyrax/Procap1.0/assembly.bases.gz \
ftp://ftp.broad.mit.edu/pub/assemblies/mammals/rockHyrax/Procap1.0/assembly.quals.gz 
    md5sum ass* > assembly.md5sum

    qaToQac assembly.quals.gz stdout | qacAgpLift assembly.agp stdin proCap1.qual.qac

   cut -f 1 assembly.agp | uniq -c | wc -l 
   # Number of scaffolds: 295006

#########################################################################
# Create .ra file and run makeGenomeDb.pl (DONE braney 2008-10-07)
    ssh kolossus
    cd /cluster/data/proCap1
cat << _EOF_ >proCap1.config.ra
# Config parameters for makeGenomeDb.pl:
db proCap1
clade mammal
genomeCladePriority 35
scientificName  Procavia capensis 
commonName Rock hyrax
assemblyDate Jul. 2008
assemblyLabel Broad Institute proCap1 
orderKey 236.5
#mitoAcc AJ222767
mitoAcc none
fastaFiles /cluster/data/proCap1/broad/assembly.bases.gz
agpFiles /cluster/data/proCap1/broad/assembly.agp
qualFiles /cluster/data/proCap1/broad/proCap1.qual.qac
dbDbSpeciesDir rockHyrax
_EOF_

    makeGenomeDb.pl -workhorse kolossus proCap1.config.ra > makeGenomeDb.out 2>&1 &

    cut -f 2 chrom.sizes | ave stdin
# Q1 1549.000000
# median 4249.000000
# Q3 11283.000000
# average 10119.316214
# min 315.000000
# max 390036.000000
# count 295006
# total 2985258999.000000
# standard deviation 16728.518926

#########################################################################
## Repeat Masker (DONE - 2008-10-16 - Hiram)
    screen	# to manage this several day job
    mkdir /hive/data/genomes/proCap1/bed/repeatMasker
    cd /hive/data/genomes/proCap1/bed/repeatMasker
    time $HOME/kent/src/hg/utils/automation/doRepeatMasker.pl \
	-workhorse=hgwdev -bigClusterHub=swarm \
	-buildDir=`pwd` proCap1 > do.log 2>&1 &
    #	real    867m49.502s
    twoBitToFa proCap1.rmsk.2bit stdout | faSize stdin > faSize.rmsk.txt
# 2985258999 bases (577411318 N's 2407847681 real 1731549681 upper 676298000
# lower) in 295006 sequences in 1 files
# %22.65 masked total, %28.09 masked rea

#########################################################################
# SIMPLE REPEATS TRF (DONE - 2008-10-16 - Hiram)
    screen # use a screen to manage this job
    mkdir /cluster/data/proCap1/bed/simpleRepeat
    cd /cluster/data/proCap1/bed/simpleRepeat
    # 
    time $HOME/kent/src/hg/utils/automation/doSimpleRepeat.pl \
	-buildDir=/cluster/data/proCap1/bed/simpleRepeat proCap1 > do.log 2>&1 &
    #	real    67m53.007s
    #	batch failed on pk, finish it manually
    #	this was an unusual error, when TrfRun.csh was splitting up the .lft
    #	file, it ended up with a single file in the last split, and that
    #	single file had no results for it, so the lift of that non-existent
    #	fail failed.  Temporarily changed, in TrfRun.csh:
    #	split -l 500 $inLft SplitLft.
    #	to be:
    #	split -l 400 $inLft SplitLft.
    #	and ran TrfRun.csh manually:
    time ./TrfRun.csh /san/sanvol1/scratch/proCap1/TrfPart/039/039.lst.bed
    #	now, continuing:
    time $HOME/kent/src/hg/utils/automation/doSimpleRepeat.pl \
	-continue=filter \
	-buildDir=/cluster/data/proCap1/bed/simpleRepeat proCap1 \
	> filter.log 2>&1 &

    cat fb.simpleRepeat
    #	24599087 bases of 2407847681 (1.022%) in intersection

    #	after RM run is done, add this mask:
    cd /hive/data/genomes/proCap1
    rm proCap1.2bit
    twoBitMask proCap1.rmsk.2bit -add bed/simpleRepeat/trfMask.bed proCap1.2bit
    #	can safely ignore warning about >=13 fields in bed file

    twoBitToFa proCap1.2bit stdout | faSize stdin > proCap1.2bit.faSize.txt
# 2985258999 bases (577411318 N's 2407847681 real 1731119427 upper 676728254
# lower) in 295006 sequences in 1 files
# %22.67 masked total, %28.11 masked rea

    #	link to gbdb
    ln -s `pwd`/proCap1.2bit /gbdb/proCap1

###########################################################################
# prepare for kluster runs (DONE 2008-10-16 - Hiram)
    # compare to size of real bases to adjust the repMatch
    #	hg18: 2881421696
    #	proCap1: 2407847681
    # thus: 1024 * 2407847681/2881421696 = 855
    #	rounding up to 900 to be conservative
    cd /hive/data/genomes/proCap1
    time blat proCap1.2bit \
	/dev/null /dev/null -tileSize=11 -makeOoc=proCap1.11.ooc -repMatch=900
    #	Wrote 30662 overused 11-mers to proCap1.11.ooc
    #	real    2m12.136s


    #	and staging data for push to kluster nodes
    mkdir /hive/data/staging/data/proCap1
    cp -p proCap1.2bit chrom.sizes proCap1.11.ooc \
	/hive/data/staging/data/proCap1
    #	request to cluster admin to push this to the kluster nodes
    #	/scratch/data/

###########################################################################
# add NCBI identifiers to the dbDb (DONE - 2008-10-21 - Hiram)
    hgsql -e 'update dbDb set
sourceName="Broad Institute proCap1 (NCBI project 13972, ABRQ01000000)" where name="proCap1";' hgcentraltest

############################################################################
#  proCap1 - Rock hyrax - Ensembl Genes version 51  (DONE - 2008-12-02 -
#  hiram)
    ssh hgwdev
    cd /hive/data/genomes/proCap1
    cat << '_EOF_' > proCap1.ensGene.ra
# required db variable
db proCap1
# do we need to translate geneScaffold coordinates
geneScaffolds yes
# ignore genes that do not properly convert to a gene pred, and contig
#       names that are not in the UCSC assembly
skipInvalid yes
# ignore the two genes that have invalid structures from Ensembl:
# 4595: ENSPCAT00000007286 no exonFrame on CDS exon 1
# 28894: ENSPCAT00000000699 no exonFrame on CDS exon 4
'_EOF_'
#  << happy emacs

    doEnsGeneUpdate.pl -ensVersion=51 proCap1.ensGene.ra
    ssh hgwdev
    cd /hive/data/genomes/proCap1/bed/ensGene.51
    featureBits proCap1 ensGene
    # 25217399 bases of 2407847681 (1.047%) in intersection

 *** All done!  (through the 'makeDoc' step)
 *** Steps were performed in /hive/data/genomes/proCap1/bed/ensGene.51

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