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

# Anolis carolinensis 2.0 sequence:
# ftp.ncbi.nlm.nih.gov/genbank/genomes/Eukaryotes/vertebrates_other/
#	Anolis_carolinensis/

#	http://www.ncbi.nlm.nih.gov/Traces/wgs/?val=AAWZ00

##########################################################################
# Download sequence (DONE - 2011-04-12 - Hiram)
    mkdir -p /hive/data/genomes/anoCar2/genbank
    cd /hive/data/genomes/anoCar2/genbank
    wget --timestamping -r --cut-dirs=5 --level=0 -nH -x \
	--no-remove-listing -np \
"ftp://ftp.ncbi.nlm.nih.gov/genbank/genomes/Eukaryotes/vertebrates_other/Anolis_carolinensis/*"

###########################################################################
# fixup to UCSC names (DONE - 2011-04-12 - Hiram)
    cd /hive/data/genomes/anoCar2/genbank

    mkdir ucsc

    cat << '_EOF_' > ucscNames.pl
#!/bin/env perl

use strict;
use warnings;

my %cuToChr;

open (FH, "<assembled_chromosomes/chr2acc") or
	die "can not read assembled_chromosomes/chr2acc";
while (my $line = <FH>) {
    chomp $line;
    my ($chrN, $cuName) = split('\s+', $line);
    $cuToChr{$cuName} = $chrN;
    printf "%s\t%s\n", $chrN, $cuName;
    open (AG, "zcat assembled_chromosomes/AGP/chr${chrN}.comp.agp.gz|") or
	die "can not read assembled_chromosomes/AGP/chr${chrN}.comp.agp.gz";
    open (UC, "|gzip -c > ucsc/chr${chrN}.agp.gz") or
	die "can not write to ucsc/chr${chrN}.agp.gz";
    while (my $agpLine = <AG>) {
	if ($agpLine =~ m/^#/) {
	    print UC $agpLine;
	} else {
	    chomp $agpLine;
	    my ($cuId, $rest) = split('\s+', $agpLine, 2);
	    printf UC "chr%s\t%s\n", $cuToChr{$cuId}, $rest;
	}
    }
    close (AG);
    close (UC);
    print `zcat assembled_chromosomes/FASTA/chr${chrN}.fa.gz | sed -e "s#>.*#>chr${chrN}#" | gzip -c > ucsc/chr${chrN}.fa.gz`
}
close (FH);
'_EOF_'
    # << happy emacs
    chmod +x ucscNames.pl


    # fixup the unplaced names to make them chrUn_*
    cat << '_EOF_' > chrUn.pl
#!/bin/env perl

use strict;
use warnings;

my $agpFile =  "unplaced_scaffolds/AGP/unplaced.scaf.agp.gz";
my $fastaFile =  "unplaced_scaffolds/FASTA/unplaced.scaf.fa.gz";
open (FH, "zcat $agpFile|") or die "can not read $agpFile";
open (UC, "|gzip -c > ucsc/chrUn.agp") or die "can not write to ucsc/chrUn.agp";
while (my $line = <FH>) {
    if ($line =~ m/^#/) {
        print UC $line;
    } else {
        $line =~ s/\.1//;    
        printf UC "chrUn_%s", $line;
    }
}
close (FH);
close (UC);

open (FH, "zcat $fastaFile|") or die "can not read $fastaFile";
open (UC, "|gzip -c > ucsc/chrUn.fa") or die "can not write to ucsc/chrUn.fa";
while (my $line = <FH>) {
    if ($line =~ m/^>/) {
        chomp $line;
        $line =~ s/.*gb\|//;
        $line =~ s/\.1\|.*//;
        printf UC ">chrUn_$line\n";
    } else {
        print UC $line;
    }
}
close (FH);
close (UC);
'_EOF_'
    # << happy emacs
    chmod +x chrUn.pl

    time ./chrUn.pl
    #	real    3m13.579s

    # verify nothing was broken, should get the same numbers:
    faSize assembled_chromosomes/FASTA/chr*.fa.gz \
	unplaced_scaffolds/FASTA/unplaced.scaf.fa.gz
    #	1799126364 bases (97789817 N's 1701336547 real
    #	1701336547 upper 0 lower) in 6456 sequences in 14 files
    faSize ucsc/chr*.fa.gz
    #	1799126364 bases (97789817 N's 1701336547 real
    #	1701336547 upper 0 lower) in 6456 sequences in 14 files

###########################################################################
# Initial genome build (DONE - 2011-04-12 - Hiram)
    cd /hive/data/genomes/anoCar2

    cat << '_EOF_' > anoCar2.config.ra
# Config parameters for makeGenomeDb.pl:
db anoCar2
clade vertebrate
genomeCladePriority 70
scientificName Anolis carolinensis
commonName Lizard
assemblyDate May 2010
assemblyLabel Broad Institute of MIT and Harvard AnoCar 2.0 (GCA_000090745.1)
assemblyShortLabel Broad AnoCar2.0
orderKey 439
mitoAcc NC_010972.2
# same mito sequence in EU747728.2
fastaFiles /cluster/data/anoCar2/genbank/ucsc/chr*.fa.gz
agpFiles /cluster/data/anoCar2/genbank/ucsc/chr*.agp.gz
# qualFiles /cluster/data/anoCar2/genbank/scaffold.lifted.qac
dbDbSpeciesDir lizard
taxId 28377
'_EOF_'

    # run step wise to confirm sequence and AGP files match each other
    time nice -n +19 makeGenomeDb.pl -fileServer=hgwdev \
	-workhorse=hgwdev -stop=agp anoCar2.config.ra > agp.out 2>&1
    #	real    1m24.691s

    time nice -n +19 makeGenomeDb.pl -fileServer=hgwdev \
	-workhorse=hgwdev -continue=db anoCar2.config.ra > db.out 2>&1
    #	real    12m41.598s

    # add the trackDb business to the source tree

##########################################################################
# running repeat masker (DONE - 2011-04-12 - Hiram)
    mkdir /hive/data/genomes/anoCar2/bed/repeatMasker
    cd /hive/data/genomes/anoCar2/bed/repeatMasker
    time doRepeatMasker.pl -buildDir=`pwd` -noSplit \
	-bigClusterHub=swarm -dbHost=hgwdev -workhorse=hgwdev \
	-smallClusterHub=memk anoCar2 > do.log 2>&1 &
    #	real    35m31.331s
    cat faSize.rmsk.txt
# 1799143587 bases (97789820 N's 1701353767 real 1565234655
#	upper 136119112 lower) in 6457 sequences in 1 files
#	%7.57 masked total, %8.00 masked real

    grep -i versi do.log
# RepeatMasker version development-$Id: RepeatMasker,v 1.25 2010/09/08 21:32:26 angie Exp $
#    June 30 2010 (open-3-2-9) version of RepeatMasker

##########################################################################
# running simple repeat (DONE - 2011-04-12 - Hiram)
    mkdir /hive/data/genomes/anoCar2/bed/simpleRepeat
    cd /hive/data/genomes/anoCar2/bed/simpleRepeat
    time doSimpleRepeat.pl -buildDir=`pwd` -bigClusterHub=swarm \
	-dbHost=hgwdev -workhorse=hgwdev -smallClusterHub=memk \
	anoCar2 > do.log 2>&1 &
    #	real    23m18.960s

    #	real    198m33.953s
    cat fb.simpleRepeat 
    #	66211508 bases of 2752505800 (2.405%) in intersection

    cd /hive/data/genomes/anoCar2
    twoBitMask anoCar2.rmsk.2bit \
	-add bed/simpleRepeat/trfMask.bed anoCar2.2bit
    #	you can safely ignore the warning about fields >= 13

    twoBitToFa anoCar2.2bit stdout | faSize stdin > faSize.anoCar2.2bit.txt
    cat faSize.anoCar2.2bit.txt
# 2914958544 bases (162452744 N's 2752505800 real 1439244378 upper
#	1313261422 lower) in 14205 sequences in 1 files
#	%45.05 masked total, %47.71 masked real

    rm /gbdb/anoCar2/anoCar2.2bit
    ln -s `pwd`/anoCar2.2bit /gbdb/anoCar2/anoCar2.2bit

#########################################################################
# Verify all gaps are marked, add any N's not in gap as type 'other'
#	(DONE - 2011-04-13 - Hiram)
    mkdir /hive/data/genomes/anoCar2/bed/gap
    cd /hive/data/genomes/anoCar2/bed/gap
    time nice -n +19 findMotif -motif=gattaca -verbose=4 \
	-strand=+ ../../anoCar2.unmasked.2bit > findMotif.txt 2>&1
    #	real    0m40.949s
    grep "^#GAP " findMotif.txt | sed -e "s/^#GAP //" > allGaps.bed
    featureBits anoCar2 -not gap -bed=notGap.bed
    #	1701353770 bases of 1701353770 (100.000%) in intersection
    featureBits anoCar2 allGaps.bed notGap.bed -bed=new.gaps.bed
    #	3 bases of 1701353770 (0.000%) in intersection
    # not worth it:
    cat new.gaps.bed
chrM    9797    9798    chrM.1
chrM    15667   15668   chrM.2
chrM    15783   15784   chrM.3

##########################################################################
## WINDOWMASKER (WORKING - 2011-04-12 - Hiram)
    mkdir /hive/data/genomes/anoCar2/bed/windowMasker
    cd /hive/data/genomes/anoCar2/bed/windowMasker
    time nice -n +19 doWindowMasker.pl -buildDir=`pwd` -workhorse=hgwdev \
	-dbHost=hgwdev anoCar2 > do.log 2>&1 &
    #	real    116m26.778s

    # Masking statistics
    twoBitToFa anoCar2.wmsk.2bit stdout | faSize stdin
    #	1799143587 bases (97789820 N's 1701353767 real 996337045 upper
    #	705016722 lower) in 6457 sequences in 1 files
    #	%39.19 masked total, %41.44 masked real
    twoBitToFa anoCar2.wmsk.sdust.2bit stdout | faSize stdin
    #	1799143587 bases (97789820 N's 1701353767 real 987278282 upper
    #	714075485 lower) in 6457 sequences in 1 files
    #	%39.69 masked total, %41.97 masked real

    hgLoadBed anoCar2 windowmaskerSdust windowmasker.sdust.bed.gz
    #	Loaded 8198619 elements of size 3

    featureBits -countGaps anoCar2 windowmaskerSdust
    #	811865302 bases of 1799143587 (45.125%) in intersection

    #	eliminate the gaps from the masking
    featureBits anoCar2 -not gap -bed=notGap.bed
    #	1701353770 bases of 1701353770 (100.000%) in intersection
    time nice -n +19 featureBits anoCar2 windowmaskerSdust notGap.bed \
        -bed=stdout | gzip -c > cleanWMask.bed.gz
    #	714075485 bases of 1701353770 (41.971%) in intersection
    #	reload track to get it clean
    hgLoadBed anoCar2 windowmaskerSdust cleanWMask.bed.gz
    #	Loaded 8199653 elements of size 4
    featureBits -countGaps anoCar2 windowmaskerSdust
    #	714075485 bases of 1799143587 (39.690%) in intersection
    #	mask the sequence with this clean mask
    zcat cleanWMask.bed.gz \
	| twoBitMask ../../anoCar2.unmasked.2bit stdin \
	    -type=.bed anoCar2.cleanWMSdust.2bit
    twoBitToFa anoCar2.cleanWMSdust.2bit stdout | faSize stdin \
        > anoCar2.cleanWMSdust.faSize.txt
    cat anoCar2.cleanWMSdust.faSize.txt
    #	1799143587 bases (97789820 N's 1701353767 real 987278282 upper
    #	714075485 lower) in 6457 sequences in 1 files
    #	%39.69 masked total, %41.97 masked real

#########################################################################
# MASK SEQUENCE WITH WM+TRF (DONE - 2011-04-13 - Hiram)
    cd /hive/data/genomes/anoCar2
    twoBitMask -add bed/windowMasker/anoCar2.cleanWMSdust.2bit \
	bed/simpleRepeat/trfMask.bed anoCar2.2bit
    #	safe to ignore the warnings about BED file with >=13 fields
    twoBitToFa anoCar2.2bit stdout | faSize stdin > faSize.anoCar2.txt
    cat faSize.anoCar2.txt
    #	1799143587 bases (97789820 N's 1701353767 real 987044085 upper
    #	714309682 lower) in 6457 sequences in 1 files
    #	%39.70 masked total, %41.98 masked real

    #	create symlink to gbdb
    ssh hgwdev
    rm /gbdb/anoCar2/anoCar2.2bit
    ln -s `pwd`/anoCar2.2bit /gbdb/anoCar2/anoCar2.2bit

########################################################################
# MAKE 11.OOC FILE FOR BLAT/GENBANK (DONE - 2011-04-13 - Hiram)
    # Use -repMatch=650, based on size -- for human we use 1024
    # use the "real" number from the faSize measurement,
    # hg19 is 2897316137, calculate the ratio factor for 1024:
    calc \( 1701353767 / 2897316137 \) \* 1024
    #	( 1701353767 / 2897316137 ) * 1024 = 601.310377
    # round up to 650

    cd /hive/data/genomes/anoCar2
    blat anoCar2.2bit /dev/null /dev/null -tileSize=11 \
      -makeOoc=jkStuff/anoCar2.11.ooc -repMatch=650
    #	Wrote 27991 overused 11-mers to jkStuff/anoCar2.11.ooc

    #	copy all of this stuff to the klusters:
    cd /hive/data/genomes/anoCar2/jkStuff
    gapToLift anoCar2 nonBridged.lift -bedFile=nonBridged.bed
    cd /hive/data/genomes/anoCar2
    mkdir /hive/data/staging/data/anoCar2
    cp -p jkStuff/anoCar2.11.ooc jkStuff/nonBridged.lift chrom.sizes \
	anoCar2.2bit /hive/data/staging/data/anoCar2

#########################################################################
# AUTO UPDATE GENBANK (DONE - 2011-04-13,14 - Hiram)
    # examine the file:
    /cluster/data/genbank/data/organism.lst
    # for your species to see what counts it has for:
# organism       mrnaCnt estCnt  refSeqCnt
# Anolis carolinensis     115     156803  0
    # to decide which "native" mrna or ests you want to specify in genbank.conf
    # this appears that anoCar2 has plenty of native est's

    ssh hgwdev  
    cd $HOME/kent/src/hg/makeDb/genbank
    git pull
    # edit etc/genbank.conf to add anoCar2 before anoCar1 and commit to GIT
# anoCar2 (A. carolinensis)
anoCar2.serverGenome = /hive/data/genomes/anoCar2/anoCar2.2bit
anoCar2.clusterGenome = /scratch/data/anoCar2/anoCar2.2bit
anoCar2.ooc = /scratch/data/anoCar2/anoCar2.11.ooc
anoCar2.lift = /scratch/data/anoCar2/nonBridged.lift
anoCar2.refseq.mrna.native.pslCDnaFilter  = ${lowCover.refseq.mrna.native.pslCDnaFilter}
anoCar2.refseq.mrna.xeno.pslCDnaFilter    = ${lowCover.refseq.mrna.xeno.pslCDnaFilter}
anoCar2.genbank.mrna.native.pslCDnaFilter = ${lowCover.genbank.mrna.native.pslCDnaFilter}
anoCar2.genbank.mrna.xeno.pslCDnaFilter   = ${lowCover.genbank.mrna.xeno.pslCDnaFilter}
anoCar2.genbank.est.native.pslCDnaFilter  = ${lowCover.genbank.est.native.pslCDnaFilter}
anoCar2.refseq.mrna.native.load = no
anoCar2.genbank.est.native.load = yes
anoCar2.refseq.mrna.xeno.load = yes
anoCar2.genbank.mrna.xeno.load = yes
anoCar2.downloadDir = anoCar2
anoCar2.perChromTables = no


    # end of section added to etc/genbank.conf
    git commit -m "adding anoCar2 lizard" genbank.conf
    git push
    make etc-update

    # ~/kent/src/hg/makeDb/genbank/src/lib/gbGenome.c already contains
    # anoCar genome information, if this is a new species, need to add stuff
    # there

    ssh hgwdev			# used to do this on "genbank" machine
    screen			# long running job managed in screen
    cd /cluster/data/genbank
    time nice -n +19 ./bin/gbAlignStep -initial anoCar2 &
    #	var/build/logs/2011.04.13-21:34:22.anoCar2.initalign.log
    #	real    805m44.341s
    # 5h33m

    # load database when finished
    ssh hgwdev
    cd /cluster/data/genbank
    time nice -n +19 ./bin/gbDbLoadStep -drop -initialLoad anoCar2 &
    #	logFile: var/dbload/hgwdev/logs/2011.04.14-12:33:34.dbload.log
    #	real    24m24.093s

    # enable daily alignment and update of hgwdev (DONE - 2011-04-14 - Hiram)
    cd ~/kent/src/hg/makeDb/genbank
    git pull
    # add anoCar2 to:
        etc/align.dbs
        etc/hgwdev.dbs
    git commit -m "Added anoCar2." etc/align.dbs etc/hgwdev.dbs
    git push
    make etc-update

###########################################################################
# construct downloads files (DONE - 2011-04-14 - Hiram)
    cd /hive/data/genomes/anoCar2
    makeDownloads.pl -dbHost=hgwdev -workhorse=hgwdev anoCar2 \
	> downloads.log 2>&1
    # edit the README.txt files to set project URL and check
    #	the text

###########################################################################
###########################################################################
# ready for first pushQ entry (DONE - 2011-04-14 - Hiram)
    mkdir /hive/data/genomes/anoCar2/pushQ
    cd /hive/data/genomes/anoCar2/pushQ
    makePushQSql.pl anoCar2 > anoCar2.sql 2> stderr.out
    # some errors are legitimate and OK:
     head stderr.out 
# WARNING: hgwdev does not have /gbdb/anoCar2/wib/gc5Base.wib
# WARNING: hgwdev does not have /gbdb/anoCar2/wib/quality.wib
# WARNING: hgwdev does not have /gbdb/anoCar2/bbi/quality.bw
# WARNING: anoCar2 does not have seq
# WARNING: anoCar2 does not have extFile
    scp -p anoCar2.sql hgwbeta:/tmp
    ssh hgwbeta
    cd /tmp
    hgsql qapushq < anoCar2.sql

###########################################################################
#  anoCar2 - Lizard - Ensembl Genes version 61  (DONE - 2011-04-19 - hiram)
    ssh hgwdev
    cd /hive/data/genomes/anoCar2
    cat << '_EOF_' > anoCar2.ensGene.ra
# required db variable
db anoCar2
# optional nameTranslation, the sed command that will transform
#       Ensemble names to UCSC names.  With quotes just to make sure.
nameTranslation 's/^\([0-9L]\)/chr\1/; s/^GL\([0-9][0-9]*\).1/chrUn_GL\1/; s/^A/chrUn_A/'
'_EOF_'
#  << happy emacs

    doEnsGeneUpdate.pl  -ensVersion=61 anoCar2.ensGene.ra
    ssh hgwdev
    cd /hive/data/genomes/anoCar2/bed/ensGene.61
    featureBits anoCar2 ensGene
    # 27905138 bases of 1701353770 (1.640%) in intersection

    hgsql -e \
'update trackVersion set dateReference="current" where db="anoCar2";' hgFixed

############################################################################
# Construct UCSC to Ensembl name translation (DONE - 2011-04-19 - Hiram)
    mkdir /hive/data/genomes/anoCar2/bed/ucscToEnsembl
    cd /hive/data/genomes/anoCar2/bed/ucscToEnsembl
    cut -f1 ../../chrom.sizes | while read C
do
    echo -e -n "${C}\t"
    echo "${C}" | sed -e "s/^chr//; s/Un_//; s/^\(GL.*\)/\1.1/; s/^M/Mt/"
done | sort > ucscToEnsembl.tab

    cat << '_EOF_' > ucscToEnsembl.sql
# UCSC to Ensembl chr name translation
CREATE TABLE ucscToEnsembl (
    ucsc varchar(255) not null,        # UCSC chromosome name
    ensembl varchar(255) not null,     # Ensembl chromosome name
              #Indices
    PRIMARY KEY(ucsc(21))
);
'_EOF_'

    hgsql anoCar2 < ucscToEnsembl.sql
    hgsql anoCar2 \
-e 'LOAD DATA LOCAL INFILE "ucscToEnsembl.tab" INTO TABLE ucscToEnsembl'

############################################################################
# lastz alignment with Human/hg19 (DONE - 2004-04-19 - Hiram)
    #	the original alignment
    cd /hive/data/genomes/hg19/bed/lastzAnoCar2.2011-04-19
    cat fb.hg19.chainAnoCar2Link.txt 
    #	102917023 bases of 2897316137 (3.552%) in intersection

    #	running the swap
    mkdir /hive/data/genomes/anoCar2/bed/blastz.hg19.swap
    cd /hive/data/genomes/anoCar2/bed/blastz.hg19.swap
    time nice -n +19 doBlastzChainNet.pl -verbose=2 \
	/hive/data/genomes/hg19/bed/lastzAnoCar2.2011-04-19/DEF \
	-noLoadChainSplit -chainMinScore=5000 -chainLinearGap=loose \
	-workhorse=hgwdev -smallClusterHub=encodek -bigClusterHub=swarm \
	-syntenicNet -swap -qRepeats=windowmaskerSdust > swap.log 2>&1 &
    #	real    20m45.683s
    cat fb.anoCar2.chainHg19Link.txt 
    #	88296392 bases of 1701353770 (5.190%) in intersection

############################################################################
#	After getting a blat server assigned by the Blat Server Gods,
    ssh hgwdev

    hgsql -e 'INSERT INTO blatServers (db, host, port, isTrans, canPcr) \
	VALUES ("anoCar2", "blat2", "17780", "1", "0"); \
	INSERT INTO blatServers (db, host, port, isTrans, canPcr) \
	VALUES ("anoCar2", "blat2", "17781", "0", "1");' \
	    hgcentraltest
    #	test it with some sequence

#########################################################################
## Default position set at LFNG (DONE - 2011-04-19 - Hiram)
    ssh hgwdev
    hgsql -e 'update dbDb set defaultPos="chr5:23430070-23476097"
	where name="anoCar2";' hgcentraltest

#########################################################################
#  anoCar2 - Lizard - Ensembl Genes version 62 (DONE - 2011-04-19 - hiram)
    ssh hgwdev
    cd /hive/data/genomes/anoCar2
    cat << '_EOF_' > anoCar2.ensGene.ra
# required db variable
db anoCar2
# optional nameTranslation, the sed command that will transform
#       Ensemble names to UCSC names.  With quotes just to make sure.
nameTranslation 's/^\([0-9L]\)/chr\1/; s/^GL\([0-9][0-9]*\).1/chrUn_GL\1/; s/^A/chrUn_A/'
'_EOF_'
#  << happy emacs

    doEnsGeneUpdate.pl  -ensVersion=62 anoCar2.ensGene.ra
    ssh hgwdev
    cd /hive/data/genomes/anoCar2/bed/ensGene.62
    featureBits anoCar2 ensGene
    # 27905138 bases of 1701353770 (1.640%) in intersection

    hgsql -e \
'update trackVersion set dateReference="current" where db="anoCar2" AND version=62;' hgFixed

    hgsql -e \
'update trackVersion set dateReference="feb2011" where db="anoCar2" AND version=61;' hgFixed

############################################################################
# LASTZ Frog X. Tropicalis AnoCar2 (DONE - 2011-04-25 - Hiram)
    mkdir /hive/data/genomes/anoCar2/bed/lastzXenTro2.2011-04-25
    cd /hive/data/genomes/anoCar2/bed/lastzXenTro2.2011-04-25

    cat << '_EOF_' > DEF
# lizard vs frog
BLASTZ_H=2000
BLASTZ_Y=3400
BLASTZ_L=6000
BLASTZ_K=2200
BLASTZ_Q=/scratch/data/blastz/HoxD55.q

# TARGET: Lizard anoCar2
SEQ1_DIR=/scratch/data/anoCar2/anoCar2.2bit
SEQ1_LEN=/scratch/data/anoCar2/chrom.sizes
SEQ1_CHUNK=10000000
SEQ1_LAP=10000

# QUERY: Frog xenTro2
SEQ2_DIR=/scratch/data/xenTro2/xenTro2.2bit
SEQ2_LEN=/scratch/data/xenTro2/chrom.sizes
SEQ2_CHUNK=10000000
SEQ2_LAP=0
SEQ2_LIMIT=100

BASE=/hive/data/genomes/anoCar2/bed/lastzXenTro2.2011-04-25
TMPDIR=/scratch/tmp
'_EOF_'
    # << happy emacs

    #	establish a screen to control this job
    screen
    time nice -n +25 doBlastzChainNet.pl -verbose=2 \
	`pwd`/DEF \
	-noLoadChainSplit -chainMinScore=5000 -chainLinearGap=loose \
	-syntenicNet -workhorse=hgwdev -smallClusterHub=encodek \
	-bigClusterHub=swarm -tRepeats=windowmaskerSdust \
	-qRepeats=windowmaskerSdust > do.log 2>&1 &
    #	real    598m18.622s
    cat fb.anoCar2.chainXenTro2Link.txt 
    #	85962319 bases of 1701353770 (5.053%) in intersection

    #	running the swap - DONE - 2011-04-26
    mkdir /hive/data/genomes/xenTro2/bed/blastz.anoCar2.swap
    cd /hive/data/genomes/xenTro2/bed/blastz.anoCar2.swap
    time nice -n +25 doBlastzChainNet.pl -verbose=2 \
	/hive/data/genomes/anoCar2/bed/lastzXenTro2.2011-04-25/DEF \
	-noLoadChainSplit -chainMinScore=5000 -chainLinearGap=loose \
	-workhorse=hgwdev -smallClusterHub=encodek -bigClusterHub=swarm \
	-syntenicNet -swap -qRepeats=windowmaskerSdust \
	-tRepeats=windowmaskerSdust > swap.log 2>&1 &
    #	real    29m36.856s
    cat fb.xenTro2.chainAnoCar2Link.txt 
    #	91934327 bases of 1359412157 (6.763%) in intersection

##############################################################################
# lastz Mouse Mm9 (DONE - 2011-04-26 - Hiram)
    # original alignment
    cd /hive/data/genomes/mm9/bed/lastzAnoCar2.2011-04-19
    cat fb.mm9.chainAnoCar2Link.txt 
    #	88067954 bases of 2620346127 (3.361%) in intersection

    #	running the swap - DONE - 2011-04-19
    mkdir /hive/data/genomes/anoCar2/bed/blastz.mm9.swap
    cd /hive/data/genomes/anoCar2/bed/blastz.mm9.swap
    time nice -n +19 doBlastzChainNet.pl -verbose=2 \
	/hive/data/genomes/mm9/bed/lastzAnoCar2.2011-04-19/DEF \
	-noLoadChainSplit -chainMinScore=5000 -chainLinearGap=loose \
	-workhorse=hgwdev -smallClusterHub=encodek -bigClusterHub=swarm \
	-syntenicNet -swap -qRepeats=windowmaskerSdust > swap.log 2>&1 &
    #	real    14m28.747s
    cat fb.anoCar2.chainMm9Link.txt 
    #	84738440 bases of 1701353770 (4.981%) in intersection

##############################################################################
# HUMAN (hg18) PROTEINS TRACK (DONE 2011-04-27 braney )
    # bash  if not using bash shell already
    cd /cluster/data/anoCar2
    mkdir /cluster/data/anoCar2/blastDb

    awk '{if ($2 > 1000000) print $1}' chrom.sizes > 1meg.lst
    twoBitToFa -seqList=1meg.lst  anoCar2.2bit temp.fa
    faSplit gap temp.fa 1000000 blastDb/x -lift=blastDb.lft
# 1869 pieces of 1869 written
    rm temp.fa 1meg.lst

    awk '{if ($2 <= 1000000) print $1}' chrom.sizes > less1meg.lst
    twoBitToFa -seqList=less1meg.lst  anoCar2.2bit temp.fa
    faSplit about temp.fa 1000000 blastDb/y 
    rm temp.fa less1meg.lst

    cd blastDb
    for i in *.fa
    do
	/hive/data/outside/blast229/formatdb -i $i -p F
    done
    rm *.fa
    ls *.nsq | wc -l
# 2133

    mkdir -p /cluster/data/anoCar2/bed/tblastn.hg18KG
    cd /cluster/data/anoCar2/bed/tblastn.hg18KG
    echo  ../../blastDb/*.nsq | xargs ls -S | sed "s/\.nsq//"  > query.lst
    wc -l query.lst
# 2133 query.lst

   # we want around 250000 jobs
   calc `wc /cluster/data/hg18/bed/blat.hg18KG/hg18KG.psl | awk '{print $1}'`/\(250000/`wc query.lst | awk '{print $1}'`\)
# 36727/(250000/2133) = 313.354764

   mkdir -p kgfa
   split -l 313 /cluster/data/hg18/bed/blat.hg18KG/hg18KG.psl  kgfa/kg
   cd kgfa
   for i in *; do 
     nice pslxToFa $i $i.fa; 
     rm $i; 
   done
   cd ..
   ls -1S kgfa/*.fa > kg.lst
   wc kg.lst
#  118  118 1534 kg.lst

   mkdir -p blastOut
   for i in `cat kg.lst`; do  mkdir blastOut/`basename $i .fa`; done
   tcsh
   cd /cluster/data/anoCar2/bed/tblastn.hg18KG
   cat << '_EOF_' > blastGsub
#LOOP
blastSome $(path1) {check in line $(path2)} {check out exists blastOut/$(root2)/q.$(root1).psl }
#ENDLOOP
'_EOF_'

   cat << '_EOF_' > blastSome
#!/bin/sh
BLASTMAT=/hive/data/outside/blast229/data
export BLASTMAT
g=`basename $2`
f=/tmp/`basename $3`.$g
for eVal in 0.01 0.001 0.0001 0.00001 0.000001 1E-09 1E-11
do
if /hive/data/outside/blast229/blastall -M BLOSUM80 -m 0 -F no -e $eVal -p tblastn -d $1 -i $2 -o $f.8
then
        mv $f.8 $f.1
        break;
fi
done
if test -f  $f.1
then
    if /cluster/bin/i386/blastToPsl $f.1 $f.2
    then
	liftUp -nosort -type=".psl" -nohead $f.3 /cluster/data/anoCar2/blastDb.lft carry $f.2
        liftUp -nosort -type=".psl" -pslQ -nohead $3.tmp /cluster/data/hg18/bed/blat.hg18KG/protein.lft warn $f.3
        if pslCheck -prot $3.tmp
        then                  
            mv $3.tmp $3     
            rm -f $f.1 $f.2 $f.3 $f.4
        fi
        exit 0               
    fi                      
fi                         
rm -f $f.1 $f.2 $3.tmp $f.8 $f.3 $f.4
exit 1
'_EOF_'
    # << happy emacs
    chmod +x blastSome
    exit 
    
    ssh swarm
    cd /cluster/data/anoCar2/bed/tblastn.hg18KG
    gensub2 query.lst kg.lst blastGsub blastSpec
    para create blastSpec
#    para try, check, push, check etc.

    para time

# Completed: 251694 of 251694 jobs
# CPU time in finished jobs:    9960850s  166014.17m  2766.90h  115.29d  0.316 y
# IO & Wait Time:               1417048s   23617.47m   393.62h   16.40d  0.045 y
# Average job time:                  45s       0.75m     0.01h    0.00d
# Longest finished job:             207s       3.45m     0.06h    0.00d
# Submission to last job:         17381s     289.68m     4.83h    0.20d

    ssh swarm
    cd /cluster/data/anoCar2/bed/tblastn.hg18KG
    mkdir chainRun
    cd chainRun
    tcsh
    cat << '_EOF_' > chainGsub
#LOOP
chainOne $(path1)
#ENDLOOP
'_EOF_'

    cat << '_EOF_' > chainOne
(cd $1; cat q.*.psl | simpleChain -prot -outPsl -maxGap=150000 stdin ../c.`basename $1`.psl)
'_EOF_'
    chmod +x chainOne
    ls -1dS ../blastOut/kg?? > chain.lst
    gensub2 chain.lst single chainGsub chainSpec
    # do the cluster run for chaining
    para create chainSpec
    para try, check, push, check etc.

# Completed: 118 of 118 jobs
# CPU time in finished jobs:     116643s    1944.05m    32.40h    1.35d  0.004 y
# IO & Wait Time:                 54371s     906.19m    15.10h    0.63d  0.002 y
# Average job time:                1449s      24.15m     0.40h    0.02d
# Longest finished job:            6307s     105.12m     1.75h    0.07d
# Submission to last job:          6319s     105.32m     1.76h    0.07d

    cd /cluster/data/anoCar2/bed/tblastn.hg18KG/blastOut
    for i in kg??
    do
       cat c.$i.psl | awk "(\$13 - \$12)/\$11 > 0.6 {print}" > c60.$i.psl
       sort -rn c60.$i.psl | pslUniq stdin u.$i.psl
       awk "((\$1 / \$11) ) > 0.60 { print   }" c60.$i.psl > m60.$i.psl
       echo $i
    done
    sort u.*.psl m60* | uniq | sort -T /tmp -k 14,14 -k 16,16n -k 17,17n > ../blastHg18KG.psl
    cd ..
    pslCheck blastHg18KG.psl
# checked: 47362 failed: 0 errors: 0

    # load table 
    ssh hgwdev
    cd /cluster/data/anoCar2/bed/tblastn.hg18KG
    hgLoadPsl anoCar2 blastHg18KG.psl

    # check coverage
    featureBits anoCar2 blastHg18KG 
# 21485462 bases of 1701353770 (1.263%) in intersection

    featureBits anoCar2 blastHg18KG ensGene  -enrichment
# blastHg18KG 1.263%, ensGene 1.640%, both 1.114%, cover 88.23%, enrich 53.80x

    rm -rf blastOut
#end tblastn
###################################
# lastz Chicken GalGal3 (DONE - 2011-04-26 - Hiram)
    # original alignment
    cd /hive/data/genomes/galGal3/bed/lastzAnoCar2.2011-04-25
    cat fb.galGal3.chainAnoCar2Link.txt 
    #	113851392 bases of 1042591351 (10.920%) in intersection

    #	running the swap - DONE - 2011-04-26
    mkdir /hive/data/genomes/anoCar2/bed/blastz.galGal3.swap
    cd /hive/data/genomes/anoCar2/bed/blastz.galGal3.swap
    time nice -n +25 doBlastzChainNet.pl -verbose=2 \
	/hive/data/genomes/galGal3/bed/lastzAnoCar2.2011-04-25/DEF \
	-noLoadChainSplit -chainMinScore=5000 -chainLinearGap=loose \
	-workhorse=hgwdev -smallClusterHub=encodek -bigClusterHub=swarm \
	-syntenicNet -swap -qRepeats=windowmaskerSdust \
	-tRepeats=windowmaskerSdust > swap.log 2>&1 &
    #	real    11m8.229s
    cat fb.anoCar2.chainGalGal3Link.txt 
    #	116069635 bases of 1701353770 (6.822%) in intersection

##############################################################################
## LASTZ Stickleback GasAcu1 - (DONE - 2011-04-26 - Hiram)
    mkdir /cluster/data/anoCar2/bed/lastzGasAcu1.2011-04-26
    cd /cluster/data/anoCar2/bed/lastzGasAcu1.2011-04-26

    cat << '_EOF_' > DEF
# lizard vs stickleback
BLASTZ_H=2000
BLASTZ_Y=3400
BLASTZ_L=6000
BLASTZ_K=2200
BLASTZ_Q=/scratch/data/blastz/HoxD55.q

# TARGET: Lizard anoCar2
SEQ1_DIR=/scratch/data/anoCar2/anoCar2.2bit
SEQ1_LEN=/scratch/data/anoCar2/chrom.sizes
SEQ1_CHUNK=10000000
SEQ1_LAP=10000

# Query: Stickleback gasAcu1
SEQ2_DIR=/scratch/data/gasAcu1/gasAcu1.2bit
SEQ2_LEN=/scratch/data/gasAcu1/chrom.sizes
SEQ2_CHUNK=10000000
SEQ2_LAP=0
SEQ2_LIMIT=10

BASE=/cluster/data/anoCar2/bed/lastzGasAcu1.2011-04-26
TMPDIR=/scratch/tmp
'_EOF_'
    # << happy emacs

    time nice -n +25 doBlastzChainNet.pl -verbose=2 \
	`pwd`/DEF \
	-noLoadChainSplit -chainMinScore=5000 -chainLinearGap=loose \
	-syntenicNet -workhorse=hgwdev -smallClusterHub=encodek \
	-bigClusterHub=swarm -tRepeats=windowmaskerSdust \
	-qRepeats=windowmaskerSdust > do.log 2>&1 &
    #	real    277m57.896s
    cat fb.anoCar2.chainGasAcu1Link.txt 
    #	53478872 bases of 1701353770 (3.143%) in intersection

    ## and running the swap
    mkdir /hive/data/genomes/gasAcu1/bed/blastz.anoCar2.swap
    cd /hive/data/genomes/gasAcu1/bed/blastz.anoCar2.swap
    time nice -n +25 doBlastzChainNet.pl -verbose=2 \
	/cluster/data/anoCar2/bed/lastzGasAcu1.2011-04-26/DEF \
	-noLoadChainSplit -chainMinScore=5000 -chainLinearGap=loose \
	-syntenicNet -workhorse=hgwdev -smallClusterHub=encodek \
	-bigClusterHub=swarm -tRepeats=windowmaskerSdust \
	-swap -qRepeats=windowmaskerSdust > swap.log 2>&1 &
    #	real    15m28.724s
    cat fb.gasAcu1.chainAnoCar2Link.txt 
    #	55990919 bases of 446627861 (12.536%) in intersection

#######################################################################
## LASTZ Platypus OrnAna1 - (DONE - 2011-04-26 - Hiram)
    mkdir /cluster/data/anoCar2/bed/lastzOrnAna1.2011-04-26
    cd /cluster/data/anoCar2/bed/lastzOrnAna1.2011-04-26

    cat << '_EOF_' > DEF
# lizard vs stickleback
BLASTZ_H=2000
BLASTZ_Y=3400
BLASTZ_L=6000
BLASTZ_K=2200
BLASTZ_Q=/scratch/data/blastz/HoxD55.q

# TARGET: Lizard anoCar2
SEQ1_DIR=/scratch/data/anoCar2/anoCar2.2bit
SEQ1_LEN=/scratch/data/anoCar2/chrom.sizes
SEQ1_CHUNK=10000000
SEQ1_LAP=10000

# Query: Stickleback ornAna1
SEQ2_DIR=/scratch/data/ornAna1/ornAna1.2bit
SEQ2_LEN=/scratch/data/ornAna1/chrom.sizes
SEQ2_CHUNK=10000000
SEQ2_LAP=0
SEQ2_LIMIT=300

BASE=/cluster/data/anoCar2/bed/lastzOrnAna1.2011-04-26
TMPDIR=/scratch/tmp
'_EOF_'
    # << happy emacs

    time nice -n +25 doBlastzChainNet.pl -verbose=2 \
	`pwd`/DEF \
	-noLoadChainSplit -chainMinScore=5000 -chainLinearGap=loose \
	-syntenicNet -workhorse=hgwdev -smallClusterHub=encodek \
	-bigClusterHub=swarm -tRepeats=windowmaskerSdust > do.log 2>&1 &
    #	real    632m47.014s
    cat fb.anoCar2.chainOrnAna1Link.txt 
    #	77594222 bases of 1701353770 (4.561%) in intersection

    ## running the swap
    mkdir /hive/data/genomes/ornAna1/bed/blastz.anoCar2.swap
    cd /hive/data/genomes/ornAna1/bed/blastz.anoCar2.swap
    time nice -n +25 doBlastzChainNet.pl -verbose=2 \
	/cluster/data/anoCar2/bed/lastzOrnAna1.2011-04-26/DEF \
	-noLoadChainSplit -chainMinScore=5000 -chainLinearGap=loose \
	-syntenicNet -workhorse=hgwdev -smallClusterHub=encodek \
	-swap -bigClusterHub=swarm -tRepeats=windowmaskerSdust > swap.log 2>&1 &
    #	real    40m51.778s
    cat fb.ornAna1.chainAnoCar2Link.txt 
    #	75402821 bases of 1842236818 (4.093%) in intersection

#######################################################################
## 7-Way Multiz (DONE - 2011-05-13 - Hiram)
    ssh hgwdev
    mkdir /hive/data/genomes/anoCar2/bed/multiz7way
    cd /hive/data/genomes/anoCar2/bed/multiz7way

    # working with: anoCar2 xenTro2 galGal3 mm9 hg19 gasAcu1 ornAna1

kent/src/hg/utils/phyloTrees/50way.nh
    anoCar2,xenTro2,galGal3,mm9,hg19,gasAcu1,ornAna1

    #	All distances remain as specified in the 46way.nh
    /cluster/bin/phast/tree_doctor --prune-all-but \
	anoCar1,xenTro2,galGal3,mm9,hg19,gasAcu1,ornAna1 \
	$HOME/kent/src/hg/utils/phyloTrees/50way.nh \
	| sed -e "s/anoCar1/anoCar2/" > 7way.nh
    #	what that looks like:
    cat 7way.nh
# (((((hg19:0.144018,mm9:0.356483):0.350649,ornAna1:0.456592):0.109504,(galGal3:0.464759,anoCar2:0.489241):0.105143):0.172371,xenTro2:0.855573):0.311354,gasAcu1:0.849152);

    cat << '_EOF_' > anoCar2.7way.nh
((((anoCar2:0.489241,galGal3:0.464759):0.105143,
((hg19:0.144018,mm9:0.356483):0.350649,ornAna1:0.456592):0.109504):0.172371,
xenTro2:0.855573):0.311354,gasAcu1:0.849152);
'_EOF_'
    # << happy emacs

    # convert to species names
    /cluster/bin/phast/tree_doctor --rename \
"anoCar2->Lizard; hg19->Human; xenTro2->X_tropicalis; gasAcu1->Stickleback; ornAna1->Platypus; mm9->Mouse; galGal3->Chicken" \
anoCar2.7way.nh > anoCar2.commonNames.7way.nh

# ((((Lizard:0.489241,Chicken:0.464759):0.105143,((Human:0.144018,Mouse:0.356483):0.350649,Platypus:0.456592):0.109504):0.172371,X._tropicalis:0.855573):0.311354,Stickleback:0.849152);

    #	Use this specification in the phyloGif tool:
    #	http://genome.ucsc.edu/cgi-bin/phyloGif
    #	to obtain a png image for src/hg/htdocs/images/phylo/anoCar2_7way.png

    /cluster/bin/phast/all_dists anoCar2.7way.nh > 7way.distances.txt
    #	Use this output to create the table below
    grep -i anoCar2 7way.distances.txt | sort -k3,3n
# anoCar2 galGal3 0.954000
# anoCar2 ornAna1 1.160480
# anoCar2 hg19    1.198555
# anoCar2 mm9     1.411020
# anoCar2 xenTro2 1.622328
# anoCar2 gasAcu1 1.927261

#	If you can fill in all the numbers in this table, you are ready for
#	the multiple alignment procedure

#                         featureBits chainLink measures
#                                   chainAnoCar2Link         chain linearGap
#    distance                    on anoCar2  on other     minScore
#  1  0.954 - chicken galGal3     (%  6.82) (% 10.92)       5000     loose
#  2  1.160 - platypus ornAna1    (%  4.56) (%  4.09)       5000     loose
#  3  1.199 - human hg19          (%  5.19) (%  3.55)       5000     loose
#  4  1.411 - mouse mm9           (%  4.98) (%  3.36)       5000     loose
#  5  1.622 - X.tropicalis xenTro2(%  5.05) (%  6.76)       5000     loose
#  6  1.927 - stickleback gasAcu1 (%  3.14) (% 12.54)       5000     loose

# None of this concern for distances matters in building the first step, the
# maf files.

    # create species list and stripped down tree for autoMZ
    sed 's/[a-z][a-z]*_//g; s/:[0-9\.][0-9\.]*//g; s/;//; /^ *$/d' \
	anoCar2.7way.nh > tmp.nh
    echo `cat tmp.nh` > tree-commas.nh
    echo `cat tree-commas.nh` | sed 's/ //g; s/,/ /g' > tree.nh
    sed 's/[()]//g; s/,/ /g' tree.nh > species.list

    # split the maf files into a set of hashed named files
    # this hash named split keeps the same chr/contig names in the same
    # named hash file.
    mkdir /hive/data/genomes/anoCar2/bed/multiz7way/mafSplit
    cd /hive/data/genomes/anoCar2/bed/multiz7way/mafSplit
    for D in `sed -e "s/anoCar2 //" ../species.list`
do
    echo "${D}"
    mkdir $D
    cd $D
    echo "mafSplit -byTarget -useHashedName=10 /dev/null . ../../../lastz.${D}/axtChain/anoCar2.${D}.synNet.maf.gz"
    mafSplit -byTarget -useHashedName=8 /dev/null . \
	../../../lastz.${D}/mafNet/anoCar2.${D}.net.maf.gz
    cd ..
done

    # construct a list of all possible maf file names.
    # they do not all exist in each of the species directories
    find . -type f | wc -l
    # 1536
    find . -type f | grep ".maf$" | xargs -L 1 basename | sort -u > maf.list
    wc -l maf.list
    # 256 maf.list

    mkdir /hive/data/genomes/anoCar2/bed/multiz7way/splitRun
    cd /hive/data/genomes/anoCar2/bed/multiz7way/splitRun
    mkdir maf run
    cd run
    mkdir penn
    cp -p /cluster/bin/penn/multiz.2009-01-21/multiz penn
    cp -p /cluster/bin/penn/multiz.2009-01-21/maf_project penn
    cp -p /cluster/bin/penn/multiz.2009-01-21/autoMZ penn

    #	set the db and pairs directories here
    cat > autoMultiz.csh << '_EOF_'
#!/bin/csh -ef
set db = anoCar2
set c = $1
set result = $2
set run = `/bin/pwd`
set tmp = /scratch/tmp/$db/multiz.$c
set pairs = /hive/data/genomes/anoCar2/bed/multiz7way/mafSplit
/bin/rm -fr $tmp
/bin/mkdir -p $tmp
/bin/cp -p ../../tree.nh ../../species.list $tmp
pushd $tmp > /dev/null
foreach s (`/bin/sed -e "s/$db //" species.list`)
    set in = $pairs/$s/$c.maf
    set out = $db.$s.sing.maf
    if (-e $in.gz) then
        /bin/zcat $in.gz > $out
	if (! -s $out) then
	    echo "##maf version=1 scoring=autoMZ" > $out
	endif
    else if (-e $in) then
        /bin/ln -s $in $out
    else
        echo "##maf version=1 scoring=autoMZ" > $out
    endif
end
set path = ($run/penn $path); rehash
$run/penn/autoMZ + T=$tmp E=$db "`cat tree.nh`" $db.*.sing.maf $c.maf \
	> /dev/null
popd > /dev/null
/bin/rm -f $result
/bin/cp -p $tmp/$c.maf $result
/bin/rm -fr $tmp
/bin/rmdir --ignore-fail-on-non-empty /scratch/tmp/$db
'_EOF_'
# << happy emacs
    chmod +x autoMultiz.csh

    cat  << '_EOF_' > template
#LOOP
./autoMultiz.csh $(root1) {check out line+ /hive/data/genomes/anoCar2/bed/multiz7way/splitRun/maf/$(root1).maf}
#ENDLOOP
'_EOF_'
# << happy emacs

    ln -s ../../mafSplit/maf.list maf.list
    ssh swarm
    cd /hive/data/genomes/anoCar2/bed/multiz7way/splitRun/run
    gensub2 maf.list single template jobList
    tac jobList > smallJobsFirst
    para -ram=8g create smallJobsFirst
# Completed: 484 of 484 jobs
# CPU time in finished jobs:     370423s    6173.72m   102.90h    4.29d  0.012 y
# IO & Wait Time:                 27319s     455.32m     7.59h    0.32d  0.001 y
# Average job time:                 822s      13.70m     0.23h    0.01d
# Longest finished job:           31994s     533.23m     8.89h    0.37d
# Submission to last job:         32007s     533.45m     8.89h    0.37d

    # assemble into a single maf file
    cd /hive/data/genomes/anoCar2/bed/multiz7way
    head -1 splitRun/maf/001.maf > multiz7way.maf
    for F in splitRun/maf/*.maf
do
    egrep -v "^#" ${F}
done >> multiz7way.maf
    tail -1 splitRun/maf/001.maf >> multiz7way.maf

# -rw-rw-r-- 1 1237537614 May 13 16:13 multiz7way.maf

    # Load into database
    ssh hgwdev
    cd /hive/data/genomes/anoCar2/bed/multiz7way
    mkdir /gbdb/anoCar2/multiz7way
    ln -s `pwd`/multiz7way.maf /gbdb/anoCar2/multiz7way
    cd /scratch/tmp
    time nice -n +19 hgLoadMaf anoCar2 multiz7way
    #	Indexing and tabulating /gbdb/anoCar2/multiz7way/multiz7way.maf
    #	Loaded 2182259 mafs in 1 files from /gbdb/anoCar2/multiz7way
    #	real    0m48.824s

    time nice -n +19 hgLoadMafSummary -verbose=2 -minSize=30000 \
	-mergeGap=1500 -maxSize=200000 anoCar2 multiz7waySummary \
	/gbdb/anoCar2/multiz7way/multiz7way.maf
    #	Created 623115 summary blocks from 4606157 components and 2182259 mafs
    #	from /gbdb/anoCar2/multiz7way/multiz7way.maf
    #	real    0m38.445s

    wc -l multiz7way*.tab
    #	2182259 multiz7way.tab
    #	623115 multiz7waySummary.tab
    #	2805374 total

    rm multiz7way*.tab

############################################################################
# GAP ANNOTATE MULTIZ7WAY MAF AND LOAD TABLES (DONE - 2011-05-16 - Hiram)
    # mafAddIRows has to be run on single chromosome maf files, it does not
    #	function correctly when more than one reference sequence
    #	are in a single file.
    mkdir /hive/data/genomes/anoCar2/bed/multiz7way/anno/mafSplit
    cd /hive/data/genomes/anoCar2/bed/multiz7way/anno/mafSplit
    time mafSplit -byTarget -useFullSequenceName \
	/dev/null . ../../multiz7way.maf
    #	real    0m49.816s
    ls | wc
    #	4423    4423   94331

    cd /hive/data/genomes/anoCar2/bed/multiz7way/anno

    # only one of these does not yet have an N.bed file
    cd /hive/data/genomes/danRer7
    twoBitInfo -nBed danRer7.2bit danRer7.N.bed
for DB in `sed -e "s/anoCar2 //" ../species.list`
do
    echo "${DB} "
    ln -s  /hive/data/genomes/${DB}/${DB}.N.bed ${DB}.bed
    echo ${DB}.bed  >> nBeds
    ln -s  /hive/data/genomes/${DB}/chrom.sizes ${DB}.len
    echo ${DB}.len  >> sizes
done
    # make sure they all are successful symLinks:
    ls -ogrtL

    ssh swarm
    cd /hive/data/genomes/anoCar2/bed/multiz7way/anno
    mkdir result
    cat << '_EOF_' > template
#LOOP
mafAddIRows -nBeds=nBeds $(path1) /hive/data/genomes/anoCar2/anoCar2.2bit result/$(file1)
#ENDLOOP
'_EOF_'
    # << happy emacs

    ls mafSplit/*.maf > maf.list
    gensub2 maf.list single template jobList
    # limit jobs to one per node with the ram=8g requirement
    para -ram=8g create jobList
    para try ... check ... push ...
# Completed: 4423 of 4423 jobs
# CPU time in finished jobs:       2692s      44.87m     0.75h    0.03d  0.000 y
# IO & Wait Time:                 11343s     189.04m     3.15h    0.13d  0.000 y
# Average job time:                   3s       0.05m     0.00h    0.00d
# Longest finished job:             700s      11.67m     0.19h    0.01d
# Submission to last job:           702s      11.70m     0.20h    0.01d

    # verify all result files have some content, look for 0 size files:
    find . -type f -size 0
    # should see none

    # combine into one file
    head -q -n 1 result/chr1.maf > anoCar2.7way.maf
    for F in result/*.maf
do
    grep -h -v "^#" ${F}
done >> anoCar2.7way.maf
    #	these maf files do not have the end marker, this does nothing:
    #	tail -q -n 1 result/chr1.maf >> anoCar2.7way.maf
    # How about an official end marker:
    echo "##eof maf" >> anoCar2.7way.maf

    # Load into database
    rm /gbdb/anoCar2/multiz7way/multiz7way.maf   # remove old symlink
    ln -s `pwd`/anoCar2.7way.maf /gbdb/anoCar2/multiz7way/multiz7way.maf
    cd /scratch/tmp
    time nice -n +19 hgLoadMaf anoCar2 multiz7way
    #	Loaded 2552410 mafs in 1 files from /gbdb/anoCar2/multiz7way
    #	real    1m18.221s

    time nice -n +19 hgLoadMafSummary -verbose=2 -minSize=30000 \
	-mergeGap=1500 -maxSize=200000 anoCar2 multiz7waySummary \
	/gbdb/anoCar2/multiz7way/multiz7way.maf
    #	Created 623115 summary blocks from 4606157 components
    #	and 2552410 mafs from /gbdb/anoCar2/multiz7way/multiz7way.maf
    #	real    1m36.715s

    wc -l multiz7way*.tab
    #	2552410 multiz7way.tab
    #	623115 multiz7waySummary.tab
    #	3175525 total

    rm multiz7way*.tab

#########################################################################
# MULTIZ7WAY MAF FRAMES (DONE - 2011-05-16 - Hiram)
    ssh hgwdev
    mkdir /hive/data/genomes/anoCar2/bed/multiz7way/frames
    cd /hive/data/genomes/anoCar2/bed/multiz7way/frames
    mkdir genes

    #	we get more annotations using the ensGene on mm9 and hg19
    #	instead of knownGene, so, using ensGene for hg19 and mm9

# hg19 knownGene
# mm9 knownGene

# anoCar2 ensGene
# galGal3 ensGene
# ornAna1 ensGene
# xenTro2	ensGene
# gasAcu1	ensGene

    #------------------------------------------------------------------------
    # get the genes for all genomes
    # mRNAs with CDS.  single select to get cds+psl, then split that up and
    # create genePred
    # using ensGene for:
for qDB in hg19 mm9 anoCar2 galGal3 ornAna1 xenTro2 gasAcu1
do
  echo hgsql -N -e \"'select * from 'ensGene\;\" ${qDB}
      hgsql -N -e "select * from ensGene" ${qDB} | cut -f 2-11 \
      | genePredSingleCover stdin stdout | gzip -2c > genes/$qDB.gp.gz
done

    # if you were using knownGene for mm9 hg19
    # genePreds; (must keep only the first 10 columns for knownGene)
    for qDB in mm9 hg19
    do
      echo hgsql -N -e \"'select * from 'knownGene\;\" ${qDB}
      hgsql -N -e "select * from knownGene" ${qDB} | cut -f 1-10 \
      | genePredSingleCover stdin stdout | gzip -2c > genes/$qDB.gp.gz
    done

    # verify counts for genes are reasonable:
    for T in genes/*.gz
do
    echo -n "# $T: "
    zcat $T | cut -f1 | sort | uniq -c | wc -l
done

# genes/anoCar2.gp.gz: 17757
# genes/galGal3.gp.gz: 16491
# genes/gasAcu1.gp.gz: 20631
# genes/hg19.gp.gz: 21254
# genes/mm9.gp.gz: 22739
# genes/ornAna1.gp.gz: 17728
# genes/xenTro2.gp.gz: 17888

    ssh hgwdev
    cd /hive/data/genomes/anoCar2/bed/multiz7way/frames
    cat ../anno/anoCar2.7way.maf \
	| nice -n +19 genePredToMafFrames anoCar2 stdin stdout \
	    `sed -e "s#\([a-zA-Z0-9]*\)#\1 genes/\1.gp.gz#g" ../species.list` \
		| gzip > multiz7way.mafFrames.gz
    #	real    12m47.652s
    # verify there are frames on everything:
    zcat multiz7way.mafFrames.gz | awk '{print $4}' | sort | uniq -c
    #	171338 anoCar2
    #	231157 galGal3
    #	249046 gasAcu1
    #	299896 hg19
    #	291605 mm9
    #	229117 ornAna1
    #	218682 xenTro2

    ssh hgwdev
    cd /hive/data/genomes/anoCar2/bed/multiz7way/frames
    time hgLoadMafFrames anoCar2 multiz7wayFrames multiz7way.mafFrames.gz
    #	real    0m16.366s

#########################################################################
# Phylogenetic tree from 7-way (DONE - 2011-05-17 - Hiram)

    mkdir /hive/data/genomes/anoCar2/bed/multiz7way/4d
    cd /hive/data/genomes/anoCar2/bed/multiz7way/4d

    # the split annotated maf's are in:
    ../anno/result/*.maf

    cd /hive/data/genomes/anoCar2/bed/multiz7way/4d

    # using ensGene for anoCar2, only transcribed genes and nothing
    #	from the randoms and other misc.
    hgsql anoCar2 -Ne \
    "select * from ensGene WHERE cdsEnd > cdsStart;" | cut -f 2-20 | egrep -E -v "chrM|random|chrUn" \
	> ensGene.gp
    #	verify chromosome selection, should just be the ordinary chroms:
    cut -f2 ensGene.gp | sort | uniq -c
    wc -l *.gp
    #	9440 ensGene.gp

    genePredSingleCover ensGene.gp stdout | sort > ensGeneNR.gp
    wc -l ensGeneNR.gp
    #	8830 ensGeneNR.gp

    ssh encodek
    mkdir /hive/data/genomes/anoCar2/bed/multiz7way/4d/run
    cd /hive/data/genomes/anoCar2/bed/multiz7way/4d/run
    mkdir ../mfa

    # newer versions of msa_view have a slightly different operation
    # the sed of the gp file inserts the reference species in the chr name
    cat << '_EOF_' > 4d.csh
#!/bin/csh -fe
set PHASTBIN = /cluster/bin/phast.build/cornellCVS/phast.2010-12-30/bin
set r = "/hive/data/genomes/anoCar2/bed/multiz7way"
set c = $1
set infile = $r/anno/result/$2
set outfile = $3
cd /scratch/tmp
# 'clean' maf
perl -wpe 's/^s ([^.]+)\.\S+/s $1/' $infile > $c.maf    
awk -v C=$c '$2 == C {print}' $r/4d/ensGeneNR.gp | sed -e "s/\t$c\t/\tanoCar2.$c\t/" > $c.gp
set NL=`wc -l $c.gp| gawk '{print $1}'`
if ("$NL" != "0") then
    $PHASTBIN/msa_view --4d --features $c.gp -i MAF $c.maf -o SS > $c.ss
    $PHASTBIN/msa_view -i SS --tuple-size 1 $c.ss > $r/4d/run/$outfile
else
    echo "" > $r/4d/run/$outfile
endif
rm -f $c.gp $c.maf $c.ss
'_EOF_'
    # << happy emacs
    chmod +x 4d.csh

    ls -1S /hive/data/genomes/anoCar2/bed/multiz7way/anno/result/*.maf | \
	egrep -E -v "chrM|random|chrUn" \
	| sed -e "s#.*multiz7way/anno/result/##" \
	> maf.list
    # check that list, should be a small number of ordinary chroms

    cat << '_EOF_' > template
#LOOP
4d.csh $(root1) $(path1) {check out line+ ../mfa/$(root1).mfa}
#ENDLOOP
'_EOF_'
    # << happy emacs

    gensub2 maf.list single template stdout | tac > jobList
    para create jobList
    para try ... check
    para -maxJob=5 push
    para time
# Completed: 13 of 13 jobs
# CPU time in finished jobs:        275s       4.59m     0.08h    0.00d  0.000 y
# IO & Wait Time:                    35s       0.58m     0.01h    0.00d  0.000 y
# Average job time:                  24s       0.40m     0.01h    0.00d
# Longest finished job:              68s       1.13m     0.02h    0.00d
# Submission to last job:            88s       1.47m     0.02h    0.00d

    # combine mfa files
    ssh hgwdev
    cd /hive/data/genomes/anoCar2/bed/multiz7way/4d
    #want comma-less species.list
    /cluster/bin/phast.build/cornellCVS/phast.2010-12-30/bin/msa_view \
	--aggregate "`cat ../species.list`" mfa/*.mfa | sed s/"> "/">"/ \
	    > 4d.all.mfa
    # check they are all in there:
    grep "^>" 4d.all.mfa
    #	>anoCar2
    #	>galGal3
    #	>hg19
    #	>mm9
    #	>ornAna1
    #	>xenTro2
    #	>gasAcu1

    # tree-commas.nh:
    #	((((anoCar2,galGal3), ((hg19,mm9),ornAna1)), xenTro2),gasAcu1)

    # use phyloFit to create tree model (output is phyloFit.mod)
    time nice -n +19 \
	/cluster/bin/phast.build/cornellCVS/phast.2010-12-30/bin/phyloFit \
	    --EM --precision MED --msa-format FASTA --subst-mod REV \
		--tree ../tree-commas.nh 4d.all.mfa
    #	real    0m27.414s
    mv phyloFit.mod all.mod

    grep TREE all.mod
    #	((((anoCar2:0.460438,galGal3:0.356919):0.110793,((hg19:0.156181,mm9:0.285682):0.241872,ornAna1:0.489289):0.118758):0.152026,xenTro2:0.761473):0.567782,gasAcu1:0.567782);

#########################################################################
# phastCons 7-way (DONE - 2011-05-17 - Hiram)

    # split 7way mafs into 10M chunks and generate sufficient statistics 
    # files for # phastCons
    ssh encodek
    mkdir -p /hive/data/genomes/anoCar2/bed/multiz7way/cons/SS
    cd /hive/data/genomes/anoCar2/bed/multiz7way/cons/SS
    mkdir result

    cat << '_EOF_' > mkSS.csh
#!/bin/csh -ef
set c = $1
set MAF = /hive/data/genomes/anoCar2/bed/multiz7way/anno/result/$c.maf
set WINDOWS = /hive/data/genomes/anoCar2/bed/multiz7way/cons/SS/result/$c
set WC = `cat $MAF | wc -l`
set NL = `grep "^#" $MAF | wc -l`
if ( -s $2 ) then
    exit 0
endif
if ( -s $2.running ) then
    exit 0
endif

date >> $2.running

rm -fr $WINDOWS
mkdir $WINDOWS
pushd $WINDOWS > /dev/null
if ( $WC != $NL ) then
/cluster/bin/phast.build/cornellCVS/phast.2010-12-30/bin/msa_split \
    $MAF -i MAF -o SS -r $WINDOWS/$c -w 10000000,0 -I 1000 -B 5000
endif
popd > /dev/null
date >> $2
rm -f $2.running
'_EOF_'
    # << happy emacs
    chmod +x mkSS.csh

    cat << '_EOF_' > template
#LOOP
mkSS.csh $(root1) {check out line+ done/$(root1)}
#ENDLOOP
'_EOF_'
    # << happy emacs

    #	do the easy ones first to see some immediate results
    ls -1S -r ../../anno/result | sed -e "s/.maf//" > maf.list

    gensub2 maf.list single template jobList
    para -ram=8g create jobList
    para try ... check ... etc
# Completed: 4423 of 4423 jobs
# CPU time in finished jobs:        725s      12.09m     0.20h    0.01d  0.000 y
# IO & Wait Time:                 11488s     191.46m     3.19h    0.13d  0.000 y
# Average job time:                   3s       0.05m     0.00h    0.00d
# Longest finished job:             111s       1.85m     0.03h    0.00d
# Submission to last job:           716s      11.93m     0.20h    0.01d

    find . -type f | grep ".ss$" | wc
    #	3291    3291  175134

    # they do not all produce .ss files because of:
    #	./mkSS.csh chrUn_AAWZ02040655 done/chrUn_AAWZ02040655
    #	Reading alignment from /hive/data/genomes/anoCar2/bed/multiz7way/anno/result/chrUn_AAWZ02040655.maf...
    #	Creating partition 1 (column 1 to column 84)...
    #	WARNING: skipping partition 1; insufficient informative sites.
    #	Done.

    # Run phastCons
    #	This job is I/O intensive in its output files, beware where this
    #	takes place or do not run too many at once.
    ssh encodek
    mkdir -p /hive/data/genomes/anoCar2/bed/multiz7way/cons/run.cons
    cd /hive/data/genomes/anoCar2/bed/multiz7way/cons/run.cons

    #	there are going to be only one phastCons run using
    #	this same script.  It triggers off of the current working directory
    #	$cwd:t which is the "grp" in this script.  It is:
    #	all 

    cat << '_EOF_' > doPhast.csh
#!/bin/csh -fe
set PHASTBIN = /cluster/bin/phast.build/cornellCVS/phast.2010-12-30/bin
set c = $1
set cX = $1:r
set f = $2
set len = $3
set cov = $4
set rho = $5
set grp = $cwd:t
set cons = /hive/data/genomes/anoCar2/bed/multiz7way/cons
set tmp = $cons/tmp/$f
mkdir -p $tmp
set ssSrc = $cons
set useGrp = "$grp.mod"
ln -s $ssSrc/SS/result/$c/$f.ss $tmp
ln -s $cons/$grp/$grp.mod $tmp
pushd $tmp > /dev/null
$PHASTBIN/phastCons $f.ss $useGrp \
    --rho $rho --expected-length $len --target-coverage $cov --quiet \
    --seqname $c --idpref $c --most-conserved $f.bed --score > $f.pp
popd > /dev/null
mkdir -p pp/$c bed/$c
sleep 4
touch pp/$c bed/$c
rm -f pp/$c/$f.pp
rm -f bed/$c/$f.bed
mv $tmp/$f.pp pp/$c
mv $tmp/$f.bed bed/$c
rm -fr $tmp
'_EOF_'
    # << happy emacs
    chmod a+x doPhast.csh

    #	this template will serve for all runs
    #	root1 == chrom name, file1 == ss file name without .ss suffix
    cat << '_EOF_' > template
#LOOP
../run.cons/doPhast.csh $(root1) $(file1) 45 0.3 0.3 {check out line+ pp/$(root1)/$(file1).pp}
#ENDLOOP
'_EOF_'
    # << happy emacs

    find ../SS -type f | grep ".ss$" | sed -e 's/.ss$//' > ss.list
    wc -l ss.list
    #	3291 ss.list

    # Create parasol batch and run it
    # run for all species
    cd /hive/data/genomes/anoCar2/bed/multiz7way/cons
    mkdir -p all
    cd all
    #	Using the .mod tree
    cp -p ../../4d/all.mod ./all.mod

    gensub2 ../run.cons/ss.list single ../run.cons/template jobList
    para -ram=8g create jobList
    para try ... check ...
    para -maxJob=64 push
# Completed: 3291 of 3291 jobs
# PU time in finished jobs:       2799s      46.65m     0.78h    0.03d  0.000 y
# IO & Wait Time:                 21580s     359.66m     5.99h    0.25d  0.001 y
# Average job time:                   7s       0.12m     0.00h    0.00d
# Longest finished job:              25s       0.42m     0.01h    0.00d
# Submission to last job:          1251s      20.85m     0.35h    0.01d

    cd /hive/data/genomes/anoCar2/bed/multiz7way/cons/all

    find ./bed -type f | grep ".bed$" | xargs cat | sort -k1,1 -k2,2n \
	| awk '{printf "%s\t%d\t%d\tlod=%d\t%s\n", $1, $2, $3, $5, $5;}' \
	    > tmpMostConserved.bed

    /cluster/bin/scripts/lodToBedScore tmpMostConserved.bed > mostConserved.bed

    # load into database
    time nice -n +19 hgLoadBed anoCar2 phastConsElements7way mostConserved.bed
    #	Loaded 773199 elements of size 5
    #	real    0m4.937s

    # on human we often try for 5% overall cov, and 70% CDS cov 
    featureBits anoCar2 -enrichment ensGene:cds phastConsElements7way
    #	--rho 0.3 --expected-length 45 --target-coverage 0.3
    #	ensGene:cds 1.608%, phastConsElements7way 6.427%, both 1.252%,
    #	cover 77.83%, enrich 12.11x

    # hg19 for comparison
    #	refGene:cds 1.196%, phastConsElements46way 5.065%,
    #	both 0.888%, cover 74.22%, enrich 14.65x
    #	ensGene:cds 1.278%, phastConsElements46way 5.065%,
    #	both 0.910%, cover 71.23%, enrich 14.06x
    #	knownGene:cds 1.252%, phastConsElements46way 5.065%,
    #	both 0.905%, cover 72.29%, enrich 14.27x

    # Create merged posterier probability file and wiggle track data files
    cd /hive/data/genomes/anoCar2/bed/multiz7way/cons/all
    mkdir downloads

    find ./pp -type f | sed -e "s#^./##; s#\.# d #g; s#-# m #;" \
	| sort -k1,1 -k3,3n | sed -e "s# d #.#g; s# m #-#g;" | xargs cat \
	| gzip -c > downloads/phastCons7way.wigFix.gz

    # check integrity of data with wigToBigWig
    time (zcat downloads/phastCons7way.wigFix.gz \
	| wigToBigWig -verbose=2 stdin /hive/data/genomes/anoCar2/chrom.sizes \
	    phastCons7way.bw) > bigWig.log 2>&1 &
    tail bigWig.log
    # pid=6511: VmPeak:      2291152 kB
    #	real    2m46.514s

    bigWigInfo phastCons7way.bw
# version: 4
# isCompressed: yes
# isSwapped: 0
# primaryDataSize: 511,293,834
# primaryIndexSize: 14,137,804
# zoomLevels: 10
# chromCount: 3186
# basesCovered: 209,259,312
# mean: 0.540605
# min: 0.000000
# max: 1.000000
# std: 0.399259

    #	encode those files into wiggle data
    time (zcat downloads/phastCons7way.wigFix.gz \
	| wigEncode stdin phastCons7way.wig phastCons7way.wib)
    #	Converted stdin, upper limit 1.00, lower limit 0.00
    #	real    0m58.876s
    du -hsc *.wi?
    #	200M    phastCons7way.wib
    #	44M     phastCons7way.wig
    #	244M    total

    # Load gbdb and database with wiggle.
    ln -s `pwd`/phastCons7way.wib /gbdb/anoCar2/multiz7way/phastCons7way.wib
    time nice -n +19 hgLoadWiggle -pathPrefix=/gbdb/anoCar2/multiz7way \
	anoCar2 phastCons7way phastCons7way.wig
    #	real    0m5.254s

    # use to set trackDb.ra entries for wiggle min and max
    # and verify table is loaded correctly

    wigTableStats.sh anoCar2 phastCons7way
# db.table      min max mean count sumData stdDev viewLimits
#anoCar2.phastCons7way   0 1 0.540605 209259312 1.13127e+08 0.399259 viewLimits=0:1

    #  Create histogram to get an overview of all the data
    time nice -n +19 hgWiggle -doHistogram -db=anoCar2 \
	-hBinSize=0.001 -hBinCount=1000 -hMinVal=0.0 -verbose=2 \
	    phastCons7way > histogram.data 2>&1
    #	real    0m23.586s

    #	create plot of histogram:

    cat << '_EOF_' | gnuplot > histo.png
set terminal png small x000000 xffffff xc000ff x66ff66 xffff00 x00ffff
set size 1.4, 0.8
set key left box
set grid noxtics
set grid ytics
set title " Lizard anoCar2 Histogram phastCons7way track"
set xlabel " phastCons7way score"
set ylabel " Relative Frequency"
set y2label " Cumulative Relative Frequency (CRF)"
set y2range [0:1]
set y2tics
set yrange [0:0.02]

plot "histogram.data" using 2:5 title " RelFreq" with impulses, \
        "histogram.data" using 2:7 axes x1y2 title " CRF" with lines
'_EOF_'
    #	<< happy emacs

    display histo.png &

#############################################################################
# phyloP for 7-way (DONE - 2011-05-17 - Hiram)
    # run phyloP with score=LRT 
    ssh encodek
    mkdir /cluster/data/anoCar2/bed/multiz7way/consPhyloP
    cd /cluster/data/anoCar2/bed/multiz7way/consPhyloP

    mkdir run.phyloP
    cd run.phyloP
    # Adjust model file base composition background and rate matrix to be
    # representative of the chromosomes in play
    grep BACKGROUND ../../cons/all/all.mod | awk '{printf "%0.3f\n", $3 + $4}'
    #	0.510
    /cluster/bin/phast.build/cornellCVS/phast.2010-12-30/bin/modFreqs \
	../../cons/all/all.mod 0.510 > all.mod

    #	following the pattern from panTro3 with grp: "all" (no other grp)
    cat << '_EOF_' > doPhyloP.csh
#!/bin/csh -fe
set PHASTBIN = /cluster/bin/phast.build/cornellCVS/phast.2010-12-30/bin
set f = $1
set file1 = $f:t
set out = $2
set cName = $f:t:r
set n = $f:r:e
set grp = $cwd:t
set cons = /hive/data/genomes/anoCar2/bed/multiz7way/consPhyloP
set tmp = $cons/tmp/$grp/$f
rm -fr $tmp
mkdir -p $tmp
set ssSrc = "/hive/data/genomes/anoCar2/bed/multiz7way/cons/SS/result/$f"
set useGrp = "$grp.mod"
ln -s $cons/run.phyloP/$grp.mod $tmp
pushd $tmp > /dev/null
$PHASTBIN/phyloP --method LRT --mode CONACC --wig-scores --chrom $cName \
    -i SS $useGrp $ssSrc.ss > $file1.wigFix
popd > /dev/null
mkdir -p $out:h
sleep 4
mv $tmp/$file1.wigFix $out
rm -fr $tmp
rmdir --ignore-fail-on-non-empty $cons/tmp/$grp/$cName
rmdir --ignore-fail-on-non-empty $cons/tmp/$grp
rmdir --ignore-fail-on-non-empty $cons/tmp
'_EOF_'
    # << happy emacs

    # Create list of chunks
    find ../../cons/SS/result -type f | grep ".ss$" \
	| sed -e "s/.ss$//; s#^../../cons/SS/result/##" > ss.list
    # make sure the list looks good
    wc -l ss.list
    #	3291 ss.list

    # Create template file
    #	file1 == $chr/$chunk/file name without .ss suffix
    cat << '_EOF_' > template
#LOOP
../run.phyloP/doPhyloP.csh $(path1) {check out line+ wigFix/$(dir1)/$(file1).wigFix}
#ENDLOOP
'_EOF_'
    # << happy emacs

    ######################   Running all species  #######################
    # setup run for all species
    mkdir /hive/data/genomes/anoCar2/bed/multiz7way/consPhyloP/all
    cd /hive/data/genomes/anoCar2/bed/multiz7way/consPhyloP/all
    rm -fr wigFix
    mkdir wigFix

    gensub2 ../run.phyloP/ss.list single ../run.phyloP/template jobList
    # the -ram=8g will allow only one job per node to slow this down since
    #	it would run too fast otherwise
    para -ram=8g create jobList
    para try ... check ... etc ...
    para -maxJob=64 push
    para time > run.time
# Completed: 3291 of 3291 jobs
# CPU time in finished jobs:       3580s      59.66m     0.99h    0.04d  0.000 y
# IO & Wait Time:                 21571s     359.52m     5.99h    0.25d  0.001 y
# Average job time:                   8s       0.13m     0.00h    0.00d
# Longest finished job:              31s       0.52m     0.01h    0.00d
# Submission to last job:          1286s      21.43m     0.36h    0.01d

    # make downloads
    mkdir downloads

    time (find ./wigFix -type f | sed -e "s#^./##; s#\.# d #g; s#-# m #;" \
	| sort -k1,1 -k3,3n | sed -e "s# d #.#g; s# m #-#g;" | xargs cat \
	| gzip -c > downloads/phyloP7way.wigFix.gz) &
    #	real    4m11.100s

    # check integrity of data with wigToBigWig
    time (zcat downloads/phyloP7way.wigFix.gz \
	| wigToBigWig -verbose=2 stdin /hive/data/genomes/anoCar2/chrom.sizes \
	phyloP7way.bw) > bigWig.log 2>&1 &
    egrep "real|VmPeak" bigWig.log
    # pid=1977: VmPeak:      2291148 kB
    #	real    2m47.897s

    bigWigInfo phyloP7way.bw 
# version: 4
# isCompressed: yes
# isSwapped: 0
# primaryDataSize: 348,394,293
# primaryIndexSize: 14,137,804
# zoomLevels: 10
# chromCount: 3186
# basesCovered: 209,259,312
# mean: 0.433542
# min: -1.565000
# max: 2.266000
# std: 0.726042

    #	encode those files into wiggle data
    time (zcat downloads/phyloP7way.wigFix.gz \
	| wigEncode stdin phyloP7way.wig phyloP7way.wib) &
    #	Converted stdin, upper limit 2.27, lower limit -1.56
    #	real    1m3.017s

    du -hsc *.wi?
    #	200M    phyloP7way.wib
    #	44M     phyloP7way.wig
    #	244M    total

    # Load gbdb and database with wiggle.
    ln -s `pwd`/phyloP7way.wib /gbdb/anoCar2/multiz7way/phyloP7way.wib
    nice hgLoadWiggle -pathPrefix=/gbdb/anoCar2/multiz7way anoCar2 \
	phyloP7way phyloP7way.wig

    # use to set trackDb.ra entries for wiggle min and max
    # and verify table is loaded correctly

    wigTableStats.sh anoCar2 phyloP7way
# db.table      min max mean count sumData stdDev viewLimits
# anoCar2.phyloP7way      -1.565 2.266 0.433542 209259312 9.07227e+07 0.726042
# viewLimits=-1.565:2.266

    #  Create histogram to get an overview of all the data
    time nice -n +19 hgWiggle -doHistogram -db=anoCar2 \
	-hBinSize=0.001 -hBinCount=1000 -hMinVal=0.0 -verbose=2 \
	    phyloP7way > histogram.data 2>&1
    #	real    0m19.396s

    # find out the range for the 2:5 graph
    grep -v chrom histogram.data | grep "^[0-9]" | ave -col=5 stdin
# Q1 0.000233
# median 0.000518
# Q3 0.000759
# average 0.001003
# min 0.000000
# max 0.110084
# count 997
# total 1.000009
# standard deviation 0.004194

    #	create plot of histogram:

    cat << '_EOF_' | gnuplot > histo.png
set terminal png small x000000 xffffff xc000ff x66ff66 xffff00 x00ffff
set size 1.4, 0.8
set key left box
set grid noxtics
set grid ytics
set title " Lizard anoCar2 Histogram phyloP7way track"
set xlabel " phyloP7way score"
set ylabel " Relative Frequency"
set y2label " Cumulative Relative Frequency (CRF)"
set y2range [0:1]
set y2tics
set yrange [0:0.007]

plot "histogram.data" using 2:5 title " RelFreq" with impulses, \
        "histogram.data" using 2:7 axes x1y2 title " CRF" with lines
'_EOF_'
    #	<< happy emacs

    display histo.png &

#############################################################################
# download data for 7-way (DONE - 2011-01-19 - Hiram)
    mkdir /usr/local/apache/htdocs-hgdownload/goldenPath/anoCar2/phastCons7way
    cd /usr/local/apache/htdocs-hgdownload/goldenPath/anoCar2/phastCons7way
    ln -s \
/hive/data/genomes/anoCar2/bed/multiz7way/cons/all/downloads/phastCons7way.wigFix.gz \
	./phastCons7way.wigFix.gz
    ln -s /hive/data/genomes/anoCar2/bed/multiz7way/cons/all/phastCons7way.bw \
	./phastCons7way.bw

    ln -s /hive/data/genomes/anoCar2/bed/multiz7way/cons/all/all.mod \
	./vertebrate.mod
    # use a README from a recent multiz like this, this one is from panTro3
    ln -s /hive/data/genomes/anoCar2/bed/multiz7way/cons/README.txt .
    md5sum * > /hive/data/genomes/anoCar2/bed/multiz7way/cons/md5sum.txt
    ln -s /hive/data/genomes/anoCar2/bed/multiz7way/cons/md5sum.txt .

    mkdir /usr/local/apache/htdocs-hgdownload/goldenPath/anoCar2/phyloP7way
    cd /usr/local/apache/htdocs-hgdownload/goldenPath/anoCar2/phyloP7way
    ln -s \
/hive/data/genomes/anoCar2/bed/multiz7way/consPhyloP/all/downloads/phyloP7way.wigFix.gz \
	./phyloP7way.wigFix.gz
    ln -s \
/hive/data/genomes/anoCar2/bed/multiz7way/consPhyloP/all/phyloP7way.bw \
	./phyloP7way.bw
    ln -s \
/hive/data/genomes/anoCar2/bed/multiz7way/consPhyloP/run.phyloP/primate.mod \
	./primate.mod
    ln -s \
/hive/data/genomes/anoCar2/bed/multiz7way/consPhyloP/run.phyloP/all.mod \
	./vertebrate.mod
    # use a README from a recent multiz like this, this one is from panTro3
    ln -s /hive/data/genomes/anoCar2/bed/multiz7way/consPhyloP/README.txt .
    md5sum * > /hive/data/genomes/anoCar2/bed/multiz7way/consPhyloP/md5sum.txt
    ln -s /hive/data/genomes/anoCar2/bed/multiz7way/consPhyloP/md5sum.txt .

    mkdir /hive/data/genomes/anoCar2/bed/multiz7way/downloads
    cd /hive/data/genomes/anoCar2/bed/multiz7way/downloads
    # this was already done elsewhere:
    grep TREE ../cons/all/all.mod | sed -e "s/TREE: //" > 7way.nh
    /cluster/bin/phast/tree_doctor --rename \
"anoCar2->Lizard; hg19->Human; xenTro2->X_tropicalis; galGal3->Chicken; ornAna1->Platypus; mm9->Mouse; gasAcu1->Stickleback;" \
	t.nh > anoCar2.commonNames.7way.nh
    rm t.nh

    time cp -p ../anno/anoCar2.7way.maf ./multiz7way.maf
    #	real    1m13.847s
    time gzip multiz7way.maf
    #	real    9m46.886s
    mkdir /usr/local/apache/htdocs-hgdownload/goldenPath/anoCar2/multiz7way
    cd /usr/local/apache/htdocs-hgdownload/goldenPath/anoCar2/multiz7way
    ln -s /hive/data/genomes/anoCar2/bed/multiz7way/downloads/7way.nh .
    ln -s \
/hive/data/genomes/anoCar2/bed/multiz7way/downloads/anoCar2.commonNames.7way.nh .
    ln -s \
/hive/data/genomes/anoCar2/bed/multiz7way/downloads/multiz7way.maf.gz .

    # use a README from a recent multiz like this, this one is from danRer7
    ln -s /hive/data/genomes/anoCar2/bed/multiz7way/downloads/README.txt .

    md5sum *.nh *.gz *.txt \
	> /hive/data/genomes/anoCar2/bed/multiz7way/downloads/md5sum.txt
    ln -s /hive/data/genomes/anoCar2/bed/multiz7way/downloads/md5sum.txt .

#############################################################################
# hgPal downloads (DONE 2011-05-18 braney)
#   FASTA from 7way for ensGene

    ssh hgwdev
    screen
    bash
    rm -rf /cluster/data/anoCar2/bed/multiz7way/pal
    mkdir /cluster/data/anoCar2/bed/multiz7way/pal
    cd /cluster/data/anoCar2/bed/multiz7way/pal
    for i in `cat ../species.list`; do echo $i; done > order.lst

    mz=multiz7way
    gp=ensGene
    db=anoCar2
    mkdir exonAA exonNuc ppredAA ppredNuc
    for j in `sort -nk 2 /cluster/data/$db/chrom.sizes | awk '{print $1}'`
    do
	echo "date"
	echo "mafGene -chrom=$j  $db $mz $gp order.lst stdout | \
	    gzip -c > ppredAA/$j.ppredAA.fa.gz"
	echo "mafGene -chrom=$j -noTrans $db $mz $gp order.lst stdout | \
	    gzip -c > ppredNuc/$j.ppredNuc.fa.gz"
	echo "mafGene -chrom=$j -exons -noTrans $db $mz $gp order.lst stdout | \
	    gzip -c > exonNuc/$j.exonNuc.fa.gz"
	echo "mafGene -chrom=$j -exons $db $mz $gp order.lst stdout | \
	    gzip -c > exonAA/$j.exonAA.fa.gz"
    done > $gp.jobs

    time sh -x $gp.jobs > $gp.jobs.log 2>&1 &
    sleep 1
    tail -f $gp.jobs.log

# real    21m50.852s
# user    4m50.684s
# sys     3m10.510s


    mz=multiz7way
    gp=ensGene
    db=anoCar2
    zcat exonAA/*.gz | gzip -c > $gp.$mz.exonAA.fa.gz
    zcat exonNuc/*.gz | gzip -c > $gp.$mz.exonNuc.fa.gz
    zcat ppredAA/*.gz | gzip -c > $gp.$mz.ppredAA.fa.gz
    zcat ppredNuc/*.gz | gzip -c > $gp.$mz.ppredNuc.fa.gz

    rm -rf exonAA exonNuc ppredAA ppredNuc &

    # we're only distributing exons at the moment
    mz=multiz7way
    gp=ensGene
    db=anoCar2
    pd=/usr/local/apache/htdocs/goldenPath/$db/$mz/alignments
    mkdir -p $pd
    ln -s `pwd`/$gp.$mz.exonAA.fa.gz $pd/$gp.exonAA.fa.gz
    ln -s `pwd`/$gp.$mz.exonNuc.fa.gz $pd/$gp.exonNuc.fa.gz

############################################################################
# running cpgIsland business (DONE - 2011-08-22 - Hiram)
    mkdir /hive/data/genomes/anoCar2/bed/cpgIsland
    cd /hive/data/genomes/anoCar2/bed/cpgIsland
    ln -s ../../../danRer6/bed/cpgIsland/cpglh.exe .

    mkdir -p hardMaskedFa
    cut -f1 ../../chrom.sizes | while read C
do
    echo ${C}
    twoBitToFa ../../anoCar2.2bit:$C stdout \
	| maskOutFa stdin hard hardMaskedFa/${C}.fa
done

    ssh encodek
    cd /hive/data/genomes/anoCar2/bed/cpgIsland
    mkdir results
    cut -f1 ../../chrom.sizes > chr.list
    cat << '_EOF_' > template
#LOOP
./runOne $(root1) {check out exists results/$(root1).cpg}
#ENDLOOP
'_EOF_'
    # << happy emacs

    #	the faCount business is to make sure there is enough sequence to
    #	work with in the fasta.  cpglh.exe does not like files with too many
    #	N's - it gets stuck
    cat << '_EOF_' > runOne
#!/bin/csh -fe
set C = `faCount hardMaskedFa/$1.fa | egrep "^chr|^Zv" | awk '{print  $2 - $7 }'`
if ( $C > 200 ) then
    ./cpglh.exe hardMaskedFa/$1.fa > /scratch/tmp/$1.$$
    mv /scratch/tmp/$1.$$ $2
else
    touch $2
endif
'_EOF_'
    # << happy emacs
    chmod +x runOne

    gensub2 chr.list single template jobList
    para create jobList
    para try
    para check ... etc
    para time
# Completed: 6457 of 6457 jobs
# CPU time in finished jobs:        135s       2.25m     0.04h    0.00d  0.000 y
# IO & Wait Time:                 16267s     271.11m     4.52h    0.19d  0.001 y
# Average job time:                   3s       0.04m     0.00h    0.00d
# Longest finished job:              28s       0.47m     0.01h    0.00d
# Submission to last job:           871s      14.52m     0.24h    0.01d

    # Transform cpglh output to bed +
    catDir results | awk '{
$2 = $2 - 1;
width = $3 - $2;
printf("%s\t%d\t%s\t%s %s\t%s\t%s\t%0.0f\t%0.1f\t%s\t%s\n",
       $1, $2, $3, $5,$6, width,
       $6, width*$7*0.01, 100.0*2*$6/width, $7, $9);
}' > cpgIsland.bed

    # verify longest unique chrom name:
    cut -f1 cpgIsland.bed | sed -e 's/_random//' \
	| awk '{print length($0)}' | sort -rn | head -1
    #	18
    # update the length 14 in the template to be 16:
    sed -e "s/14/18/" $HOME/kent/src/hg/lib/cpgIslandExt.sql > cpgIslandExt.sql

    cd /hive/data/genomes/anoCar2/bed/cpgIsland
    hgLoadBed anoCar2 cpgIslandExt -tab -sqlTable=cpgIslandExt.sql cpgIsland.bed
# Loaded 15374 elements of size 10


    featureBits anoCar2 cpgIslandExt
    #	6520314 bases of 1701353770 (0.383%) in intersection

    # there should be no output from checkTableCoords:
    checkTableCoords -verboseBlocks -table=cpgIslandExt anoCar2

    #	cleanup
    rm -fr hardMaskedFa

############################################################################
## LASTZ Lizard anoCar2 (DONE - 2011-09-19 - Hiram)
    mkdir /cluster/data/anoCar2/bed/blastz.xenTro3.2011-09-19
    cd /cluster/data/anoCar2/bed/blastz.xenTro3.2011-09-19

    cat << '_EOF_' > DEF
# Lizard vs frog
BLASTZ_H=2000
BLASTZ_Y=3400
BLASTZ_L=8000
BLASTZ_K=2200
BLASTZ_M=50
BLASTZ_Q=/scratch/data/blastz/HoxD55.q

# TARGET: Lizard AnoCar2
SEQ1_DIR=/scratch/data/anoCar2/anoCar2.2bit
SEQ1_LEN=/scratch/data/anoCar2/chrom.sizes
SEQ1_CHUNK=10000000
SEQ1_LAP=10000
SEQ1_LIMIT=30

# TARGET: Frog xenTro3 - single chunk big enough for the largest scaffold
SEQ2_DIR=/scratch/data/xenTro3/xenTro3.2bit
SEQ2_LEN=/scratch/data/xenTro3/chrom.sizes
SEQ2_CHUNK=10000000
SEQ2_LIMIT=30
SEQ2_LAP=0

BASE=/cluster/data/anoCar2/bed/blastz.xenTro3.2011-09-19
TMPDIR=/scratch/tmp
'_EOF_'
    # << happy emacs

    time doBlastzChainNet.pl -verbose=2 `pwd`/DEF \
	-bigClusterHub=swarm -workhorse=hgwdev -smallClusterHub=memk \
	-chainMinScore=5000 -chainLinearGap=loose \
	> do.log 2>&1 &
    # got lost in some swarm problems, finish the lastz manually, then:

    time doBlastzChainNet.pl -verbose=2 `pwd`/DEF \
	-bigClusterHub=swarm -workhorse=hgwdev -smallClusterHub=memk \
	-continue=cat -chainMinScore=5000 \
	-chainLinearGap=loose > cat.log 2>&1 &
    #	real    338m49.752s

    cat fb.anoCar2.chainXenTro3Link.txt
    #	86932478 bases of 1701353770 (5.110%) in intersection
    cd /cluster/data/anoCar2/bed
    ln -s blastz.xenTro3.2011-09-19 lastz.xenTro3

    mkdir /hive/data/genomes/xenTro3/bed/blastz.anoCar2.swap
    cd /hive/data/genomes/xenTro3/bed/blastz.anoCar2.swap

    time doBlastzChainNet.pl -verbose=2 \
	/cluster/data/anoCar2/bed/blastz.xenTro3.2011-09-19/DEF \
	-bigClusterHub=swarm -workhorse=hgwdev -smallClusterHub=memk \
	-swap -chainMinScore=5000 \
	-chainLinearGap=loose > swap.log 2>&1 &
    #	Elapsed time: 88m21s
    cat fb.xenTro3.chainAnoCar2Link.txt
    #	92033566 bases of 1358334882 (6.775%) in intersection

    cd /hive/data/genomes/xenTro3/bed
    ln -s blastz.anoCar2.swap lastz.anoCar2

##########################################################################
