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

 
# This file describes building the browser database for the archaeal
# species Methanosarcina mazei Go1.


    ssh eieio
    mkdir /cluster/store5/archae/methMaze1
    ln -s /cluster/store5/archae/methMaze1 /cluster/data/methMaze1
    cd /cluster/data/methMaze1
    cp /projects/lowelab/db/Bacteria/Methanosarcina_mazei/NC_003901.fna .

    cp NC_00390l.fna methMaze1.fa
    # Edit header of *.fa to '> chr '
   
    faToTwoBit methMaze1.fa methMaze1.2bit 


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

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

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

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

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

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

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

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


# TANDEM REPEAT MASKER (DONE)

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

# MULTIZ with other methanogens
# DONE (10/9/05), kpollard

    cd /cluster/data/methMaze1/bed/
    mkdir conservation
    cd conservation
    cp /cluster/data/metAce1/bed/conservation/HoxD55.q .
    cp /cluster/data/metAce1/bed/conservation/*.chr .
    cp /cluster/data/metAce1/bed/conservation/*.nib .
    #fix names to be methMaze1, methBark1, methBurt1
    faToNib metMaz1.chr methMaze1.chr.nib
    faToTwoBit metBak0.chr metBak0.2bit
    faToTwoBit metBur0.chr metBur0.2bit

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

    #blastz 
    blastz metMaz1.chr metAce1.chr Q=HoxD55.q > metMaz1-metAce1.lav
    blastz metMaz1.chr metBak0.chr Q=HoxD55.q > metMaz1-metBak0.lav
    blastz metMaz1.chr metBur0.chr Q=HoxD55.q > metMaz1-metBur0.lav

    /cluster/bin/i386/lavToAxt metMaz1-metAce1.lav . . metMaz1-metAce1.axt
    /cluster/bin/i386/lavToAxt metMaz1-metBak0.lav . metBak0.2bit metMaz1-metBak0.axt
    /cluster/bin/i386/lavToAxt metMaz1-metBur0.lav . metBur0.2bit metMaz1-metBur0.axt

    axtBest metMaz1-metAce1.axt metMaz1.chr -winSize=500 -minScore=5000 metMaz1-metAce1-best.axt
    axtBest metMaz1-metBak0.axt metMaz1.chr -winSize=500 -minScore=5000 metMaz1-metBak0-best.axt
    axtBest metMaz1-metBur0.axt metMaz1.chr -winSize=500 -minScore=5000 metMaz1-metBur0-best.axt

    axtToMaf metMaz1-metAce1-best.axt chrom.sizes chrom.sizes metMaz1-metAce1.maf
    axtToMaf metMaz1-metBak0-best.axt chrom.sizes chrom.sizes metMaz1-metBak0.maf
    axtToMaf metMaz1-metBur0-best.axt chrom.sizes chrom.sizes metMaz1-metBur0.maf

    #multiz
    #remove extra header lines
    multiz metMaz1-metAce1.maf metMaz1-metBak0.maf - > metMaz1-metAce1-metBak0.maf
    multiz metMaz1-metBur0.maf metMaz1-metAce1-metBak0.maf - > metMaz1-metAce1-metBak0-metBur0.maf

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

    #move data
    mkdir wib
    mv phastCons.wib wib/phastCons.wib
    mv phastCons.wig wib/phastCons.wig
    ln -s /cluster/data/methMaze1/bed/conservation/wib/phastCons.wib /gbdb/methMaze1/wib
    mkdir /gbdb/methMaze1/pwMaf
    mkdir -p otherMet/metAce1 otherMet/metBak1 otherMet/metBur0
    mv metMaz1-metBak0.maf otherMet/metBak0/chr.maf
    mv metMaz1-metAce1.maf otherMet/metAce1/chr.maf
    mv metMaz1-metBur0.maf otherMet/metBur0/chr.maf
    ln -s /cluster/data/methMaze1/bed/conservation/otherMet/metBak0 /gbdb/methMaze1/pwMaf/methBark0_pwMaf
    ln -s /cluster/data/methMaze1/bed/conservation/otherMet/metBur0 /gbdb/methMaze1/pwMaf/methBurt0_pwMaf
    ln -s /cluster/data/methMaze1/bed/conservation/otherMet/metAce1 /gbdb/methMaze1/pwMaf/metAce1_pwMaf
    mkdir multiz
    mv metMaz1-metAce1-metBak0-metBur0.maf multiz/chr.maf
    ln -s /cluster/data/methMaze1/bed/conservation/multiz /gbdb/methMaze1/multizMz1Ma1Mbk0Mbr0

    #load
    hgLoadWiggle methMaze1 phastCons /cluster/data/methMaze1/bed/conservation/wib/phastCons.wig
    hgLoadMaf -warn methMaze1 multizMz1Ma1Mbk0Mbr0
    hgLoadMaf -warn methMaze1 methBark0_pwMaf -pathPrefix=/gbdb/methMaze1/pwMaf/methBark0_pwMaf
    hgLoadMaf -warn methMaze1 methBurt0_pwMaf -pathPrefix=/gbdb/methMaze1/pwMaf/methBurt0_pwMaf
    hgLoadMaf -warn methMaze1 metAce1_pwMaf -pathPrefix=/gbdb/methMaze1/pwMaf/metAce1_pwMaf
    hgLoadBed methMaze1 phastConsElements metMaz1-elements.bed 

    #trackDb
    cd ~/kent/src/hg/makeDb/trackDb/archae/methMaze1
    #trackDb.ra entry
    # track multizMz1Ma1Mbk0Mbr0
    # shortLabel Cons 4way
    # longLabel Methosarcina 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 metAce1 methBark1 methBurt1
    cvs commit -m "New multiz track" trackDb.ra
    #html page
    cvs add multizMz1Ma1Mbk0Mbr0.html
    cvs commit -m "Details page for multiz track" multizMz1Ma1Mbk0Mbr0.html

# DESCRIPTION PAGE ()

    ssh hgwdev
    # Write ~/kent/src/hg/makeDb/trackDb/archae/methMaze1/description.html
    chmod a+r ~/kent/src/hg/makeDb/trackDb/archae/methMaze1/description.html
    # Check it in.
    mkdir /gbdb/methMaze1/html
    ln -s /cluster/data/methMaze1/html/description.html /gbdb/methMaze1/html/

# GENBANK PROTEIN-CODING GENES ()

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

#genbank to genePred

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

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

# 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/methMaze1/bed/loweTrnaGene
    cd /cluster/data/methMaze1/bed/loweTrnaGene
    hgLoadBed -tab methMaze1 loweTrnaGene methMaze1-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/methMaze1/bed/loweSnoGene
    cd /cluster/data/methMaze1/bed/loweSnoGene
    hgLoadBed -tab methMaze1 loweSnoGene methMaze1-snos.bed

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