#! /bin/bash
### env fasta=xxx.fa blastp=xxx.blastp.gz onam=xxx odir=yyy datad=`pwd` qsub -q shared omclrun.sh
#PBS -N omclevgrun
#PBS -A ind114
#PBS -l nodes=1:ppn=32,walltime=19:55:00
#PBS -V

## redo try removing bopts -identmin
## 20140831 update, adjust identmin down, or drop; fixed blast92orthomcl10 BITSMAXFILT
# dbset7/arp7s10b14.aa = 10 species, no ticks; arp7s12a14 = 12 spp, 2 large mRNA tick sets
ncpu=16
ntaxa=10
domake=1

## DANG forgot again otabopts myspecies=daph
datev=`date "+%y%m%d"`
otabopts="myspecies=daph idprefix=ARP7f_G date=$datev clade=Arthropods goodname=mayzebr,human,dromel,tribcas" 

if [ "X" = "X$datad" ]; then echo "ERR: env datad=path/to/data"; exit -1; fi
if [ "X" = "X$blastp" ]; then echo "ERR: env blastp=myspecies.blastp.gz"; exit -1; fi
if [ "X" = "X$fasta" ]; then echo "ERR: env fasta=myspecies.prot.fasta"; exit -1; fi

evigene=$HOME/bio/evigene
orlib=$evigene/scripts/omcl
mcl=$HOME/bio/mcl9/bin/mcl

#o.bopts="-identmin 33"; bopts="-identmin 25"
#o.bopts="-identmin 15"
bopts=""

## make onam, odir from fasta or blastp name
if [ "X" = "X$onam" ]; then 
  onam=`basename $fasta .fasta | sed 's/\.aa.*//; s/\.pep.*//; s/\.fa.*//; s/$/_omcl/;'`
fi
if [ "X" = "X$odir" ]; then
  odir="${onam}$$"
fi

echo "START omcl : `date` "
cd $datad

if [ $domake = 1 ]; then
 $evigene/scripts/blast92orthomcl10.pl $bopts  -fasta=$fasta -in=$blastp -out=$onam
fi

export MCL=$mcl 
export ORTHOMCL=$datad/

perl -I$orlib $orlib/orthomcl_evg.pl --mode par_start --former_run_dir $odir --bpo=$onam.bpo --gg=$onam.gg 

t=0; i=0; 
while [ $t -lt $ntaxa ]; do { 
 perl -I$orlib $orlib/orthomcl_evg.pl --mode par_part$t --former_run_dir $odir --bpo=$onam.bpo --gg=$onam.gg & 
 
 t=$(( $t + 1 )); i=$(( $i + 1 )); 
 if [ $i -ge $ncpu ]; then wait; i=0; fi;
} done 
wait

## now do connect$t loop
t=0; i=0;
while [ $t -lt $ntaxa ]; do {
 perl -I$orlib $orlib/orthomcl_evg.pl --mode par_connect$t --former_run_dir $odir --bpo=$onam.bpo --gg=$onam.gg &

  t=$(( $t + 1 )); i=$(( $i + 1 ));
  if [ $i -ge $ncpu ]; then wait; i=0; fi;
} done
wait

perl -I$orlib $orlib/orthomcl_evg.pl --mode par_end --former_run_dir $odir --bpo=$onam.bpo --gg=$onam.gg 

## add this tabulation
env $otabopts $evigene/scripts/omcl/orthomcl_tabulate.pl -debug -omclpath ./ -namepath ../names/  >& log.otab7b

echo "DONE omcl : `date`" 

