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

 
# This file describes building the browser database for the archaeal
# species Thermococcus kodakaraensis
#
# if this is the first time you are making your own hgwdev browser, need to do 
# cd ~/kent/src/, then a make

# DOWNLOAD SEQUENCE FROM GENBANK (DONE 10/05)

    mkdir /cluster/store5/archae/therKoda1
    ln -s /cluster/store5/archae/therKoda1 /cluster/data/therKoda1
    cd /cluster/data/therKoda1
    cp /projects/lowelab/db/Bacteria/Thermococcus_kodakaraensis_KOD1/Ther_koda_KOD1* .
    mv Ther_koda_KOD1.fa therKoda1.fa
    grep ">" therKoda1.fa
    # Edit header of therKoda1.fa seqs to '>chr >plasmid_pNRC100 >plasmid_pNRC200'
    faToTwoBit therKoda1.fa therKoda1.2bit 
    mkdir /gbdb/therKoda1
    ln -s /cluster/data/therKoda1/therKoda1.2bit /gbdb/therKoda1/therKoda1.2bit

# CREATE DATABASES AND A BUNCH OF INITIAL STUFF (DONE 10/05)

    ssh hgwdev
    echo 'create database therKoda1' | hgsql ''
    cd /cluster/data/therKoda1

    faSize -detailed therKoda1.fa > chrom.sizes
    echo "create table grp (PRIMARY KEY(NAME)) select * from hg16.grp" | hgsql therKoda1
    echo 'INSERT INTO dbDb (name, description, nibPath, organism, defaultPos, active, orderKey, genome, scientificName, htmlPath, hgNearOk) values ("therKoda1", "May 2004", "/gbdb/therKoda1", "Thermococcus kodakaraensis", "chr:500000-550000", 1, 270, "Thermococcus kodakaraensis","Thermococcus kodakaraensis KOD1", "/gbdb/therKoda1/html/description.html", 0);' | hgsql hgcentraltest
    echo 'INSERT INTO defaultDb (genome, name) values ("Thermococcus kodakaraensis", "therKoda1");' | hgsql hgcentraltest
    echo 'INSERT INTO genomeClade (genome, clade, priority) values ("Thermococcus kodakaraensis", "archaea",85);'  | hgsql hgcentraltest

# CREATE CHROMINFO TABLE (DONE 10/05)

   ssh hgwdev
   cd /cluster/data/therKoda1

   cp ~baertsch/kent/src/hg/lib/chromInfo.sql .
   hgsql therKoda1 < chromInfo.sql
   echo "load data local infile 'chrom.sizes' into table chromInfo" | hgsql therKoda1
   echo "update chromInfo set fileName = '/gbdb/therKoda1/therKoda1.2bit'" | hgsql therKoda1

    cd ~/kent/src/hg/makeDb/trackDb

    # add the trackDb directories
    mkdir -p archae/therKoda1
    cvs add archae/therKoda1
    cvs commit archae/therKoda1

    make DBS=therKoda1


# GC20BASE (DONE 10/05)

    mkdir -p /cluster/data/therKoda1/bed/gc20Base
    cd /cluster/data/therKoda1/bed/gc20Base
    hgGcPercent -wigOut -doGaps -file=stdout -overlap=10 -win=20 therKoda1 /cluster/data/therKoda1/ | wigEncode stdin gc20Base.wig gc20Base.wib
    cd /cluster/data/therKoda1/bed/gc20Base
    mkdir /gbdb/therKoda1/wib
    ln -s `pwd`/gc20Base.wib /gbdb/therKoda1/wib
    hgLoadWiggle -pathPrefix=/gbdb/therKoda1/wib therKoda1 gc20Base gc20Base.wig
    #   verify index is correct:
    hgsql therKoda1 -e "show index from gc20Base;"
    #   should see good numbers in Cardinality column

# TANDEM REPEAT MASKER (DONE 10/05)

    ssh hgwdev
    mkdir -p /cluster/data/therKoda1/bed/simpleRepeat
    cd /cluster/data/therKoda1
    trfBig therKoda1.fa /dev/null -bedAt=/cluster/data/therKoda1/bed/simpleRepeat/chr.bed
    cd /cluster/data/therKoda1/bed/simpleRepeat
    hgLoadBed therKoda1 simpleRepeat *.bed -sqlTable=/cluster/home/lowe/kent/src/hg/lib/simpleRepeat.sql

# MULTIZ with P. abyssi, P. hori, P. furi
# DONE (10/9/05), kpollard

    mkdir -p /cluster/data/therKoda1/bed/conservation
    cd /cluster/data/therKoda1/bed/conservation
    cp /cluster/data/pyrFur2/bed/conservation/HoxD55.q .
    cp /cluster/data/pyrFur2/bed/conservation/*.chr .
    cp /cluster/data/pyrFur2/bed/conservation/*.nib .
    faSize -detailed *.chr > chrom.sizes
    #blastz
    blastz therKoda1.chr pyrFur2.chr Q=HoxD55.q > therKoda1-pyrFur2.lav
    blastz therKoda1.chr pyrAby1.chr Q=HoxD55.q > therKoda1-pyrAby1.lav
    blastz therKoda1.chr pyrHor1.chr Q=HoxD55.q > therKoda1-pyrHor1.lav
    #lavToAxt
    /cluster/bin/i386/lavToAxt therKoda1-pyrFur2.lav . . therKoda1-pyrFur2.axt
    /cluster/bin/i386/lavToAxt therKoda1-pyrAby1.lav . . therKoda1-pyrAby1.axt
    /cluster/bin/i386/lavToAxt therKoda1-pyrHor1.lav . . therKoda1-pyrHor1.axt
    #axtBest
    axtBest therKoda1-pyrFur2.axt therKoda1.chr -winSize=500 -minScore=5000 therKoda1-pyrFur2-best.axt
    axtBest therKoda1-pyrAby1.axt therKoda1.chr -winSize=500 -minScore=5000 therKoda1-pyrAby1-best.axt
    axtBest therKoda1-pyrHor1.axt therKoda1.chr -winSize=500 -minScore=5000 therKoda1-pyrHor1-best.axt
    #axtToMaf
    axtToMaf therKoda1-pyrFur2-best.axt chrom.sizes chrom.sizes therKoda1-pyrFur2.maf
    axtToMaf therKoda1-pyrAby1-best.axt chrom.sizes chrom.sizes therKoda1-pyrAby1.maf
    axtToMaf therKoda1-pyrHor1-best.axt chrom.sizes chrom.sizes therKoda1-pyrHor1.maf
    #multiz
    #delete extra header lines from maf files
    multiz therKoda1-pyrAby1.maf therKoda1-pyrHor1.maf - > therKoda1-pyrAby1-pyrHor1.maf
    multiz therKoda1-pyrFur2.maf therKoda1-pyrAby1-pyrHor1.maf - > therKoda1-pyrAby1-pyrHor1-pyrFur2.maf
    #phyloHMM
    /cluster/bin/phast/msa_view -i MAF -M therKoda1.chr -o SS therKoda1-pyrAby1-pyrHor1-pyrFur2.maf > therKoda1.ss
    /cluster/bin/phast/phyloFit -i SS therKoda1.ss -t "((pyrFur2,(pyrHor1,pyrAby1)),therKoda1)" 
    /cluster/bin/phast/msa_view -i SS therKoda1.ss --summary-only
    /cluster/bin/phast/phastCons therKoda1.ss phyloFit.mod --gc 0.4611 \
	--target-coverage 0.7 --estimate-trees pyr-tree \
	--expected-lengths 25 --no-post-probs --ignore-missing \
	--nrates 1,1
    /cluster/bin/phast/phastCons therKoda1.ss \
	pyr-tree.cons.mod,pyr-tree.noncons.mod \
	--target-coverage 0.7 --expected-lengths 25 \
	--viterbi therKoda1-elements.bed --score \
	--require-informative 0 --seqname chr > cons.dat
    wigEncode cons.dat phastCons.wig phastCons.wib
    draw_tree phyloFit.mod > pyr-tree.ps
    #check similar to pyrFur2 tree
    #move data
    mkdir wib
    mv phastCons.wib wib/phastCons.wib
    mv phastCons.wig wib/phastCons.wig
    ln -s /cluster/data/therKoda1/bed/conservation/wib/phastCons.wib /gbdb/therKoda1/wib
    mkdir -p /gbdb/therKoda1/pwMaf
    mkdir -p otherSpp/pyrFur2 otherSpp/pyrAby1 otherSpp/pyrHor1
    mv therKoda1-pyrFur2.maf otherSpp/pyrFur2/chr.maf
    mv therKoda1-pyrAby1.maf otherSpp/pyrAby1/chr.maf
    mv therKoda1-pyrHor1.maf otherSpp/pyrHor1/chr.maf
    ln -s /cluster/data/therKoda1/bed/conservation/otherSpp/pyrFur2 /gbdb/therKoda1/pwMaf/pyrFur2_pwMaf
    ln -s /cluster/data/therKoda1/bed/conservation/otherSpp/pyrAby1 /gbdb/therKoda1/pwMaf/pyrAby1_pwMaf
    ln -s /cluster/data/therKoda1/bed/conservation/otherSpp/pyrHor1 /gbdb/therKoda1/pwMaf/pyrHor1_pwMaf
    mkdir multiz
    mv therKoda1-pyrAby1-pyrHor1-pyrFur2.maf multiz/chr.maf
    ln -s /cluster/data/therKoda1/bed/conservation/multiz /gbdb/therKoda1/multizTk1Pa1Ph1Pf2
    #load
    hgLoadWiggle therKoda1 phastCons /cluster/data/therKoda1/bed/conservation/wib/phastCons.wig
    hgLoadMaf -warn therKoda1 multizTk1Pa1Ph1Pf2
    hgLoadMaf -warn therKoda1 pyrFur2_pwMaf -pathPrefix=/gbdb/therKoda1/pwMaf/pyrFur2_pwMaf
    hgLoadMaf -warn therKoda1 pyrAby1_pwMaf -pathPrefix=/gbdb/therKoda1/pwMaf/pyrAby1_pwMaf
    hgLoadMaf -warn therKoda1 pyrHor1_pwMaf -pathPrefix=/gbdb/therKoda1/pwMaf/pyrHor1_pwMaf
    hgLoadBed therKoda1 phastConsElements therKoda1-elements.bed 
    #trackDb
    cd ~/kent/src/hg/makeDb/trackDb/archae/
    mkdir therKoda1
    cvs add therKoda1
    cd therKoda1
    #trackDb.ra entry
    # track multizTk1Pa1Ph1Pf2
    # shortLabel Conservation
    # longLabel Pyrococcus 4-way multiz alignments
    # group compGeno
    # priority 10.0
    # visibility pack
    # type wigMaf 0.0 1.0
    # maxHeightPixels 100:40:11
    # wiggle phastCons
    # yLineOnOff Off
    # autoScale Off
    # pairwise pwMaf
    # speciesOrder pyrAby1 pyrHor1 pyrFur2
    cvs add trackDb.ra
    cvs commit -m "New multiz track" trackDb.ra
    #html page for multizTk1Pa1Ph1Pf2
    cvs add multizTk1Pa1Ph1Pf2.html
    cvs commit -m "Details page for multiz track" multizTk1Pa1Ph1Pf2.html

# DESCRIPTION PAGE (DONE 10/20/05), kpollard

    # Write ~/kent/src/hg/makeDb/trackDb/archae/therKoda1/description.html
    cd ~/kent/src/hg/makeDb/trackDb/archae/therKoda1/
    chmod a+r description.html
    cvs add description.html
    cvs commit -m "description page" description.html
    mkdir -p /cluster/data/therKoda1/html/
    cp ~/kent/src/hg/makeDb/trackDb/archae/therKoda1/description.html \
	/cluster/data/therKoda1/html/description.html
    mkdir -p /gbdb/therKoda1/html
    ln -s /cluster/data/therKoda1/html/description.html /gbdb/therKoda1/html/
