src/hg/instinct/bioInt2/bioIntUI.c 1.10

1.10 2009/03/31 23:36:00 jsanborn
better search results
Index: src/hg/instinct/bioInt2/bioIntUI.c
===================================================================
RCS file: /projects/compbio/cvsroot/kent/src/hg/instinct/bioInt2/bioIntUI.c,v
retrieving revision 1.9
retrieving revision 1.10
diff -b -B -U 4 -r1.9 -r1.10
--- src/hg/instinct/bioInt2/bioIntUI.c	31 Mar 2009 22:32:34 -0000	1.9
+++ src/hg/instinct/bioInt2/bioIntUI.c	31 Mar 2009 23:36:00 -0000	1.10
@@ -218,28 +218,51 @@
 
 hFreeConn(&conn);
 }
 
-struct slPair *searchForFeatures(struct sqlConnection *conn, int cohort_id, 
+struct searchResults {
+    struct searchResults *next;
+    char *name;
+    char *source;
+    int length;
+};
+
+int searchResultsCmp(const void *va, const void *vb)
+/* Compare function to sort array of ints. */
+{
+const struct searchResults *a = *((struct searchResults **)va);
+const struct searchResults *b = *((struct searchResults **)vb);
+int diff = a->length - b->length;
+if (diff < 0)
+    return -1;
+else if (diff > 0)
+    return 1;
+else
+    return 0;
+}   
+
+struct searchResults *searchForFeatures(struct sqlConnection *conn, int cohort_id, 
 				 struct datasets *daList, char *feature_name)
 {
 int maxResponse = 5;
 /* Check analysis features */
 char query[256];
 safef(query, sizeof(query), 
-      "select feature_name from %s where feature_name like \"%%%s%%\" ", 
+      "select feature_name from %s where feature_name like \"%%%s%%\" "
+      "order by length(feature_name);", 
       AF_TABLE, feature_name);
 
 int count = 0;
-struct slPair *sp, *spList = NULL;
+struct searchResults *sp, *spList = NULL;
 struct sqlResult *sr = sqlGetResult(conn, query);
 char **row = NULL;
 while ((row = sqlNextRow(sr)) != NULL)
     { 
     char *name = row[0];
     AllocVar(sp);
     sp->name = cloneString(name);
-    sp->val = cloneString("gene/geneset");
+    sp->source = cloneString("gene/geneset");
+    sp->length = strlen(sp->name);
     slAddHead(&spList, sp);
     if (count > maxResponse)
 	break;
     count++;
@@ -263,9 +286,9 @@
     dyStringPrintf(dy, "%d", da->id);
     if (da->next)
 	dyStringPrintf(dy, ",");
     }
-dyStringPrintf(dy, ")");     
+dyStringPrintf(dy, ") order by length(%s.name)", FE_TABLE);     
 char *cquery = dyStringCannibalize(&dy);
 
 count = 0;
 sr = sqlGetResult(conn, cquery);
@@ -273,31 +296,31 @@
     { 
     char *name = row[0];
     AllocVar(sp);
     sp->name = cloneString(name);
-    sp->val = cloneString("clinical");
+    sp->source = cloneString("clinical");
+    sp->length = strlen(sp->name);
     slAddHead(&spList, sp);
     if (count < maxResponse)
 	break;
     count++;
     }
 sqlFreeResult(&sr);
+
+slSort(&spList, searchResultsCmp);
 return spList;
 }
 
 void sendNoMatch(struct json *js)
 {
 return;
 }
 
-void sendAmbiguities(struct json *js, struct slPair *spList)
+void sendAmbiguities(struct json *js, struct searchResults *spList)
 {
-struct slPair *sp;
+struct searchResults *sp;
 for (sp = spList; sp; sp = sp->next)
-    {
-    char *source = sp->val;
-    jsonAddString(js, sp->name, source); 
-    }
+    jsonAddString(js, sp->name, sp->source); 
 }
 
 void sendAnalysisFeatureData(struct sqlConnection *conn, struct json *js, 
 			     int cohort_id, char *feature_name)
@@ -445,14 +468,19 @@
 if (!daList)
     errAbort("No datasets matching cohort_id = %d", cohort_id);
 
 struct json *js = newJson();
-struct slPair *spList = searchForFeatures(conn, cohort_id, daList, feature_name);
-int numMatched = slCount(spList);
-if (numMatched == 0)
+if (sameString(feature_name, ""))  // blank was sent, return no match
     sendNoMatch(js);
 else
+    {    
+    struct searchResults *spList = searchForFeatures(conn, cohort_id, daList, feature_name);
+    int numMatched = slCount(spList);
+    if (numMatched == 0)
+	sendNoMatch(js);
+    else
     sendAmbiguities(js, spList);
+    }
 
 if (js)
     hPrintf("%s\n", js->print(js));
 
@@ -516,21 +544,15 @@
 int cohort_id = cartUsualInt(cart, bioIntCohortId, -1);
 if (cohort_id == -1)
     cohort_id = 1;  // hard code for first analysis during testing!
 
-int feature_id = cartUsualInt(cart, bioIntFeatureId, -1);
 char *feature_name = cartOptionalString(cart, bioIntFeatureName);
-if (!feature_name && feature_id == -1)
+if (!feature_name)
     errAbort("%s or %s must be set for mode=getClinicalData\n", bioIntFeature, bioIntFeatureId);
 
 struct sqlConnection *conn = hAllocConnProfile(localDb, db);
 
-struct features *fe = NULL;
-if (feature_id == -1)
-    fe = getFeaturesByName(conn, feature_name);
-else
-    fe = getFeaturesById(conn, feature_id);
-
+struct features *fe = getFeaturesByName(conn, feature_name);
 if (!fe)
     {
     hFreeConn(&conn);
     errAbort("Could not find clinical feature in db");