c8828612e3aba9e6c3072768129de10abe337878
hiram
  Fri May 29 11:29:04 2020 -0700
now allow orderList to be specifically stated and begin common set of html functions refs #23734

diff --git src/hg/makeDb/doc/asmHubs/trackData.pl src/hg/makeDb/doc/asmHubs/trackData.pl
index ab7922d..55b31ca 100755
--- src/hg/makeDb/doc/asmHubs/trackData.pl
+++ src/hg/makeDb/doc/asmHubs/trackData.pl
@@ -1,521 +1,502 @@
 #!/usr/bin/env perl
 
 use strict;
 use warnings;
+use FindBin qw($Bin);
+use lib "$Bin";
+use commonHtml;
 use File::stat;
 
 my $argc = scalar(@ARGV);
 if ($argc < 3) {
   printf STDERR "usage: trackData.pl Name asmHubName [two column name list] > trackData.html\n";
   printf STDERR "e.g.: trackData.pl Mammals mammals mammals.asmId.commonName.tsv > trackData.html\n";
   printf STDERR "the name list is found in \$HOME/kent/src/hg/makeDb/doc/asmHubs/\n";
   printf STDERR "\nthe two columns are 1: asmId (accessionId_assemblyName)\n";
   printf STDERR "column 2: common name for species, columns separated by tab\n";
   exit 255;
 }
 
+my $home = $ENV{'HOME'};
+my $toolsDir = "$home/kent/src/hg/makeDb/doc/asmHubs";
+
 my $testOutput = 0;
 my $spliceOut = -1;
 
 if ($argc > 2) {
   for (my $i = 0; $i < $argc; ++$i) {
     if ($ARGV[$i] =~ /-test/) {
       $testOutput = 1;
       $spliceOut = $i;
     }
   }
 }
 if ($spliceOut != -1) {
   splice @ARGV, $spliceOut, 1;
 }
 my $Name = shift;
 my $asmHubName = shift;
-my $commonNameOrder = shift;
-
-my $home = $ENV{'HOME'};
-my $toolsDir = "$home/kent/src/hg/makeDb/doc/asmHubs";
+my $inputList = shift;
+my $orderList = $inputList;
+if ( ! -s "$orderList" ) {
+  $orderList = $toolsDir/$inputList;
+}
 
-my @orderList;	# asmId of the assemblies in order from the commonNameOrder file
+my @orderList;	# asmId of the assemblies in order from the orderList file
 my %commonName;	# key is asmId, value is a common name, perhaps more appropriate
                 # than found in assembly_report file
 		# assembly_report
 my $vgpIndex = 0;
 $vgpIndex = 1 if ($Name =~ m/vgp/i);
 
 my $assemblyTotal = 0;	# complete list of assemblies in this group
 my $asmCount = 0;	# count of assemblies completed and in the table
 my $overallNucleotides = 0;
 my $overallSeqCount = 0;
 my $overallGapSize = 0;
 my $overallGapCount = 0;
 
 ##############################################################################
 # from Perl Cookbook Recipe 2.17, print out large numbers with comma delimiters:
 ##############################################################################
 sub commify($) {
     my $text = reverse $_[0];
     $text =~ s/(\d\d\d)(?=\d)(?!\d*\.)/$1,/g;
     return scalar reverse $text
 }
 
 # ($itemCount, $percentCover) = oneTrackData($asmId, $track, $trackFile, $totalSize, $trackFb, $runDir);
 # might have a track feature bits file (trackFb), maybe not
 sub oneTrackData($$$$$$) {
   my ($asmId, $trackName, $file, $genomeSize, $trackFb, $runDir) = @_;
 # printf STDERR "# %s\n", $file;
   my $itemCount = 0;
   my $percentCover = 0;
   if (! -s "${file}") {
     if ($trackName eq "gapOverlap") {
       if (-s "${runDir}/$asmId.gapOverlap.bed.gz" ) {
        my $lineCount=`zcat "${runDir}/$asmId.gapOverlap.bed.gz" | head | wc -l`;
         chomp $lineCount;
        if (0 == $lineCount) {
          return("0", "0 %");
        } else {
          return("n/a", "n/a");
        }
       }
     } elsif ($trackName eq "gap") {
       return("0", "0 %");
     } else {
       return("n/a", "n/a");
     }
   } else {
     if ($file =~ m/.bw$/) {
       my $bigWigInfo = `bigWigInfo "$file" | egrep "basesCovered:|mean:" | awk '{print \$NF}' | xargs echo | sed -e 's/,//g;'`;
       chomp $bigWigInfo;
       my ($bases, $mean) = split('\s+', $bigWigInfo);
       $percentCover = sprintf("%.2f %%", 100.0 * $bases / $genomeSize);
       $itemCount = sprintf ("%.2f", $mean);
 # printf STDERR "# bigWigInfo %s %s %s\n", $itemCount, $percentCover, $file;
     } else {
       my $bigBedInfo = `bigBedInfo "$file" | egrep "itemCount:|basesCovered:" | awk '{print \$NF}' | xargs echo | sed -e 's/,//g;'`;
       chomp $bigBedInfo;
       my ($items, $bases) = split('\s', $bigBedInfo);
       $itemCount = commify($items);
       $percentCover = sprintf("%.2f %%", 100.0 * $bases / $genomeSize);
 #             56992654 bases of 2616369673 (2.178%) in intersection
       if ( -s "${trackFb}" ) {
           my ($itemBases, undef, undef, $noGapSize, undef) = split('\s+', `cat $trackFb`, 5);
           $percentCover = sprintf("%.2f %%", 100.0 * $itemBases / $noGapSize);
       }
 # printf STDERR "# bigBedInfo %s %s %s\n", $itemCount, $percentCover, $file;
     }
   }
   return ($itemCount, $percentCover);
 }
 
 ##############################################################################
 ### start the HTML output
 ##############################################################################
 sub startHtml() {
 
 my $timeStamp = `date "+%F"`;
 chomp $timeStamp;
 
 my $subSetMessage = "subset of $asmHubName only";
 if ($asmHubName eq "vertebrate") {
    $subSetMessage = "subset of other ${asmHubName}s only";
 }
 
 if ($vgpIndex) {
+  my $vgpSubset = "(set of primary assemblies)";
+  if ($orderList =~ m/vgp.alternate/) {
+     $vgpSubset = "(set of alternate/haplotype assemblies)";
+  } elsif ($orderList =~ m/vgp.trio/) {
+     $vgpSubset = "(set of trio assemblies, maternal/paternal)";
+  } elsif ($orderList =~ m/vgp.legacy/) {
+     $vgpSubset = "(set of legacy/superseded assemblies)";
+  }
   print <<"END"
 <!DOCTYPE HTML 4.01 Transitional>
 <!--#set var="TITLE" value="VGP - Vertebrate Genomes Project assembly hubs, track statistics" -->
 <!--#set var="ROOT" value="../.." -->
 
 <!--#include virtual="\$ROOT/inc/gbPageStartHardcoded.html" -->
 
 <h1>VGP - Vertebrate Genomes Project assembly hubs, track statistics</h1>
 <p>
 <a href='https://vertebrategenomesproject.org/' target=_blank>
 <img src='VGPlogo.png' width=280 alt='VGP logo'></a></p>
 <p>
 This assembly hub contains assemblies released
 by the <a href='https://vertebrategenomesproject.org/' target=_blank>
-Vertebrate Genomes Project.</a>
+Vertebrate Genomes Project.</a> $vgpSubset
 </p>
 
 END
 } else {
   print <<"END"
 <!DOCTYPE HTML 4.01 Transitional>
 <!--#set var="TITLE" value="$Name genomes assembly hubs, track statistics" -->
 <!--#set var="ROOT" value="../.." -->
 
 <!--#include virtual="\$ROOT/inc/gbPageStartHardcoded.html" -->
 
 <h1>$Name Genomes assembly hubs, track statistics</h1>
 <p>
 Assemblies from NCBI/Genbank/Refseq sources, $subSetMessage.
 </p>
 
 END
 }
   my $indexUrl = "index";
   $indexUrl = "testIndex" if ($testOutput);
   my $asmStats = "asmStats";
   $asmStats = "testAsmStats" if ($testOutput);
   print <<"END"
 <h3>See also: <a href='$indexUrl.html'>hub access</a>,&nbsp;<a href='$asmStats.html'>assembly statistics</a></h3><br>
 
 <h3>Data resource links</h3>
 NOTE: <em>Click on the column headers to sort the table by that column</em><br>
 The <em>link to genome browser</em> will attach only that single assembly to
 the genome browser.<br>
 The numbers are: item count (percent coverage)<br>
 Except for the gc5Base column which is: overall GC % average (percent coverage)
 END
 }	#	sub startHtml()
 
 # order of columns in the table
 # eliminated the ncbiGene track
 my @trackList = qw(ncbiRefSeq xenoRefGene augustus ensGene gc5Base allGaps assembly rmsk simpleRepeat windowMasker cpgIslandExtUnmasked);
 
 ##############################################################################
 ### start the table output
 ##############################################################################
 sub startTable() {
 
 # coordinate the order of these column headings with the @trackList listed above
 
 print '<table class="sortable" border="1">
 <thead><tr><th>count</th>
   <th>common name<br>link&nbsp;to&nbsp;genome&nbsp;browser</th>
   <th class="sorttable_numeric">ncbiRefSeq</th>
 ';
 
 print "  <th class=\"sorttable_numeric\">ncbiGene</th>\n" if ($testOutput);
 
 print '  <th class="sorttable_numeric">xenoRefGene</th>
   <th class="sorttable_numeric">augustus<br>genes</th>
   <th class="sorttable_numeric">Ensembl<br>genes</th>
   <th class="sorttable_numeric">gc5 base</th>
 ';
 
 if ($testOutput) {
   print "  <th class=\"sorttable_numeric\">AGP<br>gap</th>\n";
   print "  <th class=\"sorttable_numeric\">all<br>gaps</th>\n";
 } else {
   print "  <th class=\"sorttable_numeric\">gaps</th>\n";
 }
 
 print '  <th class="sorttable_numeric">assembly<br>sequences</th>
   <th class="sorttable_numeric">Repeat<br>Masker</th>
   <th class="sorttable_numeric">TRF<br>simpleRepeat</th>
   <th class="sorttable_numeric">window<br>Masker</th>
 ';
 
 if ($testOutput) {
 print '  <th class="sorttable_numeric">gap<br>Overlap</th>
   <th class="sorttable_numeric">tandem<br>Dups</th>
   <th class="sorttable_numeric">cpg<br>unmasked</th>
   <th class="sorttable_numeric">cpg<br>island</th>
 ';
 
 } else {
   print "  <th class=\"sorttable_numeric\">cpg<br>islands</th>\n";
 }
 
 print "</tr></thead><tbody>\n";
 }	#	sub startTable()
 
 ##############################################################################
 ### end the table output
 ##############################################################################
 sub endTable() {
 
 my $commaNuc = commify($overallNucleotides);
 my $commaSeqCount = commify($overallSeqCount);
 my $commaGapSize = commify($overallGapSize);
 my $commaGapCount = commify($overallGapCount);
 
 my $percentDone = 100.0 * $asmCount / $assemblyTotal;
 my $doneMsg = "";
 if ($asmCount < $assemblyTotal) {
   $doneMsg = sprintf(" (%d build completed, %.2f %% finished)", $asmCount, $percentDone);
 }
 my $columnCount = scalar(@trackList);
 my $colSpanFill = $columnCount - 1;
 
 if ($assemblyTotal > 1) {
   print <<"END"
 
 </tbody>
 <tfoot><tr><th>TOTALS:</th><td align=center colspan=$colSpanFill>total assembly count&nbsp;${assemblyTotal}${doneMsg}</td>
   </tr></tfoot>
 </table>
 END
 } else {
   print <<"END"
 
 </tbody>
 </table>
 END
 }
 }	#	sub endTable()
 
 ##############################################################################
 ### end the HTML output
 ##############################################################################
 sub endHtml() {
 
-# do not print these links for VGP index
+&commonHtml::otherHubLinks($vgpIndex, $asmHubName);
+&commonHtml::htmlFooter($vgpIndex, $asmHubName);
 
-if ((0 == $vgpIndex) && ($asmHubName ne "viral")) {
-  printf "<p>\n<table border='1'><thead>\n<tr>";
-  printf "<th>Assembly hubs index pages:&nbsp;</th>\n";
-  printf "<th><a href='../primates/index.html'>Primates</a></th>\n";
-  printf "<th><a href='../mammals/index.html'>Mammals</a></th>\n";
-  printf "<th><a href='../birds/index.html'>Birds</a></th>\n";
-  printf "<th><a href='../fish/index.html'>Fish</a></th>\n";
-  printf "<th><a href='../vertebrate/index.html'>other vertebrates</a></th>\n";
-
-  printf "</tr><tr>\n";
-  printf "<th>Hubs assembly statistics:&nbsp;</th>\n";
-  printf "<th><a href='../primates/asmStats.html'>Primates</a></th>\n";
-  printf "<th><a href='../mammals/asmStats.html'>Mammals</a></th>\n";
-  printf "<th><a href='../birds/asmStats.html'>Birds</a></th>\n";
-  printf "<th><a href='../fish/asmStats.html'>Fish</a></th>\n";
-  printf "<th><a href='../vertebrate/asm.html'>other vertebrates</a></th>\n";
-
-  printf "</tr><tr>\n";
-  printf "<th>Hubs track statistics:&nbsp;</th>\n";
-  printf "<th><a href='../primates/trackData.html'>Primates</a></th>\n";
-  printf "<th><a href='../mammals/trackData.html'>Mammals</a></th>\n";
-  printf "<th><a href='../birds/trackData.html'>Birds</a></th>\n";
-  printf "<th><a href='../fish/trackData.html'>Fish</a></th>\n";
-  printf "<th><a href='../vertebrate/trackData.html'>other vertebrates</a></th>\n";
-
-  printf "</tr></thead>\n</table>\n</p>\n";
-}
-
-print <<"END"
-</div><!-- closing gbsPage from gbPageStartHardcoded.html -->
-</div><!-- closing container-fluid from gbPageStartHardcoded.html -->
-<!--#include virtual="\$ROOT/inc/gbFooterHardcoded.html"-->
-<script type="text/javascript" src="/js/sorttable.js"></script>
-</body></html>
-END
 }	#	sub endHtml()
 
 sub asmCounts($) {
   my ($chromSizes) = @_;
   my ($sequenceCount, $totalSize) = split('\s+', `ave -col=2 $chromSizes | egrep "^count|^total" | awk '{printf "%d\\n", \$NF}' | xargs echo`);
   return ($sequenceCount, $totalSize);
 }
 
 #    my ($gapSize, $maskPerCent, $sizeNoGaps) = maskStats($faSizeTxt);
 sub maskStats($) {
   my ($faSizeFile) = @_;
   my $sizeNoGaps = `grep 'sequences in 1 file' $faSizeFile | awk '{print \$4}'`;
   my $gapSize = `grep 'sequences in 1 file' $faSizeFile | awk '{print \$3}'`;
   chomp $gapSize;
   $gapSize =~ s/\(//;
   my $totalBases = `grep 'sequences in 1 file' $faSizeFile | awk '{print \$1}'`;
   chomp $totalBases;
   my $maskedBases = `grep 'sequences in 1 file' $faSizeFile | awk '{print \$9}'`;
   chomp $maskedBases;
   my $maskPerCent = 100.0 * $maskedBases / $totalBases;
   return ($gapSize, $maskPerCent, $sizeNoGaps);
 }
 
 # grep "sequences in 1 file" GCA_900324465.2_fAnaTes1.2.faSize.txt
 # 555641398 bases (3606496 N's 552034902 real 433510637 upper 118524265 lower) in 50 sequences in 1 files
 
 sub gapStats($$) {
   my ($buildDir, $asmId) = @_;
   my $gapBed = "$buildDir/trackData/allGaps/$asmId.allGaps.bed.gz";
   my $gapCount = 0;
   if ( -s "$gapBed" ) {
     $gapCount = `zcat $gapBed | awk '{print \$3-\$2}' | ave stdin | grep '^count' | awk '{print \$2}'`;
   }
   chomp $gapCount;
   return ($gapCount);
 }
 
 ##############################################################################
 ### tableContents()
 ##############################################################################
 sub tableContents() {
   my $asmCounted = 0;
   if ($testOutput) {  # add extra columns during 'test' output
 #                       0          1          2        3       4      5    6
 #  7       8      9            10           11        12          13
 #     14
 # my @trackList = qw(ncbiRefSeq xenoRefGene augustus ensGene gc5Base gap allGaps assembly rmsk simpleRepeat windowMasker gapOverlap tandemDups cpgIslandExtUnmasked cpgIslandExt);
 #                       0            1         2        3       4      5      6
 #      7      8           9               10
 # my @trackList = qw(ncbiRefSeq xenoRefGene augustus ensGene gc5Base allGaps assembly rmsk simpleRepeat windowMasker cpgIslandExtUnmasked);
     splice @trackList, 11, 0, "cpgIslandExt";
     splice @trackList, 10, 0, "tandemDups";
     splice @trackList, 10, 0, "gapOverlap";
     splice @trackList, 5, 0, "gap";
     splice @trackList, 1, 0, "ncbiGene";
   }
   foreach my $asmId (@orderList) {
     my $tracksCounted = 0;
     my ($gcPrefix, $asmAcc, $asmName) = split('_', $asmId, 3);
     my $accessionId = sprintf("%s_%s", $gcPrefix, $asmAcc);
     my $accessionDir = substr($asmId, 0 ,3);
     $accessionDir .= "/" . substr($asmId, 4 ,3);
     $accessionDir .= "/" . substr($asmId, 7 ,3);
     $accessionDir .= "/" . substr($asmId, 10 ,3);
     my $buildDir = "/hive/data/genomes/asmHubs/refseqBuild/$accessionDir/$asmId";
     if ($gcPrefix eq "GCA") {
      $buildDir = "/hive/data/genomes/asmHubs/genbankBuild/$accessionDir/$asmId";
     }
     my $trackDb="$buildDir/${asmId}.trackDb.txt";
     next if (! -s "$trackDb");	# assembly build not complete
     my $asmReport="$buildDir/download/${asmId}_assembly_report.txt";
     if (! -s "$asmReport") {
       printf STDERR "# no assembly report:\n# %s\n", $asmReport;
       next;
     }
     my $chromSizes = "${buildDir}/${asmId}.chrom.sizes";
     my $twoBit = "${buildDir}/trackData/addMask/${asmId}.masked.2bit";
     if (! -s "$twoBit") {
       printf STDERR "# no 2bit file:\n# %s\n", $twoBit;
       printf "<tr><td align=right>%d</td>\n", ++$asmCount;
       printf "<td align=center>%s</td>\n", $accessionId;
       printf "<th colspan=15 align=center>missing masked 2bit file</th>\n";
       printf "</tr>\n";
       next;
     }
     my $faSizeTxt = "${buildDir}/${asmId}.faSize.txt";
     if ( ! -s "$faSizeTxt" ) {
        printf STDERR "twoBitToFa $twoBit stdout | faSize stdin > $faSizeTxt\n";
        print `twoBitToFa $twoBit stdout | faSize stdin > $faSizeTxt`;
     }
     my ($gapSize, $maskPerCent, $sizeNoGaps) = maskStats($faSizeTxt);
     $overallGapSize += $gapSize;
     my ($seqCount, $totalSize) = asmCounts($chromSizes);
     $overallSeqCount += $seqCount;
 #    my $totalSize=`ave -col=2 $chromSizes | grep "^total" | awk '{printf "%d", \$NF}'`;
     $overallNucleotides += $totalSize;
     my $gapCount = gapStats($buildDir, $asmId);
     $overallGapCount += $gapCount;
     my $sciName = "notFound";
     my $commonName = "notFound";
     my $asmDate = "notFound";
     my $itemsFound = 0;
     open (FH, "<$asmReport") or die "can not read $asmReport";
     while (my $line = <FH>) {
       last if ($itemsFound > 5);
       chomp $line;
       $line =~ s/
//g;;
       $line =~ s/\s+$//g;;
       if ($line =~ m/Date:/) {
         if ($asmDate =~ m/notFound/) {
            ++$itemsFound;
            $asmDate = $line;
            $asmDate =~ s/.*:\s+//;
         }
       } elsif ($line =~ m/Organism name:/) {
         if ($sciName =~ m/notFound/) {
            ++$itemsFound;
            $commonName = $line;
            $sciName = $line;
            $commonName =~ s/.*\(//;
            $commonName =~ s/\)//;
            $commonName = $commonName{$asmId} if (exists($commonName{$asmId}));
            $sciName =~ s/.*:\s+//;
            $sciName =~ s/\s+\(.*//;
         }
       }
     }
     close (FH);
     my $hubUrl = "https://genome.ucsc.edu/h/$accessionId";
     if ($testOutput) {
       $hubUrl = "https://genome-test.gi.ucsc.edu/h/$accessionId";
     }
     printf "<tr><td align=right>%d</td>\n", ++$asmCount;
     printf "<td align=center><a href='%s' target=_blank>%s<br>%s</a></td>\n", $hubUrl, $commonName, $accessionId;
     foreach my $track (@trackList) {
       my $trackFile = "$buildDir/bbi/$asmId.$track";
       my $trackFb = "$buildDir/trackData/$track/fb.$asmId.$track.txt";
       my $runDir = "$buildDir/trackData/$track";
       my ($itemCount, $percentCover);
       if ( "$track" eq "gc5Base" ) {
          $trackFile .= ".bw";
       } else {
          $trackFile .= ".bb";
       }
       my $customKey = "";
       if ( "$track" eq "rmsk") {
         my $rmskStats = "$buildDir/trackData/repeatMasker/$asmId.rmsk.stats";
         if (! -s "${rmskStats}") {
          my $faOut = "$buildDir/trackData/repeatMasker/$asmId.sorted.fa.out.gz";
           if ( -s "$faOut") {
             my $items = `zcat "$faOut" | wc -l`;
             chomp $items;
             $itemCount = commify($items);
             my $masked = `grep masked "$buildDir/trackData/repeatMasker/faSize.rmsk.txt" | awk '{print \$4}' | sed -e 's/%//;'`;
             chomp $masked;
             $percentCover = sprintf("%.2f %%", $masked);
             open (RS, ">$rmskStats") or die "can now write to $rmskStats";
             printf RS "%s\t%s\n", $itemCount, $percentCover;
             close (RS);
           } else {
             $itemCount = "n/a";
             $percentCover = "n/a";
           }
         } else {
           ($itemCount, $percentCover) = split('\s+', `cat $rmskStats`);
           chomp $percentCover;
           $customKey = sprintf("%.2f", $percentCover);
           $percentCover = sprintf("%.2f %%", $percentCover);
         }
       } else {	# not the rmsk track
         ($itemCount, $percentCover) = oneTrackData($asmId, $track, $trackFile, $totalSize, $trackFb, $runDir);
         if (0 == $testOutput) {
           # if track ncbiRefSeq does not exist, try the ncbiGene track
           if ($trackDb eq "ncbiRefSeq" && $itemCount eq "n/a") {
            $runDir = "$buildDir/trackData/ncbiGene";
            $trackFile = "$buildDir/bbi/$asmId.$track.bb";
           ($itemCount, $percentCover) = oneTrackData($asmId, "ncbiGene", $trackFile, $totalSize, $trackFb, $runDir);
           }
         }
         if (($percentCover =~ m/%/) || ($percentCover !~ m#n/a#)) {
           $customKey = $percentCover;
           $customKey =~ s/[ %]+//;
         }
       }
       if (length($customKey)) {
       printf "    <td align=right sorttable_customkey='%s'>%s<br>(%s)</td>\n", $customKey, $itemCount, $percentCover;
       } else {
         if ($itemCount eq "n/a") {
       printf "    <td align=right>n/a</td>\n";
         } else {
       printf "    <td align=right>%s<br>(%s)</td>\n", $itemCount, $percentCover;
         }
       }
       $tracksCounted += 1 if ($itemCount ne "n/a");
     }	#	foreach my $track (@trackList)
     printf "</tr>\n";
     $asmCounted += 1;
     printf STDERR "# %03d\t%02d tracks\t%s\n", $asmCounted, $tracksCounted, $asmId;
   }
 }
 
 ##############################################################################
 ### main()
 ##############################################################################
 
-open (FH, "<$toolsDir/${commonNameOrder}") or die "can not read ${commonNameOrder}";
+open (FH, "<${orderList}") or die "can not read ${orderList}";
 while (my $line = <FH>) {
   next if ($line =~ m/^#/);
   chomp $line;
   my ($asmId, $commonName) = split('\t', $line);
   push @orderList, $asmId;
   $commonName{$asmId} = $commonName;
   ++$assemblyTotal;
 }
 close (FH);
 
 startHtml();
 startTable();
 tableContents();
 endTable();
 endHtml();