# CHAIN TRACK with haloHalo1
# DONE (10/14/05), kpollard

    cd /cluster/data/halMar1/bed/
    mkdir conservation
    cd conservation
    cp /cluster/data/haloHalo1/bed/conservation/haloHalo1.fa .
    cp /cluster/data/pyrFur2/bed/conservation/HoxD55.q .
    cat ../../*.fa > halMar1.fa
    #fix headers
    cat halMar1.fa | gawk '{if(/halMar/){print $1"."$2;}else{print toupper($0);}}' > temp
    cat temp | sed s/plas/plasmid/ > temp2
    cat temp2 | sed s/chrom/chr/ > halMar1.fa
    rm temp temp2
    #split
    faSplit byname halMar1.fa ./
    faToTwoBit haloHalo1.fa haloHalo1.2bit
    faToTwoBit halMar1.fa halMar1.2bit
    foreach f (halMar1.*.fa)
	set b=$f:t:r
    	faToNib $f $b.nib
    end
    faSize -detailed halMar1.fa haloHalo1.fa > chrom.sizes

    #blastz
    foreach f (halMar1.*.fa)
    	set b=$f:t:r
	echo $b
	blastz $f haloHalo1.fa Q=HoxD55.q > ${b}-haloHalo1.lav
    end

    foreach f (*.lav)
	set b=$f:t:r
	echo $b
	lavToAxt $f . haloHalo1.2bit $b.axt
	axtBest $b.axt all -winSize=500 -minScore=5000 ${b}-best.axt
    end	

    #chain
    cp /cluster/data/haloHalo1/bed/conservation/chicken.gap .
    mkdir chains
    foreach f (*best.axt)
	set a=$f:t:r
	set b=`basename $a -best`
	echo $b
	axtChain -scoreScheme=HoxD55.q -linearGap=chicken.gap \
                      -minScore=5000 $f halMar1.2bit haloHalo1.2bit \
                       chains/$b.chain
    end    
   
    #load chains
    #fix chroms to not have halMar1. in front
    grep ">" halMar1.fa | gawk '{print substr($1,10,length($1))}' > contigs.txt
    cd chains
    foreach c (`cat ../contigs.txt`)
	sed s/halMar1.$c/$c/ halMar1.${c}-haloHalo1.chain > temp
        mv temp halMar1.${c}-haloHalo1.chain
    	hgLoadChain halMar1 ${c}_chainHaloHalo1 halMar1.${c}-haloHalo1.chain
    end

    #load axt files for blastz track
    #fix chroms to not have halMar1. in front
    cd ..
    sed s/halMar1.// chrom.sizes > chrom.sizes.new 
    foreach c (`cat contigs.txt`)
	sed s/halMar1.$c/$c/ halMar1.${c}-haloHalo1-best.axt > temp
	mv temp halMar1.${c}-haloHalo1-best.axt
     	axtToPsl halMar1.${c}-haloHalo1-best.axt chrom.sizes.new chrom.sizes.new $c.psl
    	hgLoadPsl halMar1 -table=${c}_blastzHaloHalo1 $c.psl
    end

    #trackDb
    cd ~/kent/src/hg/makeDb/trackDb/archae/halMar1
    # track chainHaloHalo1
    # shortLabel $o_Organism Chain    
    # longLabel $o_Organism ($o_date/$o_db) Chained Alignments                
    # group compGeno
    # priority 150.1
    # visibility hide
    # color 100,50,0
    # altColor 255,240,200
    # spectrum on
    # type chain haloHalo1
    # otherDb haloHalo1
    #
    # track BlastzHaloHalo1
    # shortLabel $o_Organism Blastz    
    # longLabel $o_Organism ($o_date/$o_db) Blastz Alignments                
    # group compGeno
    # priority 154.1
    # visibility hide
    # color 100,50,0
    # altColor 255,240,200
    # spectrum on
    # type psl xeno haloHalo1
    # otherDb haloHalo1
    cvs add trackDb.ra
    cvs commit -m "haloHalo1 chains and blastz" trackDb.ra
    cvs add chainHaloHalo1.html
    cvs commit -m "added haloHalo1 chains" chainHaloHalo1.html
    cvs add blastzHaloHalo1.html
    cvs commit -m "added haloHalo1 blastz" blastzHaloHalo1.html