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

# Caenorhabditis japonica
#	Washington University School of Medicine GSC and Sanger Institute
#
#	http://www.ncbi.nlm.nih.gov/Traces/wgs/?val=ABLE03

###########################################################################
## Download sequence (DONE - 2011-06-03 - Hiram)
    mkdir /hive/data/genomes/caeJap4
    cd /hive/data/genomes/caeJap4
    mkdir genbank
    cd genbank
    wget --no-parent --timestamping -m -nH --cut-dirs=7 \
	ftp://ftp.ncbi.nlm.nih.gov/genbank/genomes/Eukaryotes/invertebrates/Caenorhabditis_japonica/C_japonica-7.0.1/

    faSize unplaced_scaffolds/FASTA/unplaced.scaf.fa.gz
    #	166256191 bases (12198257 N's 154057934 real 154057934 upper 0 lower)
    #	in 18817 sequences in 1 files

    # change the names from GL numbers to scaffold numbers
    cat << '_EOF_' > scafNames.pl
#!/bin/env perl

use strict;
use warnings;

my $argc = scalar(@ARGV);

if ($argc != 1) {
    printf STDERR "usage: scafNames.pl makeItSo\n";
    printf STDERR "via the localID2acc file translate names\n";
    printf STDERR "in the AGP and FASTA files to construct UCSC versions.\n";
}

my %scafName;	# index is GL name, value is scaffold name

open (FH, "<localID2acc") or die "can not read localID2acc";
while (my $line = <FH>) {
    chomp $line;
    my ($scaf, $glName) = split('\s+', $line);
    die "ERROR: duplicate glName: $glName" if (exists($scafName{$glName}));
    $scafName{$glName} = $scaf;
}
close (FH);

open (FA, "|gzip -c > caeJap4.scaf.agp.gz") or die "can not write to gzip -c > caeJap4.scaf.agp.gz";
open (FH, "zcat unplaced_scaffolds/AGP/unplaced.scaf.agp.gz|") or
    die "can not read unplaced_scaffolds/AGP/unplaced.scaf.agp.gz";
while (my $line = <FH>) {
    if ($line =~ m/^GL/) {
	chomp $line;
	my ($glName, $rest) = split('\s+', $line, 2);
	printf FA "%s\t%s\n", $scafName{$glName}, $rest;
    } else {
	printf FA "%s", $line;
    }
}
close (FH);
close (FA);

open (FA, "|gzip -c > caeJap4.scaf.fa.gz") or die "can not write to gzip -c > caeJap4.scaf.fa.gz";
open (FH, "zcat unplaced_scaffolds/FASTA/unplaced.scaf.fa.gz|") or
	die "can not read unplaced_scaffolds/FASTA/unplaced.scaf.fa.gz";
while (my $line = <FH>) {
    if ($line =~ m/^>/) {
	chomp $line;
	$line =~ s/.*gb.GL/GL/;
	$line =~ s/. Caeno.*//;
	printf FA ">%s\n", $scafName{$line};
	
    } else {
	printf FA "%s", $line;
    }
}
close (FH);
close (FA);
'_EOF_'
    # << happy emacs
    chmod +x ./scafNames.pl
    time ./scafNames.pl makeItSo

    faSize caeJap4.scaf.fa.gz
    #	166256191 bases (12198257 N's 154057934 real 154057934 upper 0 lower)
    #	in 18817 sequences in 1 files

    checkAgpAndFa  caeJap4.scaf.agp.gz caeJap4.scaf.fa.gz 2>&1 | tail -1
    #	All AGP and FASTA entries agree - both files are valid

###########################################################################
## Initial sequence (DONE - 2011-06-03 - Hiram)
    cd /hive/data/genomes/caeJap4
    cat << '_EOF_' > caeJap4.config.ra
# Config parameters for makeGenomeDb.pl:
db caeJap4
# clade worm
# genomeCladePriority 70
scientificName Caenorhabditis japonica
commonName C. japonica
assemblyDate Aug. 2010
assemblyLabel Washington University School of Medicine GSC C. japonica 7.0.1 (GCA_000147155.1)
assemblyShortLabel WUSTL 7.0.1
orderKey 879
mitoAcc none
fastaFiles /hive/data/genomes/caeJap4/genbank/caeJap4.scaf.fa.gz
agpFiles /hive/data/genomes/caeJap4/genbank/caeJap4.scaf.agp.gz
# qualFiles none
dbDbSpeciesDir worm
taxId 281687
'_EOF_'
    # << happy emacs

    mkdir jkStuff
    #	run just to AGP to make sure things are sane first
    time nice -n +19 makeGenomeDb.pl caeJap4.config.ra -stop=agp \
      > jkStuff/makeGenomeDb.agp.log 2>&1
    #	real    0m27.323s
    #	check that log to verify it has no errors
    #	now, continuing to make the Db and all
    time nice -n +19 makeGenomeDb.pl caeJap4.config.ra -continue=db \
      > jkStuff/makeGenomeDb.db.log 2>&1
    #	real    1m24.650s
    #	check that log to verify it has no errors

    #	take the trackDb business there and check it into the source tree
    #	fixup the description, gap and gold html page descriptions

###########################################################################
## RepeatMasker (DONE - 2011-06-03 - Hiram)
    mkdir /hive/data/genomes/caeJap4/bed/repeatMasker
    cd /hive/data/genomes/caeJap4/bed/repeatMasker
    time nice -n +19 doRepeatMasker.pl -noSplit -bigClusterHub=swarm \
	-buildDir=`pwd` caeJap4 > do.log 2>&1 &
    #	real    35m15.202s

    #	from the do.log:
# RepeatMasker version development-$Id: RepeatMasker,v
#	1.25 2010/09/08 21:32:26 angie Exp $
#	CC   RELEASE 20090604; 

    cat faSize.rmsk.txt
# 166256191 bases (12198257 N's 154057934 real 150565567 upper 3492367 lower)
#	in 18817 sequences in 1 files
# %2.10 masked total, %2.27 masked real

###########################################################################
## Simple Repeats (DONE - 2011-06-03 - Hiram)
    mkdir /cluster/data/caeJap4/bed/simpleRepeat
    cd /cluster/data/caeJap4/bed/simpleRepeat
    time nice -n +19 doSimpleRepeat.pl -smallClusterHub=memk \
	-workhorse=hgwdev -buildDir=`pwd` caeJap4 > do.log 2>&1 &
    #	real     23m51.502s
    cat fb.simpleRepeat 
    #	7800328 bases of 154057934 (5.063%) in intersection

###########################################################################
## WindowMasker (DONE - 2011-06-03 - Hiram)
    ssh hgwdev
    mkdir /hive/data/genomes/caeJap4/bed/windowMasker
    cd /hive/data/genomes/caeJap4/bed/windowMasker
    time nice -n +19 doWindowMasker.pl -verbose=2 -buildDir=`pwd` \
	-workhorse=hgwdev caeJap4 > do.log 2>&1 &
    #	real    7m5.537s

    twoBitToFa caeJap4.wmsk.sdust.2bit stdout | faSize stdin
# 166256191 bases (12198257 N's 154057934 real 82139414 upper 71918520 lower)
#	in 18817 sequences in 1 files
# %43.26 masked total, %46.68 masked real

    #	load this initial data to get ready to clean it
    cd /hive/data/genomes/caeJap4/bed/windowMasker
    hgLoadBed caeJap4 windowmaskerSdust windowmasker.sdust.bed.gz
    #	Loaded 1023570 elements of size 3
    featureBits -countGaps caeJap4 windowmaskerSdust
    #	84116777 bases of 166256191 (50.595%) in intersection

    #	eliminate the gaps from the masking
    featureBits caeJap4 -not gap -bed=notGap.bed
    #	154057934 bases of 154057934 (100.000%) in intersection
    time nice -n +19 featureBits caeJap4 windowmaskerSdust notGap.bed \
	-bed=stdout | gzip -c > cleanWMask.bed.gz
    #	71918520 bases of 154057934 (46.683%) in intersection

    #	reload track to get it clean
    hgLoadBed caeJap4 windowmaskerSdust cleanWMask.bed.gz
    #	Loaded 1028222 elements of size 4
    featureBits -countGaps caeJap4 windowmaskerSdust
    #	71918520 bases of 166256191 (43.258%) in intersection


    #	mask the sequence with this clean mask
    zcat cleanWMask.bed.gz \
	| twoBitMask ../../caeJap4.unmasked.2bit stdin \
	    -type=.bed caeJap4.cleanWMSdust.2bit
    twoBitToFa caeJap4.cleanWMSdust.2bit stdout | faSize stdin \
        > caeJap4.cleanWMSdust.faSize.txt
    cat caeJap4.cleanWMSdust.faSize.txt
# 166256191 bases (12198257 N's 154057934 real 82139414 upper 71918520 lower)
#	in 18817 sequences in 1 files
# %43.26 masked total, %46.68 masked real

########################################################################
# MASK SEQUENCE WITH WM+TRF (DONE - 2011-06-03 - Hiram)
    cd /hive/data/genomes/caeJap4
    twoBitMask -add bed/windowMasker/caeJap4.cleanWMSdust.2bit \
	bed/simpleRepeat/trfMask.bed caeJap4.2bit
    #	safe to ignore the warnings about BED file with >=13 fields
    twoBitToFa caeJap4.2bit stdout | faSize stdin > faSize.caeJap4.txt
    cat faSize.caeJap4.txt
# 166256191 bases (12198257 N's 154057934 real 81999301 upper 72058633 lower)
#	in 18817 sequences in 1 files
# %43.34 masked total, %46.77 masked real

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

#########################################################################
# MAKE 11.OOC FILE FOR BLAT (DONE - 2011-06-03 - Hiram)
    # numerator is caeJap4 gapless bases "real" as reported by faSize 
    # denominator is hg19 gapless bases "real" as reported by faSize
    # 1024 is threshold used for human -repMatch:
    calc \( 154057934 / 2897310462 \) \* 1024
    #	calc \( 154057934 / 2897310462 \) \* 1024 = 54.448885

    # Round up to use -repMatch=100 since 50 would result in too many
    cd /hive/data/genomes/caeJap4
    blat caeJap4.2bit /dev/null /dev/null -tileSize=11 \
      -makeOoc=jkStuff/caeJap4.11.ooc -repMatch=100
    #	Wrote 21319 overused 11-mers to jkStuff/caeJap4.11.ooc
    # there are no non-bridged gaps here to make a lift file from
    # cd jkStuff
    # gapToLift -verbose=2 caeJap4 caeJap4.nonBridged.lift -bedFile=caeJap4.nonBridged.bed

    mkdir /hive/data/staging/data/caeJap4
    cp -p chrom.sizes caeJap4.2bit jkStuff/caeJap4.11.ooc \
	/hive/data/staging/data/caeJap4

#########################################################################
# GENBANK AUTO UPDATE (DONE - 2011-06-03,07 - Hiram)
    # align with latest genbank process.
    ssh hgwdev
    cd ~/kent/src/hg/makeDb/genbank
    git pull

    # edit etc/genbank.conf to add caeJap4 just before caeRem3

# caeJap4 (C. japonica)
caeJap4.serverGenome = /hive/data/genomes/caeJap4/caeJap4.2bit
caeJap4.clusterGenome = /scratch/data/caeJap4/caeJap4.2bit
caeJap4.ooc = /scratch/data/caeJap4/caeJap4.11.ooc
caeJap4.lift = no
caeJap4.refseq.mrna.native.pslCDnaFilter  = ${lowCover.refseq.mrna.native.pslCDnaFilter}
caeJap4.refseq.mrna.xeno.pslCDnaFilter    = ${lowCover.refseq.mrna.xeno.pslCDnaFilter}
caeJap4.genbank.mrna.native.pslCDnaFilter = ${lowCover.genbank.mrna.native.pslCDnaFilter}
caeJap4.genbank.mrna.xeno.pslCDnaFilter   = ${lowCover.genbank.mrna.xeno.pslCDnaFilter}
caeJap4.genbank.est.native.pslCDnaFilter  = ${lowCover.genbank.est.native.pslCDnaFilter}
caeJap4.refseq.mrna.native.load = yes
caeJap4.refseq.mrna.xeno.load  = yes
caeJap4.refseq.mrna.xeno.loadDesc = yes
caeJap4.genbank.mrna.xeno.load = yes
caeJap4.genbank.est.native.load = yes
caeJap4.genbank.est.native.loadDesc = no
caeJap4.downloadDir = caeJap4
caeJap4.perChromTables = no

    git commit -m "Added caeJap4 C. japonica GCA_000147155.1" etc/genbank.conf
    git push
    # update /cluster/data/genbank/:
    make etc-update

    screen		#	use a screen to manage this job
    cd /cluster/data/genbank
    time nice -n +19 bin/gbAlignStep -initial caeJap4 &
    #	logFile:  var/build/logs/2011.06.03-11:48:21.caeJap4.initalign.log
    #	real    457m45.267s

    # load database when finished
    ssh hgwdev
    cd /cluster/data/genbank
    time nice -n +19 ./bin/gbDbLoadStep -drop -initialLoad caeJap4
    #	logFile: var/dbload/hgwdev/logs/2011.06.07-13:36:39.dbload.log
    #	real    21m36.966s

    # enable daily alignment and update of hgwdev
    cd ~/kent/src/hg/makeDb/genbank
    git pull
    # add caeJap4 to:
        etc/align.dbs
        etc/hgwdev.dbs
    git push
    make etc-update

#########################################################################
# lastz swap ce10 to caeJap4 (DONE - 2011-06-07 - Hiram)
    #	original alignment on ce10
    cd /hive/data/genomes/ce10/bed/lastzCaeJap4.2011-06-07
    cat fb.ce10.chainCaeJap4Link.txt 
    #	27815993 bases of 100286070 (27.737%) in intersection

    mkdir /hive/data/genomes/caeJap4/bed/blastz.ce10.swap
    cd /hive/data/genomes/caeJap4/bed/blastz.ce10.swap
    time nice -n +19 doBlastzChainNet.pl -verbose=2 \
	/hive/data/genomes/ce10/bed/lastzCaeJap4.2011-06-07/DEF \
	-syntenicNet -workhorse=hgwdev -bigClusterHub=swarm \
	-smallClusterHub=encodek -swap > swap.log 2>&1 &
    #	real    4m11.401s

    cat fb.caeJap4.chainCe10Link.txt
    #	30128569 bases of 154057934 (19.557%) in intersection

#########################################################################
# Constructing Downloads (DONE - 2011-06-10 - Hiram)
    cd /hive/data/genomes/caeJap4
    time makeDownloads.pl -dbHost=hgwdev -workhorse=hgwdev -verbose=2 caeJap4 \
	 > downloads.log 2>&1
    #	real    1m16.504s
    # fixup the README files constructed in goldenPath/*/README.txt

    # add window masker bed file:
    cp -p bed/windowMasker/cleanWMask.bed.gz \
	goldenPath/bigZips/chromWMSdust.bed.gz

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