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

 
# This file describes building the browser database for the archaeal
# species Methanosarcina acetivorans.
#
# 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/3/05)

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

    mkdir /gbdb/therVolc1
    ln -s /cluster/data/therVolc1/therVolc1.2bit /gbdb/therVolc1/therVolc1.2bit

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

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

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

# CREATE CHROMINFO TABLE 
  ssh hgwdev
  cd /cluster/data/therVolc1

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

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

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

    make DBS=therVolc1


# GC20BASE (DONE 10/3/05)
    mkdir -p /cluster/data/therVolc1/bed/gc20Base
    cd /cluster/data/therVolc1/bed/gc20Base
    hgGcPercent -wigOut -doGaps -file=stdout -overlap=10 -win=20 therVolc1 /cluster/data/therVolc1/ | wigEncode stdin gc20Base.wig gc20Base.wib

    cd /cluster/data/therVolc1/bed/gc20Base
    mkdir /gbdb/therVolc1/wib
    ln -s `pwd`/gc20Base.wib /gbdb/therVolc1/wib
    hgLoadWiggle -pathPrefix=/gbdb/therVolc1/wib therVolc1 gc20Base gc20Base.wig
    #	verify index is correct:
    hgsql therVolc1 -e "show index from gc20Base;"
    #	should see good numbers in Cardinality column


# TANDEM REPEAT MASKER (DONE 10/3/05)

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

# TIGR GENES (DONE 10/3/05)
    # First go to http://www.tigr.org/tigr-scripts/CMR2/gene_attribute_form.dbi
    # and fill out the web form as follows:
    #   - Pick "Retrieve attributes for the specified DNA feature within a specific 
    #     organism and/or a specific role category".
    #       * Pick "Pyrobaculum aerophilum IM2", and "Primary and TIGR annotation ORFs" 
    #         from the 1st and 3rd box.
    #       * Select everything from "Choose TIGR Annotation Gene Attributes"
    #       * Select "Primary Locus Name" from "Choose Primary Annotation Gene Attributes"
    #       * Select everything from "Choose Other Gene Attributes"
    #   - Click submit, and click save as tab-delimited file.

    mkdir -p /cluster/data/therVolc1/bed/tigrCmrORFs
    cp therVolc1-tigr.tab /cluster/data/therVolc1/bed/tigrCmrORFs
    cd /cluster/data/therVolc1/bed/tigrCmrORFs
    ~aamp/bin/i386/tigrCmrToBed therVolc1-tigr.tab therVolc1-tigr.bed
    cat therVolc1-tigr.bed | sed -e 's/chr1/chr/' > temp
    mv temp therVolc1-tigr.bed
    hgLoadBed -tab therVolc1 tigrCmrGene therVolc1-tigr.bed -sqlTable=/cluster/home/kpollard/kent/src/hg/lib/tigrCmrGene.sql
    echo "rename table tigrCmrGene to tigrCmrORFs;" | hgsql therVolc1

# DESCRIPTION PAGE (DONE 10/4/05)

    # Write ~/kent/src/hg/makeDb/trackDb/archae/therVolc1/description.html
    chmod a+r ~/kent/src/hg/makeDb/trackDb/archae/therVolc1/description.html
    mkdir -p /cluster/data/therVolc1/html/
    cp ~/kent/src/hg/makeDb/trackDb/archae/therVolc1/description.html /cluster/data/therVolc1/html/description.html
    mkdir -p /gbdb/therVolc1/html
    ln -s /cluster/data/therVolc1/html/description.html /gbdb/therVolc1/html/

# MULTIZ with therVolc, picrTorr, and ferrAcid
# DONE (10/11/05), kpollard

    cd /cluster/data/therVolc1/bed/
    mkdir conservation
    cd conservation
    cp /cluster/data/therAcid1/bed/conservation/HoxD55.q .
    cp /cluster/data/therAcid1/bed/conservation/*.chr .
    cp /cluster/data/therAcid1/bed/conservation/*.nib .
    cp /cluster/data/therAcid1/bed/conservation/*.2bit .

    #chrom sizes
    faSize -detailed *.chr > chrom.sizes

    #blastz 
    blastz therVolc1.chr therAcid1.chr Q=HoxD55.q > therVolc1-therAcid1.lav
    blastz therVolc1.chr picrTorr1.chr Q=HoxD55.q > therVolc1-picrTorr1.lav
    blastz therVolc1.chr ferrAcid1.chr Q=HoxD55.q > therVolc1-ferrAcid1.lav

    /cluster/bin/i386/lavToAxt therVolc1-therAcid1.lav . . therVolc1-therAcid1.axt
    /cluster/bin/i386/lavToAxt therVolc1-picrTorr1.lav . . therVolc1-picrTorr1.axt
    /cluster/bin/i386/lavToAxt therVolc1-ferrAcid1.lav . ferrAcid1.2bit therVolc1-ferrAcid1.axt

    axtBest therVolc1-therAcid1.axt therVolc1.chr -winSize=500 -minScore=5000 therVolc1-therAcid1-best.axt
    axtBest therVolc1-picrTorr1.axt therVolc1.chr -winSize=500 -minScore=5000 therVolc1-picrTorr1-best.axt
    axtBest therVolc1-ferrAcid1.axt therVolc1.chr -winSize=500 -minScore=5000 therVolc1-ferrAcid1-best.axt

    axtToMaf therVolc1-therAcid1-best.axt chrom.sizes chrom.sizes therVolc1-therAcid1.maf
    axtToMaf therVolc1-picrTorr1-best.axt chrom.sizes chrom.sizes therVolc1-picrTorr1.maf
    axtToMaf therVolc1-ferrAcid1-best.axt chrom.sizes chrom.sizes therVolc1-ferrAcid1.maf

    #multiz
    #remove extra header lines
    multiz therVolc1-therAcid1.maf therVolc1-picrTorr1.maf - > therVolc1-therAcid1-picrTorr1.maf
    multiz therVolc1-ferrAcid1.maf therVolc1-therAcid1-picrTorr1.maf - > therVolc1-therAcid1-picrTorr1-ferrAcid1.maf

    #phyloHMM
    /cluster/bin/phast/msa_view -i MAF -M therVolc1.chr -o SS therVolc1-therAcid1-picrTorr1-ferrAcid1.maf > therVolc1.ss
    /cluster/bin/phast/phyloFit -i SS therVolc1.ss -t "(ferrAcid1,(picrTorr1,(therAcid1,therVolc1)))" -o TvTaPtFa
    /cluster/bin/phast/msa_view -i SS therVolc1.ss --summary-only
    #add GC content to next call
    /cluster/bin/phast/phastCons therVolc1.ss TvTaPtFa.mod --gc 0.4078 \
    --target-coverage 0.7 --estimate-trees ther-tree \
    --expected-lengths 25 --no-post-probs --ignore-missing \
    --nrates 1,1
    /cluster/bin/phast/phastCons therVolc1.ss \
    ther-tree.cons.mod,ther-tree.noncons.mod \
    --target-coverage 0.7 --expected-lengths 25 \
    --viterbi therVolc1-elements.bed --score \
    --require-informative 0 --seqname chr > cons.dat
    wigEncode cons.dat phastCons.wig phastCons.wib
    /cluster/bin/phast/draw_tree TvTaPtFa.mod > ther-tree.ps 
    #compare to therAcid1 tree.

    #move data
    mkdir wib
    mv phastCons.wib wib/phastCons.wib
    mv phastCons.wig wib/phastCons.wig
    ln -s /cluster/data/therVolc1/bed/conservation/wib/phastCons.wib /gbdb/therVolc1/wib
    mkdir /gbdb/therVolc1/pwMaf
    mkdir -p otherSpp/therAcid1 otherSpp/picrTorr1 otherSpp/ferrAcid1
    mv therVolc1-picrTorr1.maf otherSpp/picrTorr1/chr.maf
    mv therVolc1-therAcid1.maf otherSpp/therAcid1/chr.maf
    mv therVolc1-ferrAcid1.maf otherSpp/ferrAcid1/chr.maf
    ln -s /cluster/data/therVolc1/bed/conservation/otherSpp/picrTorr1 /gbdb/therVolc1/pwMaf/picrTorr1_pwMaf
    ln -s /cluster/data/therVolc1/bed/conservation/otherSpp/ferrAcid1 /gbdb/therVolc1/pwMaf/ferrAcid1_pwMaf
    ln -s /cluster/data/therVolc1/bed/conservation/otherSpp/therAcid1 /gbdb/therVolc1/pwMaf/therAcid1_pwMaf
    mkdir multiz
    mv therVolc1-therAcid1-picrTorr1-ferrAcid1.maf multiz/chr.maf
    ln -s /cluster/data/therVolc1/bed/conservation/multiz /gbdb/therVolc1/multizTvTaPtFa

    #load
    hgLoadWiggle therVolc1 phastCons /cluster/data/therVolc1/bed/conservation/wib/phastCons.wig
    hgLoadMaf -warn therVolc1 multizTvTaPtFa
    hgLoadMaf -warn therVolc1 picrTorr1_pwMaf -pathPrefix=/gbdb/therVolc1/pwMaf/picrTorr1_pwMaf
    hgLoadMaf -warn therVolc1 ferrAcid1_pwMaf -pathPrefix=/gbdb/therVolc1/pwMaf/ferrAcid1_pwMaf
    hgLoadMaf -warn therVolc1 therAcid1_pwMaf -pathPrefix=/gbdb/therVolc1/pwMaf/therAcid1_pwMaf
    hgLoadBed therVolc1 phastConsElements therVolc1-elements.bed 

    #trackDb
    cd ~/kent/src/hg/makeDb/trackDb/archae/therVolc1
    #trackDb.ra entry
    # track multizTvTaPtFa
    # shortLabel Conservation
    # longLabel Thermoplasma/Ferroplasma/Picrophilus 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 therAcid1 picrTorr1 ferrAcid1
    cvs add trackDb.ra
    cvs commit -m "New multiz track" trackDb.ra
    #html page
    cvs add multizTvTaPtFa.html
    cvs commit -m "Details page for multiz track" multizTvTaPtFa.html

# GENBANK PROTEIN-CODING GENES 

    ssh hgwdev
    mkdir /cluster/data/therVolc1/genbank
    cd /cluster/data/therVolc1/genbank
    cp /projects/lowelab/db/Bacteria/Thermoplasma_volcanium/ .
    
    mv NC_003552.gbk therVolc1.gbk
    # Create 3 files to assist parsing of the genbank
    # 1. for a bed file
    echo 'chr
start
end
gene
1000
strand' > therVolc1-params-bed.txt
    # 2. for the peptide parts
    echo 'gene
translation' > therVolc1-params-pep.txt
    # 3. for the other gene information
    echo 'gene
product
note' > therVolc1-params-xra.txt
    # Now extract the genes and information:
    gbArchaeGenome therVolc1.gbk therVolc1-params-bed.txt therVolc1-genbank-cds.bed
    gbArchaeGenome therVolc1.gbk therVolc1-params-pep.txt therVolc1-genbank-cds.pep
    gbArchaeGenome therVolc1.gbk therVolc1-params-xra.txt therVolc1-genbank-cds.xra
    hgLoadBed therVolc1 gbProtCode therVolc1-genbank-cds.bed
    hgsql therVolc1 < ~/kent/src/hg/lib/pepPred.sql
    hgsql therVolc1 < ~/kent/src/hg/lib/minGeneInfo.sql
    echo rename table pepPred to gbProtCodePep | hgsql therVolc1
    echo rename table minGeneInfo to gbProtCodeXra | hgsql therVolc1
    echo load data local infile \'therVolc1-genbank-cds.pep\' into table gbProtCodePep | hgsql therVolc1
    echo load data local infile \'therVolc1-genbank-cds.xra\' into table gbProtCodeXra | hgsql therVolc1

#genbank to genePred

csh
tawk '{print $1,$2,$3,$4,$5,$6,$2,$3,0,1,$3-$2,0}' therVolc1-genbank-cds.bed | bedToGenePred stdin tmp.gp
tawk '{print $1,$2,$3,$4,$5,$6,$7,$8,$9,$10,substr($1,3,4),name2,"cmpl","cmpl",0}' tmp.gp  > tmp2.gp
join -t "     " -o 1.1,1.2 1.3 1.4 1.5 1.6 1.7 1.8 1.9 1.10 1.11 2.3 1.13 1.14 1.15  tmp2.gp therVolc1-genbank-cds.xra > therVolc1.gp

# GENBANK rRNA GENES 
    ssh hgdev
    cd /cluster/data/therVolc1/genbank
    gbArchaeGenome -kind=rRNA therVolc1.gbk therVolc1-params-bed.txt therVolc1-rrnas.bed
    echo 'gene product NA' > therVolc1-params-rrna-xra.txt
    gbArchaeGenome -kind=rRNA therVolc1.gbk therVolc1-params-rrna-xra.txt therVolc1-rrnas-xra.txt
    hgLoadBed therVolc1 gbRRNA therVolc1-rrnas.bed
    hgsql therVolc1 < ~/kent/src/hg/lib/minGeneInfo.sql
    echo rename table minGeneInfo to gbRRNAXra | hgsql therVolc1
    echo load data local infile \'therVolc1-rrnas-xra.txt\' into table gbRRNAXra | hgsql therVolc1

# COG STUFF
    # Cut and paste http://www.ncbi.nlm.nih.gov/cgi-bin/COG/palox into emacs (COG list)
    # and save as cogpage.txt
    awk '{printf("%s\t%s\n",$6,$5)}' < cogpage.txt | sed -e 's/\[//' -e 's/\]//' > cogs.txt
    rm cogpage.txt
    # Now we have the basic list of cogs and the letter code for each one.
    

# TODD LOWE tRNA GENES 
 # This one is a bed 6+ file created by hand of 46 tRNAs and 1 pseudo tRNA by Todd
    # Lowe.  See ~/kent/src/hg/lib/loweTrnaGene.as for a description of the fields.
    # **Showing the tRNAScanSE instructions would be nice in the future.  
    ssh hgwdev
    mkdir /cluster/data/therVolc1/bed/loweTrnaGene
    cd /cluster/data/therVolc1/bed/loweTrnaGene
    hgLoadBed -tab therVolc1 loweTrnaGene therVolc1-lowe-trnas.bed -sqlTable=~/kent/src/hg/lib/loweTrnaGene.sql

# TODD LOWE snoRNA GENES 
    # This is a bed 6 file created by hand.
    ssh hgwdev
    mkdir /cluster/data/therVolc1/bed/loweSnoGene
    cd /cluster/data/therVolc1/bed/loweSnoGene
    hgLoadBed -tab therVolc1 loweSnoGene therVolc1-snos.bed
