04737fb7f583ed3168dc5594e0cb8403efccd911
hiram
  Thu Jan 30 17:18:50 2020 -0800
protect against zero results refs #23891

diff --git src/hg/utils/automation/doXenoRefGene.pl src/hg/utils/automation/doXenoRefGene.pl
index 1f3e3f5..a522341 100755
--- src/hg/utils/automation/doXenoRefGene.pl
+++ src/hg/utils/automation/doXenoRefGene.pl
@@ -277,77 +277,93 @@
     die "doMakeGp: the previous step filterPsl did not complete \n" .
       "successfully ($buildDir/$db.xenoRefGene.psl does not exist).\nPlease " .
       "complete the previous step: -continue=filterPsl\n";
   } elsif (-e "$runDir/$db.xenoRefGene.bb" ) {
     die "doMakeGp: looks like this was run successfully already\n" .
       "($db.xenoRefGene.bb exists).  Either run with -continue cleanup\n" .
 	"or move aside/remove $runDir/$db.xenoRefGene.bb\nand run again.\n";
   }
 
   my $whatItDoes = "Makes bigGenePred.bb file from filterPsl output.";
   my $bossScript = newBash HgRemoteScript("$runDir/makeGp.bash", $workhorse,
 				      $runDir, $whatItDoes);
 
   $bossScript->add(<<_EOF_
 export db=$db
+if [ -s "\$db.xenoRefGene.psl" ]; then
   grep NR_ \$db.xenoRefGene.psl > NR.psl
   grep NM_ \$db.xenoRefGene.psl > NM.psl
   mrnaToGene -cdsDb=hgFixed NM.psl NM.gp
   mrnaToGene -noCds NR.psl NR.gp
   cat NM.gp NR.gp | genePredSingleCover stdin \$db.xenoRefGene.gp
   genePredCheck -db=\$db -chromSizes=\$db.chrom.sizes \$db.xenoRefGene.gp
   genePredToBigGenePred -geneNames=$mrnas/geneOrgXref.txt \$db.xenoRefGene.gp \\
      stdout | sort -k1,1 -k2,2n > \$db.bgpInput
   sed -e 's#Alternative/human readable gene name#species of origin of the mRNA#; s#Name or ID of item, ideally both human readable and unique#RefSeq accession id#; s#Primary identifier for gene#gene name#;' \\
     \$HOME/kent/src/hg/lib/bigGenePred.as > xenoRefGene.as
   bedToBigBed -extraIndex=name,geneName -type=bed12+8 -tab -as=xenoRefGene.as \\
      \$db.bgpInput \$db.chrom.sizes \$db.xenoRefGene.bb
   \$HOME/kent/src/hg/utils/automation/xenoRefGeneIx.pl \$db.bgpInput | sort -u > \$db.ix.txt
   ixIxx \$db.ix.txt \$db.xenoRefGene.ix \$db.xenoRefGene.ixx
+fi
 _EOF_
   );
   $bossScript->execute();
 } # doMakeGp
 
 #########################################################################
 # * step: cleanup [workhorse]
 sub doCleanup {
   my $runDir = $buildDir;
 
   if (-e "$runDir/$db.xenoRefGene.gp.gz" ) {
     die "doCleanup: looks like this was run successfully already\n" .
       "($db.xenoRefGene.gp.gz exists).  Investigate the run directory:\n" .
 	" $runDir/\n";
   }
   my $whatItDoes = "It cleans up or compresses intermediate files.";
   my $bossScript = newBash HgRemoteScript("$runDir/cleanup.bash", $workhorse,
 				      $runDir, $whatItDoes);
   $bossScript->add(<<_EOF_
 export db="$db"
 rm -fr $buildDir/target/
 rm -fr $buildDir/blatRun/err/
 rm -fr $buildDir/blatRun/result/
 rm -f $buildDir/blatRun/batch.bak
 rm -f $buildDir/NM.gp
 rm -f $buildDir/NR.gp
 rm -f $buildDir/NM.psl
 rm -f $buildDir/NR.psl
+if [ -s "$buildDir/\$db.bgpInput" ]; then
   gzip $buildDir/\$db.bgpInput &
+fi
+if [ -s "$buildDir/\$db.ix.txt" ]; then
   gzip $buildDir/\$db.ix.txt &
+fi
+if [ -s "$buildDir/\$db.all.psl" ]; then
   gzip $buildDir/\$db.all.psl &
+else
+  rm -f $buildDir/\$db.all.psl
+fi
+if [ -s "$buildDir/\$db.xenoRefGene.psl" ]; then
   gzip $buildDir/\$db.xenoRefGene.psl &
+else
+  rm -f $buildDir/\$db.xenoRefGene.psl
+fi
+if [ -s "$buildDir/\$db.xenoRefGene.gp" ]; then
 gzip $buildDir/\$db.xenoRefGene.gp
+fi
 wait
 _EOF_
   );
   $bossScript->execute();
 } # doCleanup
 
 #########################################################################
 # main
 
 # Prevent "Suspended (tty input)" hanging:
 &HgAutomate::closeStdin();
 
 # Make sure we have valid options and exactly 1 argument:
 &checkOptions();
 &usage(1) if (scalar(@ARGV) != 1);