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

#	Gorilla gorilla gorilla
# ftp.ncbi.nlm.nih.gov/genbank/genomes/Eukaryotes/vertebrates_mammals/
#	Gorilla_gorilla/gorGor3.1/
#
# http://www.ncbi.nlm.nih.gov/Traces/wgs/?val=CABD02
#	WGS: AAMC01000001:AAMC01190823
#
##########################################################################
# Download sequence (DONE - 2011-10-11 - Hiram)
    mkdir -p /hive/data/genomes/gorGor3/genbank
    cd /hive/data/genomes/gorGor3/genbank
    wget --timestamping -r --cut-dirs=6 --level=0 -nH -x \
	--no-remove-listing -np \
"ftp://ftp.ncbi.nlm.nih.gov/genbank/genomes/Eukaryotes/vertebrates_mammals/Gorilla_gorilla/gorGor3.1/*"

    faSize Primary_Assembly/unplaced_scaffolds/FASTA/unplaced.scaf.fa.gz 
1511717716 bases (153400444 N's 1358317272 real 1358317272 upper 0 lower) in 19549 sequences in 1 files

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

use strict;
use warnings;

my %chrFrNameToChrN;

open (FH, "< Primary_Assembly/assembled_chromosomes/chr2acc") or die "can not read Primary_Assembly/assembled_chromosomes/chr2acc";
while (my $line = <FH>) {
    next if ($line =~ m/^#/);
    chomp $line;
    my ($chrN, $frName) = split('\s+', $line);
    $chrFrNameToChrN{$frName} = $chrN;
}
close (FH);

my $firstHeader = 1;
open (UC, "|gzip -c > ucsc.agp.gz") or die "can not write to ucsc.agp.gz";
open (FA, "|gzip -c > ucsc.fa.gz") or die "can not write to uscs.fa.gz";
foreach my $key (sort keys %chrFrNameToChrN) {
    my $chrN = $chrFrNameToChrN{$key};
    printf "%s\tchr%s\n", $key, $chrN;
    my $fastaFile = "Primary_Assembly/assembled_chromosomes/FASTA/chr${chrN}.fa.gz";
    my $agpFile = "Primary_Assembly/assembled_chromosomes/AGP/chr${chrN}.agp.gz";
    open (FH, "zcat $agpFile|") or die "can not read $agpFile";
    while (my $line = <FH>) {
	if ($line =~ m/^#/) {
	    if ($firstHeader) {
		printf UC "%s", $line;
	    }
	    next;
	}
	$firstHeader = 0;
	$line =~ s/$key/chr${chrN}/;
	printf UC "%s", $line;
    }
    close (FH);
    open (FH, "zcat $fastaFile|") or die "can not read $fastaFile";
    while (my $line = <FH>) {
	if ($line =~ m/^>/) {
	    die "can not match fasta name $key in $fastaFile\n$line" if ($line !~ m/$key/);
	    printf FA ">chr${chrN}\n";
	    next;
	}
	printf FA "%s", $line;
    }
    close (FH);
}
close (UC);
close (FA);
'_EOF_'
    # << happy emacs
    chmod +x mkUcsc.pl

    time ./mkUcsc.pl 
    #	real    18m3.244s

    zcat Primary_Assembly/unplaced_scaffolds/AGP/unplaced.scaf.agp.gz | \
	sed -e 's/\.1//' | gzip -c > ucsc.unplaced.agp.gz

    zcat Primary_Assembly/unplaced_scaffolds/FASTA/unplaced.scaf.fa.gz \
	| sed -e 's/^>.*emb|\([A-Z0-9]*\).*/>\1/' | gzip -c \
	> ucsc.unplaced.fa.gz

###########################################################################
# Initial genome build (DONE - 2011-10-11 - Hiram)
    cd /hive/data/genomes/gorGor3
    cat << '_EOF_' > gorGor3.config.ra
# Config parameters for makeGenomeDb.pl:
db gorGor3
clade mammal
genomeCladePriority 12
scientificName  Gorilla gorilla gorilla
commonName Gorilla
assemblyDate May 2011
assemblyLabel Wellcome Trust Sanger Institute May 2011 (NCBI project 31265, GCA_000151905.1)
assemblyShortLabel gorGor3.1
orderKey 25
mitoAcc NC_011120
fastaFiles /hive/data/genomes/gorGor3/genbank/ucsc.*fa.gz
agpFiles /hive/data/genomes/gorGor3/genbank/ucsc.*agp.gz
# qualFiles none
dbDbSpeciesDir gorilla
taxId 9595
'_EOF_'
    # << happy emacs

    time makeGenomeDb.pl -stop=agp -workhorse=hgwdev -fileServer=hgwdev \
	gorGor3.config.ra > step.agp.log 2>&1 &
    #	real    25m45.151s
    # take a look at the constructed AGP file to see if it has the names
    #	desired
    time makeGenomeDb.pl -continue=db -workhorse=hgwdev -fileServer=hgwdev \
	gorGor3.config.ra > step.db.log 2>&1 &
    #	real    86m25.408s

##########################################################################
# running repeat masker (DONE - 2011-10-12 - Hiram)
    mkdir /hive/data/genomes/gorGor3/bed/repeatMasker
    cd /hive/data/genomes/gorGor3/bed/repeatMasker
    time doRepeatMasker.pl -buildDir=`pwd` -noSplit \
	-bigClusterHub=swarm -dbHost=hgwdev -workhorse=hgwdev \
	-smallClusterHub=memk gorGor3 > do.log 2>&1 &
    #	real    1356m28.922s
    #	real    35m31.331s
    cat faSize.rmsk.txt
    #	3029553646 bases (206793566 N's 2822760080 real 1446718337 upper
    #	1376041743 lower) in 46823 sequences in 1 files
    #	%45.42 masked total, %48.75 masked real

    grep -i versi do.log
# RepeatMasker version development-$Id: RepeatMasker,v 1.26 2011/09/26 16:19:44 angie Exp $
#    April 26 2011 (open-3-3-0) version of RepeatMasker

    featureBits -countGaps gorGor3 rmsk
    #	1384011521 bases of 3029553646 (45.684%) in intersection
    # why is it different than the faSize above ?
    # because rmsk masks out some N's as well as bases, the count above
    #	separates out the N's from the bases, it doesn't show lower case N's

##########################################################################
# running simple repeat (DONE - 2011-10-12 - Hiram)
    mkdir /hive/data/genomes/gorGor3/bed/simpleRepeat
    cd /hive/data/genomes/gorGor3/bed/simpleRepeat
    time doSimpleRepeat.pl -buildDir=`pwd` -bigClusterHub=swarm \
	-dbHost=hgwdev -workhorse=hgwdev -smallClusterHub=memk \
	gorGor3 > do.log 2>&1 &
    #	real    3208m54.385s
    #	There were problems in three chunks.  They would not complete
    #	after a couple days running time.  They were taken manually
    #	and broken up into smaller parts to get completed.  Note
    #	work in directories:
    #	/hive/data/genomes/gorGor3/bed/simpleRepeat/stragglers
    #	/hive/data/genomes/gorGor3/bed/simpleRepeat/moreStragglers
    time doSimpleRepeat.pl -buildDir=`pwd` -bigClusterHub=swarm \
	-continue=filter -dbHost=hgwdev -workhorse=hgwdev \
	-smallClusterHub=memk \
	gorGor3 > filter.log 2>&1 &
    #	real    1m1.322s

    cat fb.simpleRepeat 
    #	199433151 bases of 2822760080 (7.065%) in intersection
    #	This is quite a large intersection, highly unusual compared to
    #	most genomes

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

    twoBitToFa gorGor3.2bit stdout | faSize stdin > faSize.gorGor3.2bit.txt
    cat faSize.gorGor3.2bit.txt
    #	3029553646 bases (206793566 N's 2822760080 real 1445288963 upper
    #	1377471117 lower) in 46823 sequences in 1 files
    #	%45.47 masked total, %48.80 masked real

    #  *** REMEMBER *** to reset they symLink in gbdb:
    rm /gbdb/gorGor3/gorGor3.2bit
    ln -s `pwd`/gorGor3.2bit /gbdb/gorGor3/gorGor3.2bit

#########################################################################
# Verify all gaps are marked, add any N's not in gap as type 'other'
#	(DONE - 2011-10-13 - Hiram)
    mkdir /hive/data/genomes/gorGor3/bed/gap
    cd /hive/data/genomes/gorGor3/bed/gap
    time nice -n +19 findMotif -motif=gattaca -verbose=4 \
	-strand=+ ../../gorGor3.unmasked.2bit > findMotif.txt 2>&1
    #	real    1m8.647s

    grep "^#GAP " findMotif.txt | sed -e "s/^#GAP //" > allGaps.bed
    featureBits gorGor3 -not gap -bed=notGap.bed
    #	3026913193 bases of 3026913193 (100.000%) in intersection
    featureBits gorGor3 allGaps.bed notGap.bed -bed=new.gaps.bed
    #	204153113 bases of 3026913193 (6.745%) in intersection
    #	Wow, that's a lot of unmarked gap

    #	what is the highest index in the existing gap table:
    hgsql -N -e "select ix from gap;" gorGor3 | sort -n | tail -1
    #	1034
    #	that number is used below in the script to mark all the new gaps
    #	with a higher ix than that
    cat << '_EOF_' > mkGap.pl
#!/bin/env perl

use strict;
use warnings;

my $ix=`hgsql -N -e "select ix from gap;" gorGor3 | sort -n | tail -1`;
chomp $ix;

open (FH,"<new.gaps.bed") or die "can not read new.gaps.bed";
while (my $line = <FH>) {
    my ($chrom, $chromStart, $chromEnd, $rest) = split('\s+', $line);
    ++$ix;
    printf "%s\t%d\t%d\t%d\tN\t%d\tother\tyes\n", $chrom, $chromStart,
        $chromEnd, $ix, $chromEnd-$chromStart;
}
close (FH);
'_EOF_'
    # << happy emacs
    chmod +x ./mkGap.pl
    ./mkGap.pl > other.bed
    featureBits -countGaps gorGor3 other.bed
    #	204153113 bases of 3029553646 (6.739%) in intersection
    wc -l other.bed
    #	423025
    hgLoadBed -sqlTable=$HOME/kent/src/hg/lib/gap.sql \
	-noLoad gorGor3 otherGap other.bed
    #	starting with this many
    hgsql -e "select count(*) from gap;" gorGor3
    #	10836
    hgsql gorGor3 -e 'load data local infile "bed.tab" into table gap;'
    #	result count:
    hgsql -e "select count(*) from gap;" gorGor3
    #	433861
    # == 10836 + 423025
    # verify we aren't adding gaps where gaps already exist
    # this would output errors if that were true:
    gapToLift -minGap=1 gorGor3 nonBridged.lift -bedFile=nonBridged.bed
    # see example in danRer7.txt

    # there are non-bridged gaps here:

    hgsql -N -e "select bridge from gap;" gorGor3 | sort | uniq -c
    #	7001 no
    #	426860 yes

##########################################################################
#  gorGor3 - Gorilla - Ensembl Genes version 64  (DONE - 2011-10-12 - hiram)
    ssh hgwdev
    cd /hive/data/genomes/gorGor3
    cat << '_EOF_' > gorGor3.ensGene.ra
# required db variable
db gorGor3
# optional nameTranslation, the sed command that will transform
#       Ensemble names to UCSC names.  With quotes just to make sure.
# delete commands take out genes that are only in patch sequence
nameTranslation '/^cutchr/d; /^unplaced/d; s/^\([0-9X][0-9ab]*\)/chr\1/; s/^MT/chrM/;'
'_EOF_'
#  << happy emacs

    doEnsGeneUpdate.pl  -ensVersion=64 gorGor3.ensGene.ra
    ssh hgwdev
    cd /hive/data/genomes/gorGor3/bed/ensGene.64
    featureBits gorGor3 ensGene
    # 50017329 bases of 3026913193 (1.652%) in intersection

############################################################################
# set this as defaultDb (DONE - 2011-10-13 - Hiram)
    #	and make this the default genome for Pig
    hgsql -e 'update defaultDb set name="gorGor3" where name="gorGor1";' \
	hgcentraltest
############################################################################
# scaffolds ctgPos2 track (DONE - 2011-10-13 - Hiram)
    mkdir /hive/data/genomes/gorGor3/bed/ctgPos2
    cd /hive/data/genomes/gorGor3/bed/ctgPos2
    cat << '_EOF_' > scaffoldToCtgPos2.pl 
#!/usr/bin/env perl

use strict;
use warnings;

my %chromSizes;

open (FH, "<../../chrom.sizes") or die "can not read ../../chrom.sizes";
while (my $line = <FH>) {
    chomp $line;
    my ($chr, $size) = split('\s+', $line);
    $chromSizes{$chr} = $size;
}
close (FH);

my %fragToChrom;
my %fragToPosition;
my %fragType;
my %fragSize;
my %chrType;

open (FH, "hgsql -N -e 'select chrom,chromStart,chromEnd,type,frag,fragStart,fragEnd,strand from gold;' gorGor3|") or die "can not select from gold.gorGor3";
while (my $line = <FH>) {
    chomp $line;
    my ($chrom,$chromStart,$chromEnd,$type,$frag,$fragStart,$fragEnd,$strand) =
	split('\t', $line);
    next if (exists($fragToChrom{$frag}));
    my $size = $fragEnd - $fragStart;
    $fragToChrom{$frag} = $chrom;
    $fragToPosition{$frag} = $chromStart;
    $fragType{$frag} = $type;
    if (exists($chrType{$chrom})) {
	if ($chrType{$chrom} ne $type) {
	    printf STDERR "WARN: different types for chrom $chrom: $type NE $chrType{$chrom}\n"; 
	}
    }
    $chrType{$chrom} = $type;
    $fragSize{$frag} = $size;
}
close (FH);

open (FH, "</hive/data/genomes/gorGor3/genbank/Primary_Assembly/scaffold_localID2acc") or die "can not read scaffold_localID2acc";
while (my $line = <FH>) {
    next if ($line =~ m/^#Scaffold/);
    chomp $line;
    my ($scafName, $fragName) = split('\s+', $line);
    my $noSuffix = $fragName;
    $noSuffix =~ s/\.1$//;
    if (exists($chromSizes{$noSuffix})) {
	my $type = "";
	$type = $chrType{$noSuffix} if (exists($chrType{$noSuffix}));
	$type = $fragType{$fragName} if (exists($fragType{$fragName}));
	if (length($type) > 0) {
	my $size = $chromSizes{$noSuffix};
	printf "%s\t%d\t%s\t0\t%d\t%s\n", $scafName, $size, $noSuffix,
	    $size, $type;
	} else {
	    printf STDERR "can not find fragType for $fragName, $noSuffix\n";
	}
    } elsif (exists($fragToChrom{$fragName})) {
	printf "%s\t%d\t%s\t0\t%d\t%s\n", $scafName, $fragSize{$fragName},
	    $fragToChrom{$fragName}, $fragSize{$fragName}, $fragType{$fragName};
    } else {
	printf STDERR "can not find: $fragName -> $scafName\n";
    }
}
close (FH);
'_EOF_'
    # << happy emacs
    chmod +x scaffoldToCtgPos2.pl
    ./scaffoldToCtgPos2.pl > scaffold.ctgPos2.tab

    cut -f1 scaffold.ctgPos2.tab | awk '{print length($1)}' | sort -rn | head -1
    #	23
    cut -f3 scaffold.ctgPos2.tab | awk '{print length($1)}' | sort -rn | head -1
    #	12

    sed -e "s/20/23/; s/16/12/" $HOME/kent/src/hg/lib/ctgPos2.sql > ctgPos2.sql

    hgLoadSqlTab gorGor3 ctgPos2 ctgPos2.sql scaffold.ctgPos2.tab

########################################################################
# MAKE 11.OOC FILE FOR BLAT/GENBANK (DONE - 2011-10-14 - 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 \(  2822760080 / 2897316137 \) \* 1024
    #	( 2822760080 / 2897316137 ) * 1024 = 997.649613
    # round up to 1000

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

    #	copy all of this stuff to the klusters:
    # there are non-bridged gaps, but they aren't really that, since
    #	they are all exactly 100 bases and they are all over the normal
    #	chromosomes:
    hgsql -N -e "select bridge from gap;" gorGor3 | sort | uniq -c
    #	7001 no
    #	426860 yes

    #	cd /hive/data/genomes/gorGor3/jkStuff
    #	gapToLift gorGor3 nonBridged.lift -bedFile=nonBridged.bed
    cd /hive/data/genomes/gorGor3
    mkdir /hive/data/staging/data/gorGor3
    cp -p jkStuff/gorGor3.11.ooc chrom.sizes \
	gorGor3.2bit /hive/data/staging/data/gorGor3
    # request rsync copy from cluster admin

#########################################################################
# AUTO UPDATE GENBANK (DONE - 2011-10-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
# Gorilla gorilla	546	4	0
# Gorilla gorilla gorilla	1	0	0
    # to decide which "native" mrna or ests you want to specify in genbank.conf
    # this appears that gorGor3 has almost no native est's

    ssh hgwdev  
    cd $HOME/kent/src/hg/makeDb/genbank
    git pull
    # edit etc/genbank.conf to add gorGor3 before gorGor2 and commit to GIT
# Gorilla
gorGor3.serverGenome = /hive/data/genomes/gorGor3/gorGor3.2bit
gorGor3.clusterGenome = /scratch/data/gorGor3/gorGor3.2bit
gorGor3.ooc = /scratch/data/gorGor3/gorGor3.11.ooc
gorGor3.lift = no
gorGor3.perChromTables = no
gorGor3.refseq.mrna.native.pslCDnaFilter  = ${ordered.refseq.mrna.native.pslCDnaFilter}
gorGor3.refseq.mrna.xeno.pslCDnaFilter    = ${ordered.refseq.mrna.xeno.pslCDnaFilter}
gorGor3.genbank.mrna.native.pslCDnaFilter = ${ordered.genbank.mrna.native.pslCDnaFilter}
gorGor3.genbank.mrna.xeno.pslCDnaFilter   = ${ordered.genbank.mrna.xeno.pslCDnaFilter}
gorGor3.genbank.est.native.pslCDnaFilter  = ${ordered.genbank.est.native.pslCDnaFilter}
gorGor3.genbank.est.xeno.pslCDnaFilter    = ${ordered.genbank.est.xeno.pslCDnaFilter}
gorGor3.downloadDir = gorGor3
gorGor3.refseq.mrna.native.load  = yes
gorGor3.refseq.mrna.xeno.load = yes
gorGor3.refseq.mrna.xeno.loadDesc  = yes
gorGor3.genbank.est.native.load = no
gorGor3.upstreamGeneTbl = xenoRefGene

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

    # ~/kent/src/hg/makeDb/genbank/src/lib/gbGenome.c already contains
    # gorGor 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 gorGor3 &
    #	var/build/logs/2011.10.17-09:14:51.gorGor3.initalign.log
    #	real    788m42.989s

    # load database when finished
    ssh hgwdev
    cd /cluster/data/genbank
    time nice -n +19 ./bin/gbDbLoadStep -drop -initialLoad gorGor3 &
    #	logFile: var/dbload/hgwdev/logs/2011.10.21-08:59:38.dbload.log
    #	real    22m45.028s

    # enable daily alignment and update of hgwdev
    cd ~/kent/src/hg/makeDb/genbank
    git pull
    # add gorGor3 to:
        etc/align.dbs
        etc/hgwdev.dbs
    git commit -m "daily update for gorGor3 replaces daily update for gorGor2" \
	etc/align.dbs etc/hgwdev.dbs

    git push
    make etc-update

#########################################################################
#  BLATSERVERS ENTRY (DONE - 2007-11-04 - Hiram)
#	After getting a blat server assigned by the Blat Server Gods,
    ssh hgwdev

    hgsql -e 'INSERT INTO blatServers (db, host, port, isTrans, canPcr) \
	VALUES ("gorGor3", "blatx", "17798", "1", "0"); \
	INSERT INTO blatServers (db, host, port, isTrans, canPcr) \
	VALUES ("gorGor3", "blatx", "17799", "0", "1");' \
	    hgcentraltest
    #	test it with some sequence

###########################################################################
# lastz swap Human hg19 (DONE - 2011-10-21 - Hiram)
    # original alignment
    cd /hive/data/genomes/hg19/bed/lastzGorGor3.2011-10-17
    cat fb.hg19.chainGorGor3Link.txt  
    #	2603997992 bases of 2897316137 (89.876%) in intersection

    #	running the swap
    mkdir /hive/data/genomes/gorGor3/bed/blastz.hg19.swap
    cd /hive/data/genomes/gorGor3/bed/blastz.hg19.swap
    time nice -n +19 doBlastzChainNet.pl -verbose=2 \
	/hive/data/genomes/hg19/bed/lastzGorGor3.2011-10-17/DEF \
	-swap -syntenicNet \
	-noLoadChainSplit -chainMinScore=5000 -chainLinearGap=medium \
	-workhorse=hgwdev -smallClusterHub=encodek -bigClusterHub=swarm \
	> swap.log 2>&1 &
    #	real    69m39.685s
    cat fb.gorGor3.chainHg19Link.txt 
    #	2571797450 bases of 2822760080 (91.109%) in intersection

    cd /hive/data/genomes/gorGor3/bed
    ln -s blastz.hg19.swap lastz.hg19

############################################################################
# LASTZ Gorilla NomLeu1 (DONE - 2011-10-21,27 - Hiram)
    mkdir /hive/data/genomes/gorGor3/bed/lastzNomLeu1.2011-10-21
    cd /hive/data/genomes/gorGor3/bed/lastzNomLeu1.2011-10-21

    cat << '_EOF_' > DEF
# Gorilla vs Gibbon
# maximum M allowed with lastz is only 254
BLASTZ_M=254
BLASTZ_Q=/scratch/data/blastz/human_chimp.v2.q
BLASTZ_O=600
BLASTZ_E=150
# other parameters on advice from Webb
BLASTZ_K=4500
BLASTZ_Y=15000
BLASTZ_T=2

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

# QUERY: Gibbon nomLeu1
SEQ2_DIR=/scratch/data/nomLeu1/nomLeu1.2bit
SEQ2_LEN=/scratch/data/nomLeu1/chrom.sizes
SEQ2_CHUNK=20000000
SEQ2_LAP=0
SEQ2_LIMIT=100

BASE=/hive/data/genomes/gorGor3/bed/lastzNomLeu1.2011-10-21
TMPDIR=/scratch/tmp
'_EOF_'
    # << happy emacs

    #	establish a screen to control this job
    screen
    time nice -n +19 doBlastzChainNet.pl -verbose=2 \
	`pwd`/DEF \
	-syntenicNet \
	-noLoadChainSplit -chainMinScore=5000 -chainLinearGap=medium \
	-workhorse=hgwdev -smallClusterHub=encodek -bigClusterHub=swarm \
	> do.log 2>&1 &
    #	after recovering manually after a hive filesystem outage
    time nice -n +19 doBlastzChainNet.pl -verbose=2 \
	`pwd`/DEF \
	-continue=cat -syntenicNet \
	-noLoadChainSplit -chainMinScore=5000 -chainLinearGap=medium \
	-workhorse=hgwdev -smallClusterHub=encodek -bigClusterHub=swarm \
	> cat.log 2>&1 &
    #	Elapsed time: 160m12s
    cat fb.gorGor3.chainNomLeu1Link.txt
    #	2356321697 bases of 2822760080 (83.476%) in intersection

    cd /hive/data/genomes/gorGor3/bed
    ln -s lastzNomLeu1.2011-10-21 lastz.nomLeu1

    #	running the swap
    mkdir /hive/data/genomes/nomLeu1/bed/blastz.gorGor3.swap
    cd /hive/data/genomes/nomLeu1/bed/blastz.gorGor3.swap
    time nice -n +19 doBlastzChainNet.pl -verbose=2 \
	/hive/data/genomes/gorGor3/bed/lastzNomLeu1.2011-10-21/DEF \
	-swap -syntenicNet \
	-noLoadChainSplit -chainMinScore=5000 -chainLinearGap=medium \
	-workhorse=hgwdev -smallClusterHub=encodek -bigClusterHub=swarm \
	> swap.log 2>&1 &
    #	real    74m9.554s
    cat fb.nomLeu1.chainGorGor3Link.txt 
    #	2318822567 bases of 2756591777 (84.119%) in intersection

    cd /hive/data/genomes/nomLeu1/bed
    ln -s blastz.gorGor3.swap lastz.gorGor3

############################################################################
# LASTZ Baboon papHam1 (DONE - 2011-10-21 - Hiram)
    mkdir /hive/data/genomes/gorGor3/bed/lastzPapHam1.2011-10-21
    cd /hive/data/genomes/gorGor3/bed/lastzPapHam1.2011-10-21

    cat << '_EOF_' > DEF
# Gorilla vs Gibbon
# maximum M allowed with lastz is only 254
BLASTZ_M=254
BLASTZ_Q=/scratch/data/blastz/human_chimp.v2.q
BLASTZ_O=600
BLASTZ_E=150
# other parameters on advice from Webb
BLASTZ_K=4500
BLASTZ_Y=15000
BLASTZ_T=2

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

# QUERY: Gibbon papHam1
SEQ2_DIR=/scratch/data/papHam1/papHam1.2bit
SEQ2_LEN=/scratch/data/papHam1/chrom.sizes
SEQ2_CHUNK=10000000
SEQ2_LAP=0
SEQ2_LIMIT=300

BASE=/hive/data/genomes/gorGor3/bed/lastzPapHam1.2011-10-21
TMPDIR=/scratch/tmp
'_EOF_'
    # << happy emacs

    #	establish a screen to control this job
    screen
    time nice -n +19 doBlastzChainNet.pl -verbose=2 \
	`pwd`/DEF \
	-syntenicNet \
	-noLoadChainSplit -chainMinScore=5000 -chainLinearGap=medium \
	-workhorse=hgwdev -smallClusterHub=encodek -bigClusterHub=swarm \
	> do.log 2>&1 &
    # after finishing the lastz run manually due to hive filesystem problems
    time nice -n +19 doBlastzChainNet.pl -verbose=2 \
	`pwd`/DEF \
	-continue=cat -syntenicNet \
	-noLoadChainSplit -chainMinScore=5000 -chainLinearGap=medium \
	-workhorse=hgwdev -smallClusterHub=encodek -bigClusterHub=swarm \
	> cat.log 2>&1 &
    #	real    205m20.060s
    cat fb.gorGor3.chainPapHam1Link.txt 
    #	2242232481 bases of 2822760080 (79.434%) in intersection

    cd /hive/data/genomes/gorGor3/bed
    ln -s lastzPapHam1.2011-10-21 lastz.papHam1

    #	running the swap - DONE - 2011-09-21
    mkdir /hive/data/genomes/papHam1/bed/blastz.gorGor3.swap
    cd /hive/data/genomes/papHam1/bed/blastz.gorGor3.swap
    time nice -n +19 doBlastzChainNet.pl -verbose=2 \
	/hive/data/genomes/gorGor3/bed/lastzPapHam1.2011-10-21/DEF \
	-swap -syntenicNet \
	-noLoadChainSplit -chainMinScore=5000 -chainLinearGap=medium \
	-workhorse=hgwdev -smallClusterHub=encodek -bigClusterHub=swarm \
	> swap.log 2>&1 &
    #	real    499m31.793s
    cat fb.papHam1.chainGorGor3Link.txt 
    #	2217454777 bases of 2741867288 (80.874%) in intersection

    cd /hive/data/genomes/papHam1/bed
    ln -s blastz.gorGor3.swap lastz.gorGor3

############################################################################
# LASTZ Bushbaby otoGar1 (DONE - 2011-10-21,31 - Hiram)
    mkdir /hive/data/genomes/gorGor3/bed/lastzOtoGar1.2011-10-21
    cd /hive/data/genomes/gorGor3/bed/lastzOtoGar1.2011-10-21

    cat << '_EOF_' > DEF
# Gorilla vs Bushbaby
# maximum M allowed with lastz is only 254
BLASTZ_M=254
BLASTZ_Q=/scratch/data/blastz/human_chimp.v2.q
BLASTZ_O=600
BLASTZ_E=150
# other parameters on advice from Webb
BLASTZ_K=4500
BLASTZ_Y=15000
BLASTZ_T=2

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

# QUERY: Bushbaby otoGar1
SEQ2_DIR=/scratch/data/otoGar1/otoGar1.2bit
SEQ2_LEN=/scratch/data/otoGar1/chrom.sizes
SEQ2_CHUNK=10000000
SEQ2_LAP=0
SEQ2_LIMIT=300

BASE=/hive/data/genomes/gorGor3/bed/lastzOtoGar1.2011-10-21
TMPDIR=/scratch/tmp
'_EOF_'
    # << happy emacs

    #	establish a screen to control this job
    screen
    time nice -n +19 doBlastzChainNet.pl -verbose=2 \
	`pwd`/DEF \
	-syntenicNet \
	-noLoadChainSplit -chainMinScore=5000 -chainLinearGap=medium \
	-workhorse=hgwdev -smallClusterHub=encodek -bigClusterHub=swarm \
	> do.log 2>&1 &
    # after manually finishing the batch after hive filesystem outage
    time nice -n +19 doBlastzChainNet.pl -verbose=2 \
	`pwd`/DEF \
	-continue=cat -syntenicNet \
	-noLoadChainSplit -chainMinScore=5000 -chainLinearGap=medium \
	-workhorse=hgwdev -smallClusterHub=encodek -bigClusterHub=swarm \
	> cat.log 2>&1 &
    #	real    134m17.379s
    cat fb.gorGor3.chainOtoGar1Link.txt  
    #	339201275 bases of 2822760080 (12.017%) in intersection

    cd /hive/data/genomes/gorGor3/bed
    ln -s lastzOtoGar1.2011-10-21 lastz.otoGar1

    #	running the swap
    mkdir /hive/data/genomes/otoGar1/bed/blastz.gorGor3.swap
    cd /hive/data/genomes/otoGar1/bed/blastz.gorGor3.swap
    time nice -n +19 doBlastzChainNet.pl -verbose=2 \
	/hive/data/genomes/gorGor3/bed/lastzOtoGar1.2011-10-21/DEF \
	-swap -syntenicNet \
	-noLoadChainSplit -chainMinScore=5000 -chainLinearGap=medium \
	-workhorse=hgwdev -smallClusterHub=encodek -bigClusterHub=swarm \
	> swap.log 2>&1 &
    #	real    60m56.773s
    cat fb.otoGar1.chainGorGor3Link.txt 
    #	336379843 bases of 1969052059 (17.083%) in intersection

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

############################################################################
# lastz swap Chimp rheMac2 (DONE - 2011-10-23 - Hiram)
    # original alignment
    cd /hive/data/genomes/rheMac2/bed/lastzGorGor3.2011-10-21
    cat fb.rheMac2.chainGorGor3Link.txt  
    #	2175264883 bases of 2646704109 (82.188%) in intersection

    #	running the swap - DONE - 2011-09-21
    mkdir /hive/data/genomes/gorGor3/bed/blastz.rheMac2.swap
    cd /hive/data/genomes/gorGor3/bed/blastz.rheMac2.swap
    time nice -n +19 doBlastzChainNet.pl -verbose=2 \
	/hive/data/genomes/rheMac2/bed/lastzGorGor3.2011-10-21/DEF \
	-swap -syntenicNet \
	-noLoadChainSplit -chainMinScore=5000 -chainLinearGap=medium \
	-workhorse=hgwdev -smallClusterHub=encodek -bigClusterHub=swarm \
	> swap.log 2>&1 &
    #	real    85m16.618s
    cat fb.gorGor3.chainRheMac2Link.txt
    #	2236867094 bases of 2822760080 (79.244%) in intersection

    cd /hive/data/genomes/gorGor3/bed
    ln -s  blastz.rheMac2.swap lastz.rheMac2

############################################################################
# lastz swap Orangutan ponAbe2 (DONE - 2011-10-23 - Hiram)
    # original alignment to Orangutan
    cd /hive/data/genomes/ponAbe2/bed/lastzGorGor3.2011-10-21
    cat fb.ponAbe2.chainGorGor3Link.txt  
    #	2584628377 bases of 3093572278 (83.548%) in intersection

    #	running the swap
    mkdir /hive/data/genomes/gorGor3/bed/blastz.ponAbe2.swap
    cd /hive/data/genomes/gorGor3/bed/blastz.ponAbe2.swap
    time nice -n +19 doBlastzChainNet.pl -verbose=2 \
	/hive/data/genomes/ponAbe2/bed/lastzGorGor3.2011-10-21/DEF \
	-swap -syntenicNet \
	-noLoadChainSplit -chainMinScore=5000 -chainLinearGap=medium \
	-workhorse=hgwdev -smallClusterHub=encodek -bigClusterHub=swarm \
	> swap.log 2>&1 &
    #	real    86m19.901s
    cat fb.gorGor3.chainPonAbe2Link.txt 
    #	2437562544 bases of 2822760080 (86.354%) in intersection

    cd /hive/data/genomes/gorGor3/bed
    ln -s  blastz.ponAbe2.swap lastz.ponAbe2

############################################################################
# lastz swap Marmoset calJac3 (DONE - 2011-10-27 - Hiram)
    # the original alignment
    cd /hive/data/genomes/calJac3/bed/lastzGorGor3.2011-10-21
    cat fb.calJac3.chainGorGor3Link.txt
    #	1928768328 bases of 2752505800 (70.073%) in intersection

    #	running the swap
    mkdir /hive/data/genomes/gorGor3/bed/blastz.calJac3.swap
    cd /hive/data/genomes/gorGor3/bed/blastz.calJac3.swap
    time nice -n +19 doBlastzChainNet.pl -verbose=2 \
	/hive/data/genomes/calJac3/bed/lastzGorGor3.2011-10-21/DEF \
	-swap -syntenicNet \
	-noLoadChainSplit -chainMinScore=5000 -chainLinearGap=medium \
	-workhorse=hgwdev -smallClusterHub=encodek -bigClusterHub=swarm \
	> swap.log 2>&1 &
    #	real    86m1.559s

    cat fb.gorGor3.chainCalJac3Link.txt 
    #	1933784568 bases of 2822760080 (68.507%) in intersection

    cd /hive/data/genomes/gorGor3/bed
    ln -s  blastz.calJac3.swap lastz.calJac3

############################################################################
# lastz swap Chimp panTro3 (DONE - 2011-10-27 - Hiram)
    # original alignment
    cd /hive/data/genomes/panTro3/bed/lastzGorGor3.2011-10-21
    cat fb.panTro3.chainGorGor3Link.txt  
    #	2553716156 bases of 2900529764 (88.043%) in intersection

    #	running the swap
    mkdir /hive/data/genomes/gorGor3/bed/blastz.panTro3.swap
    cd /hive/data/genomes/gorGor3/bed/blastz.panTro3.swap
    time nice -n +19 doBlastzChainNet.pl -verbose=2 \
	/hive/data/genomes/panTro3/bed/lastzGorGor3.2011-10-21/DEF \
	-swap -syntenicNet \
	-noLoadChainSplit -chainMinScore=5000 -chainLinearGap=medium \
	-workhorse=hgwdev -smallClusterHub=encodek -bigClusterHub=swarm \
	> swap.log 2>&1 &
    #	about 48 minutes
    # an odd error:
# netClass -verbose=0 -noAr noClass.net gorGor3 panTro3 gorGor3.panTro3.net
# hashMustFindVal: 'chr2' not found
    #	I can't find any chr2 in any of these files ...
    #	This was due to a bogus simpleRepeat table in panTro3, mistakenly
    #	loaded 09 May 2011, backpushed from the RR 27 October 2011
    cat fb.gorGor3.chainPanTro3Link.txt 
    #	2513463838 bases of 2822760080 (89.043%) in intersection
    # after replacing the simpleRepeat table, finish off the
    # axtChain/loadUp.csh script and 
    time nice -n +19 doBlastzChainNet.pl -verbose=2 \
	/hive/data/genomes/panTro3/bed/lastzGorGor3.2011-10-21/DEF \
	-continue=download -swap -syntenicNet \
	-noLoadChainSplit -chainMinScore=5000 -chainLinearGap=medium \
	-workhorse=hgwdev -smallClusterHub=encodek -bigClusterHub=swarm \
	> download.log 2>&1 &
    #	real    25m50.173s

    cd /hive/data/genomes/gorGor3/bed
    ln -s blastz.panTro3.swap lastz.panTro3

############################################################################
# LASTZ Bushbaby micMur1 (DONE - 2011-11-01,03 - Hiram)
    mkdir /hive/data/genomes/gorGor3/bed/lastzMicMur1.2011-11-01
    cd /hive/data/genomes/gorGor3/bed/lastzMicMur1.2011-11-01

    cat << '_EOF_' > DEF
# Gorilla vs Mouse lemur
# maximum M allowed with lastz is only 254
BLASTZ_M=254
BLASTZ_Q=/scratch/data/blastz/human_chimp.v2.q
BLASTZ_O=600
BLASTZ_E=150
# other parameters on advice from Webb
BLASTZ_K=4500
BLASTZ_Y=15000
BLASTZ_T=2

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

# QUERY: Mouse lemur micMur1
SEQ2_DIR=/scratch/data/micMur1/micMur1.2bit
SEQ2_LEN=/scratch/data/micMur1/chrom.sizes
SEQ2_CHUNK=20000000
SEQ2_LAP=0
SEQ2_LIMIT=400

BASE=/hive/data/genomes/gorGor3/bed/lastzMicMur1.2011-11-01
TMPDIR=/scratch/tmp
'_EOF_'
    # << happy emacs

    #	establish a screen to control this job
    screen
    time nice -n +19 doBlastzChainNet.pl -verbose=2 \
	`pwd`/DEF \
	-syntenicNet \
	-noLoadChainSplit -chainMinScore=5000 -chainLinearGap=medium \
	-workhorse=hgwdev -smallClusterHub=encodek -bigClusterHub=swarm \
	> do.log 2>&1 &
    #	Elapsed time: 2962m15s
    #	real    2962m15.467s

    cat fb.gorGor3.chainMicMur1Link.txt
    #	646647849 bases of 2822760080 (22.908%) in intersection

    cd /hive/data/genomes/gorGor3/bed
    ln -s lastzMicMur1.2011-11-01 lastz.micMur1

    #	running the swap
    mkdir /hive/data/genomes/micMur1/bed/blastz.gorGor3.swap
    cd /hive/data/genomes/micMur1/bed/blastz.gorGor3.swap
    time nice -n +19 doBlastzChainNet.pl -verbose=2 \
	/hive/data/genomes/gorGor3/bed/lastzMicMur1.2011-11-01/DEF \
	-swap -syntenicNet \
	-noLoadChainSplit -chainMinScore=5000 -chainLinearGap=medium \
	-workhorse=hgwdev -smallClusterHub=encodek -bigClusterHub=swarm \
	> swap.log 2>&1 &
    #	real    139m3.414s
    cat fb.micMur1.chainGorGor3Link.txt 
    #	640110238 bases of 1852394361 (34.556%) in intersection

    cd /hive/data/genomes/micMur1/bed
    ln -s blastz.gorGor3.swap lastz.gorGor3

############################################################################
# LASTZ Tarsier tarSyr1 (DONE - 2011-11-01,06 - Hiram)
    mkdir /hive/data/genomes/gorGor3/bed/lastzTarSyr1.2011-11-01
    cd /hive/data/genomes/gorGor3/bed/lastzTarSyr1.2011-11-01

    cat << '_EOF_' > DEF
# Gorilla vs Tarsier
# maximum M allowed with lastz is only 254
BLASTZ_M=254
BLASTZ_Q=/scratch/data/blastz/human_chimp.v2.q
BLASTZ_O=600
BLASTZ_E=150
# other parameters on advice from Webb
BLASTZ_K=4500
BLASTZ_Y=15000
BLASTZ_T=2

# TARGET: Gorilla gorGor3
SEQ1_DIR=/scratch/data/gorGor3/gorGor3.2bit
SEQ1_LEN=/scratch/data/gorGor3/chrom.sizes
SEQ1_CHUNK=20000000
SEQ1_LAP=10000
SEQ1_LIMIT=100

# QUERY: Tarsier tarSyr1
SEQ2_DIR=/scratch/data/tarSyr1/tarSyr1.2bit
SEQ2_LEN=/scratch/data/tarSyr1/chrom.sizes
SEQ2_CHUNK=20000000
SEQ2_LAP=0
SEQ2_LIMIT=400

BASE=/hive/data/genomes/gorGor3/bed/lastzTarSyr1.2011-11-01
TMPDIR=/scratch/tmp
'_EOF_'
    # << happy emacs

    #	establish a screen to control this job
    screen
    time nice -n +19 doBlastzChainNet.pl -verbose=2 \
	`pwd`/DEF \
	-syntenicNet \
	-noLoadChainSplit -chainMinScore=5000 -chainLinearGap=medium \
	-workhorse=hgwdev -smallClusterHub=encodek -bigClusterHub=swarm \
	> do.log 2>&1 &
    #	real    5869m15.429s

    cat fb.gorGor3.chainTarSyr1Link.txt 
    #	549926460 bases of 2822760080 (19.482%) in intersection

    cd /hive/data/genomes/gorGor3/bed
    ln -s lastzTarSyr1.2011-11-01 lastz.tarSyr1

    #	running the swap
    mkdir /hive/data/genomes/tarSyr1/bed/blastz.gorGor3.swap
    cd /hive/data/genomes/tarSyr1/bed/blastz.gorGor3.swap
    #	 swarm kluster off-line, hence bigHub=encodek, although it isn't
    #	used for this
    time nice -n +19 doBlastzChainNet.pl -verbose=2 \
	/hive/data/genomes/gorGor3/bed/lastzTarSyr1.2011-11-01/DEF \
	-swap -syntenicNet \
	-noLoadChainSplit -chainMinScore=5000 -chainLinearGap=medium \
	-workhorse=hgwdev -smallClusterHub=encodek -bigClusterHub=encodek \
	> swap.log 2>&1 &
    #	real    599m8.506s
    cat fb.tarSyr1.chainGorGor3Link.txt 
    #	567036277 bases of 2768536343 (20.481%) in intersection

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

############################################################################
# Construct download files (DONE - 2011-11-14 - Hiram)
    cd /hive/data/genomes/gorGor3
    time makeDownloads.pl gorGor3 -workhorse=hgwdev > downloads.log 2>&1 &
    #	real    24m7.903s
    # you must edit the */README files in ./goldenPath/
    # verify joinerCheck is good and gorGor3 is in all.joiner
    joinerCheck -keys -database=gorGor3 \
	$HOME/kent/src/hg/makeDb/schema/all.joiner

############################################################################
# after downloads, construct pushQ (DONE - 2011-11-14 - Hiram)
    mkdir /hive/data/genomes/gorGor3/pushQ
    cd /hive/data/genomes/gorGor3/pushQ
    time makePushQSql.pl gorGor3 > gorGor3.sql 2> stderr.log
    # check stderr.log for anything not categorized

    scp -p gorGor3.sql hgwbeta:/tmp
    ssh hgwbeta
    cd /tmp
    hgsql qapushq < gorGor3.sql
    # after loading, view the subQ in the WEB interface, go through each
    #	item and verify it can measure the tables and files

############################################################################
## 11-Way Multiz (WORKING - 2011-11-09 - Hiram)
    ssh hgwdev
    mkdir /hive/data/genomes/gorGor3/bed/multiz11way
    cd /hive/data/genomes/gorGor3/bed/multiz11way

    # species list:
    cd /hive/data/genomes/gorGor3/bed
    ls -d lastz.* | sed -e "s/lastz.//" | sort | xargs echo
# calJac3 hg19 micMur1 nomLeu1 otoGar1 panTro3 papHam1 ponAbe2 rheMac2 tarSyr1
    # plus this one
# gorGor3

    #	All distances remain as specified in the 51way.nh
    /cluster/bin/phast/tree_doctor --prune-all-but \
calJac3,hg19,micMur1,nomLeu1,otoGar1,panTro3,papHam1,ponAbe2,rheMac2,tarSyr1,gorGor3 \
	$HOME/kent/src/hg/utils/phyloTrees/51way.nh > 11way.nh
    #	what that looks like:
    cat 11way.nh
# ((((((((hg19:0.006700,panTro3:0.006667):0.002250,gorGor3:0.008825):0.009680,
# ponAbe2:0.018318):0.007000,nomLeu1:0.010000):0.007000,(rheMac2:0.007853,
# papHam1:0.007637):0.029618):0.021965,calJac3:0.066131):0.057590,
# tarSyr1:0.137823):0.011062,(micMur1:0.092749,otoGar1:0.129725):0.035463);


    #	rearrange to get gorGor3 on top:
    cat << '_EOF_' > gorGor3.11way.nh
(((((((gorGor3:0.008825,(hg19:0.006700,panTro3:0.006667):0.002250):0.009680,
ponAbe2:0.018318):0.007000,nomLeu1:0.010000):0.007000,(rheMac2:0.007853,
papHam1:0.007637):0.029618):0.021965,calJac3:0.066131):0.057590,
tarSyr1:0.137823):0.011062,(micMur1:0.092749,otoGar1:0.129725):0.035463);
'_EOF_'
    # << happy emacs

    # convert to species names
    /cluster/bin/phast/tree_doctor --rename \
"gorGor3->Gorilla; hg19->Human; panTro3->Chimp; ponAbe2->Orangutan; nomLeu1->Gibbon; rheMac2->Rhesus; papHam1->Baboon; calJac3->Marmoset; tarSyr1->Tarsier; micMur1->Mouse_lemur; otoGar1->Bushbaby" \
gorGor3.11way.nh > gorGor3.commonNames.11way.nh

# (((((((Gorilla:0.008825,(Human:0.006700,Chimp:0.006667):0.002250):0.009680,
# Orangutan:0.018318):0.007000,Gibbon:0.010000):0.007000,
# (Rhesus:0.007853,Baboon:0.007637):0.029618):0.021965,
# Marmoset:0.066131):0.057590,Tarsier:0.137823):0.011062,
# (Mouse_lemur:0.092749,Bushbaby:0.129725):0.035463);

    #	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/gorGor3_11way.png

    /cluster/bin/phast/all_dists gorGor3.11way.nh > 11way.distances.txt
    #	Use this output to create the table below
    grep -i gorGor3 11way.distances.txt | sort -k3,3n
# gorGor3 panTro3 0.017742
# gorGor3 hg19    0.017775
# gorGor3 nomLeu1 0.035505
# gorGor3 ponAbe2 0.036823
# gorGor3 papHam1 0.069760
# gorGor3 rheMac2 0.069976
# gorGor3 calJac3 0.120601
# gorGor3 tarSyr1 0.249883
# gorGor3 micMur1 0.251334
# gorGor3 otoGar1 0.288310

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

#                         featureBits chainLink measures
#                                        chainGorGor3Link  chain linearGap
#    distance                    on gorGor3  on other     minScore
#  1  0.018 - chimp panTro3      (% 89.04) (% 88.04)       5000     medium
#  2  0.018 - human hg19         (% 91.11) (% 89.88)       5000     medium
#  3  0.036 - gibbon nomLeu1     (% 83.48) (% 84.12)       5000     medium
#  4  0.037 - orangutan ponAbe2  (% 86.35) (% 83.55)       5000     medium
#  5  0.070 - baboon papHam1     (% 79.43) (% 80.87)       5000     medium
#  6  0.070 - rhesus rheMac2     (% 79.24) (% 82.19)       5000     medium
#  7  0.121 - marmoset calJac3   (% 68.51) (% 70.07)       5000     medium
#  8  0.250 - tarsier tarSyr1    (% 19.48) (% 20.48)       5000     medium
#  9  0.251 - mouse lemur micMur1(% 22.91) (% 34.56)       5000     medium
# 10  0.288 - bushbaby otoGar1   (% 12.02) (% 17.08)       5000     medium

# 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' \
	gorGor3.11way.nh > tmp.nh
    echo `cat tmp.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/gorGor3/bed/multiz11way/mafSplit
    cd /hive/data/genomes/gorGor3/bed/multiz11way/mafSplit
    for D in `sed -e "s/gorGor3 //" ../species.list`
do
    echo "${D}"
    mkdir $D
    cd $D
    echo "mafSplit -byTarget -useHashedName=10 /dev/null . ../../../lastz.${D}/axtChain/gorGor3.${D}.synNet.maf.gz"
    mafSplit -byTarget -useHashedName=8 /dev/null . \
	../../../lastz.${D}/axtChain/gorGor3.${D}.synNet.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
    # 1071
    find . -type f | grep ".maf$" | xargs -L 1 basename | sort -u > maf.list
    wc -l maf.list
    # 206 maf.list

    mkdir /hive/data/genomes/gorGor3/bed/multiz11way/splitRun
    cd /hive/data/genomes/gorGor3/bed/multiz11way/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 = gorGor3
set c = $1
set result = $2
set run = `/bin/pwd`
set tmp = /scratch/tmp/$db/multiz.$c
set pairs = /hive/data/genomes/gorGor3/bed/multiz11way/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/gorGor3/bed/multiz11way/splitRun/maf/$(root1).maf}
#ENDLOOP
'_EOF_'
# << happy emacs

    ln -s ../../mafSplit/maf.list maf.list
    ssh swarm
    cd /hive/data/genomes/gorGor3/bed/multiz11way/splitRun/run
    # the tac reverses the list to get the small jobs first
    gensub2 maf.list single template stdout | tac > jobList
    # the ram=8g will allow only one job on a node to slow them down
    para -ram=8g create jobList
# Completed: 206 of 206 jobs
# CPU time in finished jobs:     436154s    7269.23m   121.15h    5.05d  0.014 y
# IO & Wait Time:                 38798s     646.64m    10.78h    0.45d  0.001 y
# Average job time:                2306s      38.43m     0.64h    0.03d
# Longest finished job:           37853s     630.88m    10.51h    0.44d
# Submission to last job:         37884s     631.40m    10.52h    0.44d

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

# -rw-rw-r--  1 25370474447 Dec  1 15:33 multiz11way.maf

    # Load into database
    ssh hgwdev
    cd /hive/data/genomes/gorGor3/bed/multiz11way
    mkdir /gbdb/gorGor3/multiz11way
    ln -s `pwd`/multiz11way.maf /gbdb/gorGor3/multiz11way
    cd /scratch/tmp
    time nice -n +19 hgLoadMaf gorGor3 multiz11way
    #	Indexing and tabulating /gbdb/gorGor3/multiz11way/multiz11way.maf
    #	Loaded 12607889 mafs in 1 files from /gbdb/gorGor3/multiz11way
    #	real    12m16.842s

    time nice -n +19 hgLoadMafSummary -verbose=2 -minSize=30000 \
	-mergeGap=1500 -maxSize=200000 gorGor3 multiz11waySummary \
	/gbdb/gorGor3/multiz11way/multiz11way.maf
    #	Created 1350677 summary blocks from 84367135 components
    #	and 12607889 mafs from /gbdb/gorGor3/multiz11way/multiz11way.maf
    #	real    17m3.441s

    wc -l multiz11way*.tab
    #	12607889 multiz11way.tab
    #	1350677 multiz11waySummary.tab
    #	13958566 total

    rm multiz11way*.tab

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