src/hg/utils/bedExtendRanges/bedExtendRanges.c 1.3

1.3 2009/04/06 18:13:51 larrym
allow user to use remote db (e.g. mysql.cse.ucsc.edu)
Index: src/hg/utils/bedExtendRanges/bedExtendRanges.c
===================================================================
RCS file: /projects/compbio/cvsroot/kent/src/hg/utils/bedExtendRanges/bedExtendRanges.c,v
retrieving revision 1.2
retrieving revision 1.3
diff -b -B -U 4 -r1.2 -r1.3
--- src/hg/utils/bedExtendRanges/bedExtendRanges.c	7 Oct 2008 00:04:41 -0000	1.2
+++ src/hg/utils/bedExtendRanges/bedExtendRanges.c	6 Apr 2009 18:13:51 -0000	1.3
@@ -13,11 +13,17 @@
 static char const rcsid[] = "$Id$";
 
 static boolean strictTab = FALSE;	/* Separate on tabs. */
 static struct hash *chromHash = NULL;
+char *host = NULL;
+char *user = NULL;
+char *password = NULL;
 
 static struct optionSpec optionSpecs[] = {
     {"tab", OPTION_BOOLEAN},
+    {"host", OPTION_STRING},
+    {"user", OPTION_STRING},
+    {"password", OPTION_STRING},
     {NULL, 0}
 };
 
 /* Command line switches. */
@@ -29,12 +35,16 @@
   "taking strand directionality into account.\n\n"
   "usage:\n"
   "   bedExtendRanges database length files(s)\n\n"
   "options:\n"
+  "   -host\tmysql host\n"
+  "   -user\tmysql user\n"
+  "   -password\tmysql password\n"
   "   -tab\t\tSeparate by tabs rather than space\n"
   "   -verbose=N - verbose level for extra information to STDERR\n\n"
   "example:\n\n"
   "   bedExtendRanges hg18 250 stdin\n\n"
+  "   bedExtendRanges -user=genome -host=mysql.cse.ucsc.edu hg18 250 stdin\n\n"
   "will transform:\n"
   "    chr1 500 525 . 100 +\n" 
   "    chr1 1000 1025 . 100 -\n" 
   "to:\n"
@@ -50,10 +60,19 @@
 struct sqlConnection *conn = NULL;
 struct sqlResult *sr = NULL;
 struct hash *ret;
 char **row;
+boolean localDb = TRUE;
 
-conn = hAllocConn(database);
+if(host)
+    {
+    conn = sqlConnectRemote(host, user, password, database);
+    localDb = FALSE;
+    }
+else
+    {
+    conn = hAllocConn(database);
+    }
 ret = newHash(0);
 
 sr = sqlGetResult(conn, "select * from chromInfo");
 while ((row = sqlNextRow(sr)) != NULL)
@@ -62,9 +81,10 @@
     verbose(4, "Add hash %s value %u (%#lx)\n", el->chrom, el->size, (unsigned long)&el->size);
     hashAdd(ret, el->chrom, (void *)(& el->size));
     }
 sqlFreeResult(&sr);
-hFreeConn(&conn);
+if(localDb)
+    hFreeConn(&conn);
 return ret;
 }
 
 static unsigned chromosomeSize(char *chromosome)
@@ -176,8 +196,11 @@
 optionInit(&argc, argv, optionSpecs);
 if (argc < 3)
     usage();
 strictTab = optionExists("tab");
+host = optionVal("host", NULL);
+user = optionVal("user", NULL);
+password = optionVal("password", NULL);
 
 bedExtendRanges(argv[1], atoi(argv[2]), argc-3, argv+3);
 return 0;
 }