0c511f781581025b2963c78ef87f090435fa2a73
kate
  Mon Jan 4 16:13:07 2021 -0800
Create trashfile for custom bed url, as multi-region code doesn't currently handle custom bed file (maybe change this later). refs #26385

diff --git src/hg/lib/hui.c src/hg/lib/hui.c
index b96c22b..8927d88 100644
--- src/hg/lib/hui.c
+++ src/hg/lib/hui.c
@@ -43,31 +43,36 @@
 #include "memgfx.h"
 #include "trackHub.h"
 #include "gtexUi.h"
 #include "genbank.h"
 #include "htmlPage.h"
 #include "longRange.h"
 #include "barChartUi.h"
 #include "interactUi.h"
 #include "interact.h"
 #include "hicUi.h"
 #include "bigDbSnp.h"
 #include "customComposite.h"
 #include "trackVersion.h"
 #include "hubConnect.h"
 #include "bigBedFilter.h"
+
+// TODO: these should go away after refactoring of multi-region link
+#include "hex.h"
 #include "net.h"
+#include "trashDir.h"
+#include <openssl/sha.h>
 
 #define SMALLBUF 256
 #define MAX_SUBGROUP 9
 #define ADD_BUTTON_LABEL        "add"
 #define CLEAR_BUTTON_LABEL      "clear"
 #define JBUFSIZE 2048
 
 
 #define DEF_BUTTON "<IMG id=\"btn_%s\" src=\"../images/%s\" alt=\"%s\">\n"
 #define DEF_BUTTON_JS "setCheckBoxesThatContain('%s',true,false,'%s','','%s');" \
 	       "setCheckBoxesThatContain('%s',false,false,'%s','_defOff','%s');" 
 #define DEFAULT_BUTTON(nameOrId,anc,beg,contains) \
     printf(DEF_BUTTON,(anc),"defaults_sm.png","default"); \
     safef(id, sizeof id, "btn_%s", (anc)); \
     jsOnEventByIdF("click", id, DEF_BUTTON_JS,(nameOrId),(beg),(contains),(nameOrId),(beg),(contains)); 
@@ -404,92 +409,133 @@
 close(sd);
 if (!dsRegionBed)
     return FALSE;
 char *regionBedTxt = dyStringCannibalize(&dsRegionBed);
 
 // count fields in BED. Accept up to BED9 (user-spec colors)
 char *bedTxt = cloneString(regionBedTxt);
 struct lineFile *lf = lineFileOnString(NULL, TRUE, bedTxt);
 char *words[9];
 int bedSize = lineFileChopNext(lf, words, sizeof words);
 lineFileClose(&lf);
 freeMem(bedTxt);
 
 lf = lineFileOnString(NULL, TRUE, regionBedTxt);
 
-char *colorLight = "184,201,255";       // blue
-char *colorDark = "0,0,0";              // black
+// TODO: refactor with interact multi-region
+
+static char *colorLight = "184,201,255";       // blue
+static char *colorDark = "0,0,0";              // black
 char *color = colorLight;
 boolean doLightColor = TRUE;
 
 int id = 1;
 char name[100];
 char userColor[10];
 struct bed *region;
 
+struct tempName mrTn;
+trashDirFile(&mrTn, "hgt", "custRgn_track", ".bed");
+FILE *f = fopen(mrTn.forCgi, "w");
+if (f == NULL)
+    errAbort("can't create temp file %s", mrTn.forCgi);
+char regionInfo[1024];
+char *regionFile = cloneString(mrTn.forCgi);
+
+#ifdef LATER
+safef(regionInfo, sizeof regionInfo, "#padding %d\n", padding);
+mustWrite(f, regionInfo, strlen(regionInfo));
+
+safef(regionInfo, sizeof regionInfo, "#shortDesc %s\n", name);
+mustWrite(f, regionInfo, strlen(regionInfo));
+#endif
+
+// write to trash file and custom track
 while (lineFileChopNext(lf, words, bedSize))
     {
     region = bedLoadN(words, bedSize);
     if (bedSize < 9)
         {
         // assign alternating light/dark color
         color = doLightColor ? colorLight : colorDark;
         doLightColor = !doLightColor;
         }
     else
         {
         // no lib ? sigh
         int colorIx = (int)region->itemRgb;
         struct rgbColor rgb = colorIxToRgb(colorIx);
         safef(userColor, sizeof userColor, "%d,%d,%d", rgb.b, rgb.g, rgb.r);
 
         // or this ? -- uglier but deeper in code
         //safef(userColor, sizeof userColor, "%d,%d,%d", (region->itemRgb & 0xff0000) >> 16,
             //(region->itemRgb & 0xff00) >> 8, (region->itemRgb & 0xff));
         }
     if (bedSize < 4)
         {
         // region label based on chrom and an item number
         safef(name, sizeof name, "r%d/%s", id++, region->chrom);
         }
+    else
+        {
+        strcpy(name, region->name);
+        }
+    // write to trash file
+    safef(regionInfo, sizeof regionInfo, "%s\t%d\t%d\n",
+                    region->chrom, region->chromStart, region->chromEnd);
+    mustWrite(f, regionInfo, strlen(regionInfo));
+
+    // write to custom track
     dyStringPrintf(dsCustomText, "%s\t%d\t%d\t%s\t"
                         "0\t.\t%d\t%d\t%s\n",
                             region->chrom, region->chromStart, region->chromEnd,  name,
                             region->chromStart, region->chromEnd, color);
     }
 lineFileClose(&lf);
+fclose(f);
+
+// create SHA1 file; used to see if file has changed
+unsigned char hash[SHA_DIGEST_LENGTH];
+SHA1((const unsigned char *)regionInfo, strlen(regionInfo), hash);
+char newSha1[(SHA_DIGEST_LENGTH + 1) * 2];
+hexBinaryString(hash, SHA_DIGEST_LENGTH, newSha1, (SHA_DIGEST_LENGTH + 1) * 2);
+char sha1File[1024];
+safef(sha1File, sizeof sha1File, "%s.sha1", mrTn.forCgi);
+f = mustOpen(sha1File, "w");
+mustWrite(f, newSha1, strlen(newSha1));
+carefulClose(&f);
 
 char customHtml[1000];
 safef(customHtml, sizeof customHtml, "<h2>Description</h2>\n"
     "<p>This custom track displays regions of interest for the "
     "<a href='../cgi-bin/hgTrackUi?db=%s&g=%s'><em>%s</em> track</a>.</p>",
         db, tdb->track, tdb->shortLabel);
 
 // TODO: support #padding in custom regions file
 
-printf("<p><a target='_blank' "
-            "href='../cgi-bin/hgTracks?"
+printf("<p>View regions of interest for this track in "
+            "<a target='_blank' href='../cgi-bin/hgTracks?"
                 "virtMode=1&"
                 "virtModeType=customUrl&"
                 "virtWinFull=on&"
                 "virtShortDesc=%s&"
                 "multiRegionsBedUrl=%s&"
                 "%s=%s&"
                 "%s=%s'>"
-            "View regions of interest</a>"
-                " for this track in multi-region view</a> (custom regions mode)</p>",
-                    tdb->track, cgiEncode(regionUrl),
+            "multi-region view</a>"
+                " (custom regions mode)</p>",
+                    tdb->track, cgiEncode(regionFile),
                     CT_CUSTOM_DOC_TEXT_VAR, cgiEncode(customHtml),
                     CT_CUSTOM_TEXT_VAR, cgiEncode(dyStringCannibalize(&dsCustomText)));
 return TRUE;
 }
 
 void extraUiLinks(char *db,struct trackDb *tdb)
 // Show metadata, and downloads, schema links where appropriate
 {
 makeMultiRegionLink(db, tdb);
 
 struct slPair *pairs = trackDbMetaPairs(tdb);
 if (pairs != NULL)
     printf("<b>Metadata:</b><br>%s\n", pairsAsHtmlTable( pairs, tdb, FALSE, FALSE));
 else if (!tdbIsComposite(tdb) && !trackHubDatabase(db) && (metadataForTable(db, tdb, NULL) != NULL))
     printf("<b>Metadata:</b><br>%s\n", metadataAsHtmlTable(db, tdb, FALSE, FALSE));