029ebed9dfb6d343c7842137c1e5e212fcd81e55
kent
  Thu Aug 28 14:04:37 2014 -0700
Making tmp dir a command line option.  Adding id to merged wigs, which will keep temp files from clashing better (oops).
diff --git src/utils/bigWigCluster/bigWigCluster.c src/utils/bigWigCluster/bigWigCluster.c
index 5882560..0fea77b 100644
--- src/utils/bigWigCluster/bigWigCluster.c
+++ src/utils/bigWigCluster/bigWigCluster.c
@@ -1,89 +1,93 @@
 /* bigWigCluster - Cluster bigWigs using a hactree. */
 #include "sqlNum.h"
 #include "common.h"
 #include "linefile.h"
 #include "hash.h"
 #include "options.h"
 #include "hacTree.h"
 #include "rainbow.h"
 #include "portable.h"
 
 int gThreadCount = 10;
+char *gTmpDir = ".";
 
 void usage()
 /* Explain usage and exit. */
 {
 errAbort(
   "bigWigCluster - Cluster bigWigs using a hacTree\n"
   "usage:\n"
   "   bigWigCluster input.list chrom.sizes output.json output.tab\n"
   "where: input.list is a list of bigWig file names\n"
   "       chrom.sizes is tab separated <chrom><size> for assembly for bigWigs\n"
   "       output.json is json formatted output suitable for graphing with D3\n"
   "       output.tab is tab-separated file of  of items ordered by tree with the fields\n"
   "           label - label from -labels option or from file name with no dir or extention\n"
   "           pos - number from 0-1 representing position according to tree and distance\n"
   "           red - number from 0-255 representing recommended red component of color\n"
   "           green - number from 0-255 representing recommended green component of color\n"
   "           blue - number from 0-255 representing recommended blue component of color\n"
   "           path - file name from input.list including directory and extension\n" 
   "options:\n"
   "   -labels=fileName - label files from tabSeparated file with fields\n"
   "           path - path to bigWig file\n"
   "           label - a string with no tabs\n"
   "   -precalc=precalc.tab - tab separated file with <file1> <file2> <distance>\n"
   "            columns.\n"
   "   -threads=N - number of threads to use, default %d\n"
-  "Note: creates temp files in current dir but cleans them up on normal program exit\n"
+  "   -tmpDir=/tmp/path - place to put temp files, default current dir\n"
   , gThreadCount
   );
 }
 
 char tmpPrefix[PATH_LEN] = "bwc_tmp_";  /* Prefix for temp file names */
 
 /* Command line validation table. */
 static struct optionSpec options[] = {
    {"precalc", OPTION_STRING},
    {"threads", OPTION_INT},
    {"labels", OPTION_STRING},
+   {"tmpDir", OPTION_STRING},
    {NULL, 0},
 };
 
+int bigWigNextId;
+
 struct bigWig
 /* Information on a bigWig */
     {
     struct bigWig *next;  //next item in series
     int id;	// Unique numerical identifier
     char *fileName; // Associated file name
     char *label;    // Associated labels if any
     struct rgbColor color; //for coloring
     double pos;	    // Position between 0.0 and 1.0 when ordered by tree and distance
     };
 
 struct bigWig *readBigWigList(char* input)
 // get the bigWig files 
 {
 struct bigWig *list = NULL;
 struct lineFile *lf = lineFileOpen(input,TRUE);
 char *line = NULL;
 while(lineFileNextReal(lf, &line))
     {
     /* Allocate new item and initialize id and fileName fields */
     struct bigWig *bw;
     AllocVar(bw);
-    bw->id = lf->lineIx;
+    bw->id = ++bigWigNextId;
     bw->fileName = cloneString(trimSpaces(line));
 
     // Make up default label - same as file name without directory and extension
     char root[FILENAME_LEN];
     splitPath(line, NULL, root, NULL);
     bw->label = cloneString(root);
 
     /* Add to list */
     slAddHead(&list,bw);
     }
 
 /* Clean up and go home */
 lineFileClose(&lf);
 slReverse(&list);
 return list;
@@ -217,30 +221,31 @@
 struct bigWig *result;
 AllocVar(result);
 const struct bigWig *kid1 = (const struct bigWig *)item1;
 const struct bigWig *kid2 = (const struct bigWig *)item2;
 char tmpName[PATH_LEN];
 safef(tmpName, sizeof(tmpName), "%s%d_%d.bedGraph", tmpPrefix, kid1->id, kid2->id);
 char cmd1[1024];
 char cmd2[1024];
 safef(cmd1, 1024, "bigWigMerge %s %s %s -verbose=0", kid1->fileName, kid2->fileName, tmpName);
 char fileName[PATH_LEN];
 safef(fileName, sizeof(fileName), "%s%d_%d.bw", tmpPrefix, kid1->id, kid2->id);
 safef(cmd2, 1024, "bedGraphToBigWig %s %s %s", tmpName, chromSizesFile, fileName);
 mustSystem(cmd1);
 mustSystem(cmd2);
 remove(tmpName);
+result->id = ++bigWigNextId;
 result->fileName = cloneString(fileName);
 result->label = " ";
 return (struct slList *)result;
 }
 
 double findCachedDistance(struct hash *distanceHash, struct bigWig *bw1, struct bigWig *bw2)
 /* Return cached distance - from hash if possible */
 {
 double distance;
 double *cached = hacTreeDistanceHashLookup(distanceHash, bw1, bw2);
 if (cached != NULL)
    distance = *cached;
 else
    {
    distance = slBigWigDistance((struct slList *)bw1, (struct slList *)bw2, NULL);
@@ -382,18 +387,20 @@
 printOrderedTab(outputTab, orderedList);
 
 // Clean up remaining temp files 
 char cmd[1024];
 safef(cmd, sizeof(cmd), "rm %s*", tmpPrefix);
 mustSystem(cmd);
 }
 
 int main(int argc, char *argv[])
 /* Process command line. */
 {
 optionInit(&argc, argv, options);
 if (argc != 5)
     usage();
 gThreadCount = optionInt("threads", gThreadCount);
+gTmpDir = optionVal("tmpDir", gTmpDir);
+safef(tmpPrefix, sizeof(tmpPrefix), "%s/bwc_tmp_", gTmpDir);
 bigWigCluster(argv[1], argv[2], argv[3], argv[4]);
 return 0;
 }