4d2d9e487f9381072433d04115271e0f00294570
jcasper
  Fri Nov 18 08:42:53 2022 -0800
Fix for RNAplot running out of filename buffer size, refs #30296

diff --git src/hg/hgGene/rnaStructure.c src/hg/hgGene/rnaStructure.c
index 289f1d9..105cf61 100644
--- src/hg/hgGene/rnaStructure.c
+++ src/hg/hgGene/rnaStructure.c
@@ -1,21 +1,22 @@
 /* rnaStructure - do section on 3' and 5' UTR structure. */
 
 /* 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 <limits.h>
 #include "hash.h"
 #include "linefile.h"
 #include "jksql.h"
 #include "rnaFold.h"
 #include "hui.h"
 #include "web.h"
 #include "portable.h"
 #include "hgGene.h"
 #include "hgConfig.h"
 #include "pipeline.h"
 
 
 static void rnaTrashDirsInit(char **tables, int count)
 /*	create trash directories if necessary */
 {
@@ -61,65 +62,75 @@
 
 char *rnaPlotPath = cfgOptionDefault("rnaPlotPath", "../cgi-bin/RNAplot");
 
 for (side = 0; side < ArraySize(names); ++side)
     {
     char *table = tables[side];
     struct sqlResult *sr;
     char query[256], **row;
     sqlSafef(query, sizeof(query), "select * from %s where name = '%s'",
     	table, geneId);
     sr = sqlGetResult(conn, query);
     if ((row = sqlNextRow(sr)) != NULL)
 	{
 	struct rnaFold fold;
 	int bases;
-	char psName[128];
+	char psName[2048], altPsName[2048];
 
 	/* Load fold and save it as postScript. */
 	rnaFoldStaticLoad(row, &fold);
-	safef(psName, sizeof(psName), "../trash/%s/%s_%s.ps", table, table, geneId);
+	safef(psName, sizeof(psName), "%s/%s/%s_%s.ps", trashDir(), table, table, geneId);
+        // newer versions of RNAplot add _ss.ps to the file name
+        safef(altPsName, sizeof(altPsName), "%s/%s/%s_%s.ps_ss.ps", trashDir(), table, table, geneId);
         bool plotDone = FALSE;
-	if (fileExists(psName))
+	if (fileExists(psName) || fileExists(altPsName))
             plotDone = TRUE;
         else
 	    {
 	    FILE *f;
 
             if (!fileExists(rnaPlotPath))
                 {
                 plotDone = FALSE;
-                fprintf(stderr, "Could not find %s", rnaPlotPath);
                 }
             else
                 {
-                char *plotCmd[] = {rnaPlotPath, NULL};
+                char *absoluteRnaPlotPath = NULL;
+                absoluteRnaPlotPath = realpath(rnaPlotPath, NULL);
+                char *plotCmd[] = {absoluteRnaPlotPath, NULL};
+                char *curdir, outputDir[2048], psNameLocal[2048];
+                curdir = getCurrentDir();
+                safef(outputDir, sizeof(outputDir), "%s/%s/", trashDir(), table);
+                setCurrentDir(outputDir); /* RNAplot only writes output to the current directory */
                 struct pipeline *plStruct = pipelineOpen1(plotCmd, pipelineWrite | pipelineNoAbort, "/dev/null", NULL, 0);
+                safef(psNameLocal, sizeof(psNameLocal), "%s_%s.ps", table, geneId);
                 f = pipelineFile(plStruct);
                 if (f != NULL)
                     {
-                    fprintf(f, ">%s\n", psName);	/* This tells where to put file. */
+                    fprintf(f, ">%s\n", psNameLocal);	/* This is used by RNAplot for the filename. */
                     fprintf(f, "%s\n%s\n", fold.seq, fold.fold);
                     plotDone = TRUE;
                     }
                 pipelineClose(&plStruct);
+                setCurrentDir(curdir); // return home
+                free(absoluteRnaPlotPath);
                 }
             }
 
         // newer versions of RNAplot add _ss.ps to the file name
         if (!fileExists(psName))
-            safef(psName, sizeof(psName), "../trash/%s/%s_%s.ps_ss.ps", table, table, geneId);
+            safecpy(psName, sizeof(psName), altPsName);
             
 	/* Print row of table, starting with energy terms . */
 	hPrintf("</TR><TR>");
 	bases = strlen(fold.seq);
 	webPrintLinkCell(names[side]);
 	webPrintLinkCellStart();
 	hPrintf("%1.2f", fold.energy);
 	webPrintLinkCellEnd();
 	webPrintLinkCellStart();
 	hPrintf("%d", bases);
 	webPrintLinkCellEnd();
 	webPrintLinkCellStart();
 	hPrintf("%1.3f", fold.energy/bases);
 	webPrintLinkCellEnd();