04a5652eeca73cbc1c426587576cf8d3b29a1f4c
chmalee
  Mon Sep 18 15:57:17 2023 -0700
Fix hgTracks and hgc crash when a users session has pcr results from both before we allowed multiple queries to stack and after, refs #32236

diff --git src/hg/cgilib/pcrResult.c src/hg/cgilib/pcrResult.c
index 5d51810..ff9961f 100644
--- src/hg/cgilib/pcrResult.c
+++ src/hg/cgilib/pcrResult.c
@@ -1,26 +1,27 @@
 /* pcrResult -- support for internal track of hgPcr results. */
 
 /* Copyright (C) 2014 The Regents of the University of California 
  * See kent/LICENSE or http://genome.ucsc.edu/license/ for licensing information. */
 
 #include "common.h"
 #include "hdb.h"
 #include "hui.h"
 #include "obscure.h"
 #include "targetDb.h"
 #include "pcrResult.h"
+#include "trackHub.h"
 
 
 char *pcrResultCartVar(char *db)
 /* Returns the cart variable name for PCR result track info for db. 
  * Don't free the result! */
 {
 static char buf[1024];
 safef(buf, sizeof(buf), "%s_%s", PCR_RESULT_TRACK_NAME, db);
 return buf;
 }
 
 #define setPtIfNotNull(pt, val) if (pt != NULL) *pt = val
 
 boolean pcrResultParseCart(char *db, struct cart *cart, char **retPslFile,
 			   char **retPrimerFile,
@@ -38,31 +39,31 @@
     setPtIfNotNull(retPrimerFile, NULL);
     setPtIfNotNull(retTarget, NULL);
     return FALSE;
     }
 static char buf[2048];
 char *words[3];
 int wordCount;
 safecpy(buf, sizeof(buf), hgPcrResult);
 wordCount = chopLine(buf, words);
 if (wordCount < 2)
     errAbort("Badly formatted hgPcrResult variable: %s", hgPcrResult);
 char *pslFile = words[0];
 char *primerFile = words[1];
 char *targetName = (wordCount > 2) ? words[2] : NULL;
 struct targetDb *target = NULL;
-if (isNotEmpty(targetName))
+if (!trackHubDatabase(db))
     target = targetDbLookup(db, targetName);
 
 if (!fileExists(pslFile) || !fileExists(primerFile) ||
     (wordCount > 2 && target == NULL))
     {
     cartRemove(cart, cartVar);
     setPtIfNotNull(retPslFile, NULL);
     setPtIfNotNull(retPrimerFile, NULL);
     setPtIfNotNull(retTarget, NULL);
     return FALSE;
     }
 setPtIfNotNull(retPslFile, cloneString(pslFile));
 setPtIfNotNull(retPrimerFile, cloneString(primerFile));
 setPtIfNotNull(retTarget, target);
 if (retTarget == NULL)
@@ -129,54 +130,58 @@
 		     struct psl **retItemPsl, struct psl **retOtherPsls, char *fPrimer, char *rPrimer)
 /* Read in psl from file.  If a psl matches the given item position, set 
  * retItemPsl to that; otherwise add it to retOtherPsls.  Die if no psl
  * matches the given item position. */
 {
 struct lineFile *lf = lineFileOpen(fileName, TRUE);
 struct psl *itemPsl = NULL, *otherPsls = NULL;
 char *pslFields[21];
 boolean targetSearchResult = stringIn("__", item) != NULL;
 while (lineFileRow(lf, pslFields))
     {
     struct psl *psl = pslLoad(pslFields);
     boolean gotIt = FALSE;
     // if "_" is in the item name, we look up the result(s) by the primer pair, else
     // we just show all psls
-    if (stringIn("_", item))
+    if (stringIn("_", item) && !sameString(psl->qName, "n/a"))
         {
         char *pair = cloneString(psl->qName);
         char *under = strchr(pair, '_');
         *under = '\0';
         char *thisFPrimer = pair;
         char *thisRPrimer = under+1;
         if (!differentWord(thisFPrimer, fPrimer) && !differentWord(thisRPrimer, rPrimer))
             {
             gotIt = checkPcrResultCoordinates(targetSearchResult, psl->tName, psl->tStart,
                     psl->tEnd, item, itemStart, itemEnd, chrom);
+            if (gotIt)
+                itemPsl = psl;
+            else
+                slAddHead(&otherPsls, psl);
             }
         }
     else
         {
         gotIt = checkPcrResultCoordinates(targetSearchResult, psl->tName, psl->tStart,
                 psl->tEnd, item, itemStart, itemEnd, chrom);
-        }
-
         if (gotIt)
             itemPsl = psl;
-    else
+        else if (!(fPrimer && rPrimer))
             slAddHead(&otherPsls, psl);
         }
+
+    }
 lineFileClose(&lf);
 if (itemPsl == NULL)
     {
     if (target != NULL)
         errAbort("Did not find record for amplicon in %s sequence %s",
              target->description, item);
     else
         errAbort("Did not find record for amplicon at %s:%d-%d",
              chrom, itemStart, itemEnd);
     }
 if (retItemPsl != NULL)
     *retItemPsl = itemPsl;
 else
     pslFree(&itemPsl);
 if (retOtherPsls != NULL)