8a5f4201a85c4d12a6efe6a7c6955a4e6e53de61
kent
  Fri Jan 15 08:47:15 2021 -0800
Making checking on label counts matching matrix dimensions more robust, and catching file close error.

diff --git src/utils/matrixRelabel/matrixRelabel.c src/utils/matrixRelabel/matrixRelabel.c
index 3f46ac8..47eecdb 100644
--- src/utils/matrixRelabel/matrixRelabel.c
+++ src/utils/matrixRelabel/matrixRelabel.c
@@ -98,61 +98,84 @@
 lineFileNeedNext(lf, &line, &lineSize);
 int colCount = 0;
 if (newColumns != NULL)
     {
     colCount = newColumnCount;
     fputs(newColumns[0], f);
     int i;
     for (i=1; i<colCount; ++i)
         fprintf(f, "\t%s", newColumns[i]);
     fputc('\n', f);
     }
 else
     {
     char *word = nextTabWord(&line);
     if (word != NULL)
+	{
 	fprintf(f, "%s", word);
+	colCount += 1;
+	}
     while ((word = nextTabWord(&line)) != NULL)
          {
 	 fprintf(f, "\t%s", word);
 	 colCount += 1;
 	 }
     fputc('\n', f);
     }
 
-int rowIx = 0;
+int rowIx = 1;
+boolean checkedColCount = FALSE;
 while (lineFileNext(lf, &line, NULL))
     {
     char *rowLabel = nextTabWord(&line);
     if (newRows != NULL)
         {
-	++rowIx;
 	if (rowIx >= newRowCount)
 	    errAbort("Not enough lines in %s for %s", newRowFile, input);
 	rowLabel = newRows[rowIx];
+	++rowIx;
 	}
     if (lookupHash != NULL)
         {
 	char *newLabel = hashFindVal(lookupHash, rowLabel);
 	if (newLabel == NULL)
 	    {
 	    if (doTrim)
 	       continue;
 	    else
 	       errAbort("Couldn't find %s from line %d of %s in %s", 
 			rowLabel, lf->lineIx, lf->fileName, lookupRowFile);
 	    }
 	rowLabel = newLabel;
 	}
+    if (!checkedColCount)
+        {
+	char *dupe = cloneString(line);
+	char *s = dupe;
+	int count = 1;  // Include rowLabel
+	while (nextTabWord(&s) != NULL)
+	    ++count;
+	freez(&dupe);
+	if (count != colCount)
+	    lineFileExpectWords(lf, colCount, count);
+	checkedColCount = TRUE;
+	}
     fprintf(f, "%s\t%s\n", rowLabel, line);
     }
+if (newRows != NULL)
+    {
+    if (rowIx != newRowCount)
+        errAbort("%s has %d rows and %s has %d, not all row labels written",
+	    input, rowIx, newRowFile, newRowCount);
+    }
+carefulClose(&f);
 }
 
 int main(int argc, char *argv[])
 /* Process command line. */
 {
 optionInit(&argc, argv, options);
 if (argc != 3)
     usage();
 matrixRelabel(argv[1], argv[2]);
 return 0;
 }