# 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/05)

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

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

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

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

    faSize -detailed sulfAcid1.fa > chrom.sizes
    echo "create table grp (PRIMARY KEY(NAME)) select * from hg16.grp" \
	    | hgsql sulfAcid1
    echo 'INSERT INTO dbDb (name, description, nibPath, organism, defaultPos, active, orderKey, genome, scientificName, htmlPath, hgNearOk) values ("sulfAcid1", "July 2005", "/gbdb/sulfAcid1", "Sulfolobus acidocaldarius", "chr:500000-550000", 1, 220, "Sulfolobus acidocaldarius","Sulfolobus acidocaldarius DSM 639", "/gbdb/sulfAcid1/html/description.html", 0);' | hgsql hgcentraltest
    echo 'INSERT INTO defaultDb (genome, name) values ("Sulfolobus acidocaldarius", "sulfAcid1");' | hgsql hgcentraltest
    echo 'INSERT INTO genomeClade (genome, clade, priority) values ("Sulfolobus acidocaldarius", "archaea",85);'  | hgsql hgcentraltest

# CREATE CHROMINFO TABLE (DONE 10/05)
  ssh hgwdev
  cd /cluster/data/sulfAcid1

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

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

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

    make DBS=sulfAcid1

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

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


# TANDEM REPEAT MASKER (DONE 10/05)

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

# MULTIZ (DONE 10/8/05)

    cd /cluster/data/sulfAcid1/bed
    mkdir conservation
    cd conservation
    cp /cluster/data/pyrFur2/bed/conservation/HoxD55.q .
    cp /cluster/data/sulSol1/bed/conservation/sulAci1.chr sulfAcid1.chr
    cp /cluster/data/sulSol1/bed/conservation/sulTok1.chr sulfToko1.chr
    cp /cluster/data/sulSol1/bed/conservation/sulSol1.chr .
    #edit headers for Aci and Tok to be sulfAcid1, sulfToko1
    /cluster/bin/i386/faToNib sulfAcid1.chr sulfAcid1.chr.nib
    /cluster/bin/i386/faToNib sulfToko1.chr sulfToko1.chr.nib
    cp /cluster/data/sulSol1/bed/conservation/sulSol1.chr.nib .
    faSize -detailed *.chr > chrom.sizes
    #blastz for Aci-Tok
    blastz sulfAcid1.chr sulfToko1.chr Q=HoxD55.q > sulfAcid1-sulfToko1.lav
    /cluster/bin/i386/lavToAxt sulfAcid1-sulfToko1.lav . . sulfAcid1-sulfToko1.axt
    axtBest sulfAcid1-sulfToko1.axt sulfAcid1.chr -winSize=500 -minScore=5000 sulfAcid1-sulfToko1-best.axt
    axtToMaf sulfAcid1-sulfToko1-best.axt chrom.sizes chrom.sizes sulfAcid1-sulfToko1.maf
    #blastz for Aci-Sol
    blastz sulfAcid1.chr sulSol1.chr Q=HoxD55.q > sulfAcid1-sulSol1.lav
    /cluster/bin/i386/lavToAxt sulfAcid1-sulSol1.lav . . sulfAcid1-sulSol1.axt
    axtBest sulfAcid1-sulSol1.axt sulfAcid1.chr -winSize=500 -minScore=5000 sulfAcid1-sulSol1-best.axt
    axtToMaf sulfAcid1-sulSol1-best.axt chrom.sizes chrom.sizes sulfAcid1-sulSol1.maf
    #multiz
    #delete extra header lines from maf files
    multiz sulfAcid1-sulfToko1.maf sulfAcid1-sulSol1.maf - > sulfAcid1-sulfToko1-sulSol1.maf
    #phyloHMM
    /cluster/bin/phast/msa_view -i MAF -M sulfAcid1.chr -o SS sulfAcid1-sulfToko1-sulSol1.maf > sulfAcid1.ss
    /cluster/bin/phast/phyloFit -i SS sulfAcid1.ss -t "(sulSol1,(sulfToko1,sulfAcid1))" 
    /cluster/bin/phast/msa_view -i SS sulfAcid1.ss --summary-only
    /cluster/bin/phast/phastCons sulfAcid1.ss phyloFit.mod --gc 0.3507 \
	--target-coverage 0.7 --estimate-trees sul-tree \
	--expected-lengths 25 --no-post-probs --ignore-missing \
	--nrates 1,1
    /cluster/bin/phast/phastCons sulfAcid1.ss \
	sul-tree.cons.mod,sul-tree.noncons.mod \
	--target-coverage 0.7 --expected-lengths 25 \
	--viterbi sulfAcid1-elements.bed --score \
	--require-informative 0 --seqname chr > cons.dat
    wigEncode cons.dat phastCons.wig phastCons.wib
    draw_tree phyloFit.mod > sulfAcid1-tree.ps
    #check that tree is similar to sul-tree.ps from sulSol1
    #move data
    mkdir wib
    mv phastCons.wib wib/phastCons.wib
    mv phastCons.wig wib/phastCons.wig
    ln -s /cluster/data/sulfAcid1/bed/conservation/wib/phastCons.wib /gbdb/sulfAcid1/wib
    mkdir -p /gbdb/sulfAcid1/pwMaf
    mkdir -p otherSul/sulfToko1 otherSul/sulSol1
    mv sulfAcid1-sulfToko1.maf otherSul/sulfToko1/chr.maf
    mv sulfAcid1-sulSol1.maf otherSul/sulSol1/chr.maf
    ln -s /cluster/data/sulfAcid1/bed/conservation/otherSul/sulfToko1 /gbdb/sulfAcid1/pwMaf/sulfToko1_pwMaf
    ln -s /cluster/data/sulfAcid1/bed/conservation/otherSul/sulSol1 /gbdb/sulfAcid1/pwMaf/sulSol1_pwMaf
    mkdir multiz
    mv sulfAcid1-sulfToko1-sulSol1.maf multiz/chr.maf
    ln -s /cluster/data/sulfAcid1/bed/conservation/multiz /gbdb/sulfAcid1/multizSa1St1Ss1
    #load
    hgLoadWiggle sulfAcid1 phastCons /cluster/data/sulfAcid1/bed/conservation/wib/phastCons.wig
    hgLoadMaf -warn sulfAcid1 multizSa1St1Ss1
    hgLoadMaf -warn sulfAcid1 sulfToko1_pwMaf -pathPrefix=/gbdb/sulfAcid1/pwMaf/sulfToko1_pwMaf
    hgLoadMaf -warn sulfAcid1 sulSol1_pwMaf -pathPrefix=/gbdb/sulfAcid1/pwMaf/sulSol1_pwMaf
    hgLoadBed sulfAcid1 phastConsElements sulfAcid1-elements.bed 
    #trackDb
    cd ~/kent/src/hg/makeDb/trackDb/archae
    mkdir sulfAcid1
    cvs add sulfAcid1
    cd sulfAcid1
    #trackDb.ra entry
    # track multizSa1St1Ss1
    # shortLabel Conservation
    # longLabel Sulfolobus 3-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 sulfToko1 sulSol1
    cvs add trackDb.ra
    cvs commit -m "Added multiz track" trackDb.ra
    #html page for multizSa1St1Ss1
    cd ~/kent/src/hg/makeDb/trackDb/archae/sulfAcid1
    cvs add multizSa1St1Ss1.html
    cvs commit -m "Details page for multiz track" multizSa1St1Ss1.html
 
# DESCRIPTION PAGE (DONE 10/20/05), kpollard

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

# GENBANK PROTEIN-CODING GENES (TO DO)

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

#genbank to genePred

csh
tawk '{print $1,$2,$3,$4,$5,$6,$2,$3,0,1,$3-$2,0}' sulfAcid1-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 sulfAcid1-genbank-cds.xra > sulfAcid1.gp

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

# 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 (TO DO)

    # 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/sulfAcid1/bed/loweTrnaGene
    cd /cluster/data/sulfAcid1/bed/loweTrnaGene
    hgLoadBed -tab sulfAcid1 loweTrnaGene sulfAcid1-lowe-trnas.bed -sqlTable=~/kent/src/hg/lib/loweTrnaGene.sql

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

# TIGR GENES (TO DO)
    # 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.
    ssh hgwdev
    mkdir /cluster/data/sulfAcid1/bed/tigrCmrORFs
    cp sulfAcid1-tigr.tab /cluster/data/sulfAcid1/bed/tigrCmrORFs
    cd /cluster/data/sulfAcid1/bed/tigrCmrORFs
    /projects/lowelab/users/aamp/bin/i386/tigrCmrToBed sulfAcid1-tigr.tab sulfAcid1-tigr.bed
    hgLoadBed -tab sulfAcid1 tigrCmrORFs sulfAcid1-tigr.bed -sqlTable=~/kent/src/hg/lib/tigrCmrGene.sql
