Skip to content

Commit

Permalink
Merge pull request #49 from RBVI/feature/switch-resolving
Browse files Browse the repository at this point in the history
Merge the new feature that uses jensenlab for resolving instead of STITCH and Viruses
  • Loading branch information
scaramonche authored May 2, 2024
2 parents 5d4eed5 + 9a82e04 commit 23cfcdc
Show file tree
Hide file tree
Showing 8 changed files with 100 additions and 36 deletions.
Original file line number Diff line number Diff line change
@@ -1,14 +1,15 @@
package edu.ucsf.rbvi.stringApp.internal.model;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.json.simple.JSONArray;
import org.json.simple.JSONObject;

import edu.ucsf.rbvi.stringApp.internal.model.Species;
import edu.ucsf.rbvi.stringApp.internal.utils.ModelUtils;

public class Annotation {
Expand Down Expand Up @@ -77,9 +78,9 @@ public String toString() {
public boolean isResolved() {return resolved;}
public void setResolved(boolean resolved) {this.resolved = resolved;}

public static Map<String, List<Annotation>> getAnnotations(JSONObject json, String queryTerms, Species species) {
public static Map<String, List<Annotation>> getAnnotations(JSONObject json, String queryTerms, Species species, String useDATABASE) {
Map<String, List<Annotation>> map = new HashMap<>();
return getAnnotations(json, queryTerms, map, species);
return getAnnotations(json, queryTerms, map, species, useDATABASE);
}

public static boolean allResolved(List<Annotation> annotations) {
Expand All @@ -91,9 +92,11 @@ public static boolean allResolved(List<Annotation> annotations) {
}

public static Map<String, List<Annotation>> getAnnotations(JSONObject json, String queryTerms,
Map<String, List<Annotation>> map, Species species) {
Map<String, List<Annotation>> map, Species species, String useDATABASE) {
String[] terms = queryTerms.trim().split("\n");
JSONArray annotationArray = ModelUtils.getResultsFromJSON(json, JSONArray.class);
if (useDATABASE.equals(Databases.JENSENLAB.getAPIName()))
annotationArray = (JSONArray) annotationArray.get(0);
Integer version = ModelUtils.getVersionFromJSON(json);

// If we switch the API back to use a start of 0, this will need to change
Expand All @@ -110,13 +113,16 @@ public static Map<String, List<Annotation>> getAnnotations(JSONObject json, Stri
String taxonName = null;
int taxId = -1;
int queryIndex = -1;
String query = null;
String uniprot = null;
String sequence = null;
String image = null;
String color = null;

// Fields coming from string-db
if (ann.containsKey("preferredName"))
preferredName = (String)ann.get("preferredName");
// annotation contains the description of the entity as it comes from STRING-DB
if (ann.containsKey("annotation"))
annotation = (String)ann.get("annotation");
if (ann.containsKey("stringId"))
Expand All @@ -143,6 +149,7 @@ public static Map<String, List<Annotation>> getAnnotations(JSONObject json, Stri
species.setOfficialName(taxonName);
}
}
// Fields coming from string-db with csdata=1
if (ann.containsKey("uniprot"))
uniprot = (String)ann.get("uniprot");
if (ann.containsKey("sequence"))
Expand All @@ -152,6 +159,30 @@ public static Map<String, List<Annotation>> getAnnotations(JSONObject json, Stri
if (ann.containsKey("image"))
image = (String)ann.get("image");

// Fields coming from jensenlab
if(ann.containsKey("primary")) {
preferredName = (String)ann.get("primary");
if (description != null && !description.equals(""))
annotation = description;
}
// TODO: type is actually the taxId, but do we need it?
//if (ann.containsKey("type"))
// taxId = (Long)ann.get("type");
if(ann.containsKey("id")) {
if (ann.containsKey("type") && (Long)ann.get("type") != -1)
stringId = (Long)ann.get("type") + "." +(String)ann.get("id");
else
stringId = (String)ann.get("id");
}
// TODO: [N] Currently, we still get both CIDm and CIDs
// We only have networks for the CIDm, so we ignore the others for now
if (stringId.startsWith("CIDs"))
continue;
if(ann.containsKey("query")) {
query = (String)ann.get("query");
queryIndex = Arrays.asList(terms).indexOf(query);
}

// Temporary HACK
// if (stringId.startsWith("-1.CID1"))
// stringId = stringId.replaceFirst("-1.CID1","CIDm");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,8 @@ public enum Databases {
// AGOTOOL("aGOtool", "agotool")
VIRUSES("Viruses", "viruses"),
STITCH("Stitch", "stitch"),
STRINGDB("String-db", "string-db");
STRINGDB("String-db", "string-db"),
JENSENLAB("Jensenlab", "jensenlab");

String dbName;
String apiName;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -391,48 +391,72 @@ private Map<String, List<Annotation>> getAnnotationBatch(Species species, final
try {
results = HttpUtils.postJSON(url, args, manager);
// System.out.println(results.toString());
if (results != null) {
// System.out.println("Got results");
annotations = Annotation.getAnnotations(results, encTerms, annotations, species, Databases.STRING.getAPIName());
// System.out.println("Get annotations returns "+annotations.size());
}
} catch (ConnectionException ex) {
if (ex instanceof UnknownSpeciesException && manager.isVirusesEnabled() && annotations.size() == 0 && includeViruses) {
// also call the viruses API
// http://viruses.string-db.org/cgi/webservice_handler.pl?species=11320&identifiers=NS1_I34A1
// &caller_identity=string_app_v1_1_1&output=json&request=resolveList
url = manager.getResolveURL(Databases.VIRUSES.getAPIName());
//url = manager.getResolveURL(Databases.VIRUSES.getAPIName());
//args = new HashMap<>();
//args.put("species", speciesId);
//args.put("identifiers", encTerms);
//args.put("caller_identity", StringManager.CallerIdentity);
//args.put("output", "json");
//args.put("request", "resolveList");
//manager.info("URL:" + url + "?species=" + speciesId + "&caller_identity="
// + StringManager.CallerIdentity + "&identifiers=" + HttpUtils.truncate(encTerms));

url = manager.getEntityQueryURL();
args = new HashMap<>();
args.put("species", speciesId);
args.put("identifiers", encTerms);
args.put("caller_identity", StringManager.CallerIdentity);
args.put("output", "json");
args.put("request", "resolveList");
manager.info("URL:" + url + "?species=" + speciesId + "&caller_identity="
+ StringManager.CallerIdentity + "&identifiers=" + HttpUtils.truncate(encTerms));
args.put("limit", "5");
args.put("types", speciesId);
args.put("format", "json");
args.put("query", encTerms);
manager.info("URL: "+url+"?types=" + speciesId + "&limit=5&format=json"+"&identifiers="+HttpUtils.truncate(encTerms));

// Get the results
// System.out.println("Getting VIRUSES term resolution");
results = HttpUtils.postJSON(url, args, manager);
if (results != null) {
// System.out.println("Got results");
annotations = Annotation.getAnnotations(results, encTerms, annotations, species, Databases.JENSENLAB.getAPIName());
// System.out.println("Get annotations returns "+annotations.size());
}
}
}

if (results != null) {
// System.out.println("Got results");
annotations = Annotation.getAnnotations(results, encTerms, annotations, species);
// System.out.println("Get annotations returns "+annotations.size());
}
results = null;

// then, call other APIs to get resolve them
// resolve compounds
if (useDATABASE.equals(Databases.STITCH.getAPIName())) {
url = manager.getResolveURL(Databases.STITCH.getAPIName())+"json/resolveList";
//url = manager.getResolveURL(Databases.STITCH.getAPIName())+"json/resolveList";
//args = new HashMap<>();
//args.put("species", "CIDm");
//args.put("identifiers", encTerms);
//args.put("caller_identity", StringManager.CallerIdentity);
// manager.info("URL: "+url+"?species="+speciesId+"&caller_identity="+StringManager.CallerIdentity+"&identifiers="+HttpUtils.truncate(encTerms));

url = manager.getEntityQueryURL();
args = new HashMap<>();
args.put("species", "CIDm");
args.put("identifiers", encTerms);
args.put("caller_identity", StringManager.CallerIdentity);
manager.info("URL: "+url+"?species="+speciesId+"&caller_identity="+StringManager.CallerIdentity+"&identifiers="+HttpUtils.truncate(encTerms));
// we ask for 10 since half of them are CIDs, which we ignore
args.put("limit", "10");
args.put("types", "-1");
args.put("format", "json");
args.put("query", encTerms);
manager.info("URL: "+url+"?types=-1&limit=10&format=json"+"&identifiers="+HttpUtils.truncate(encTerms));

// Get the results
// System.out.println("Getting STITCH term resolution");
results = HttpUtils.postJSON(url, args, manager);

if (results != null) {
updateAnnotations(results, encTerms, species);
updateAnnotations(results, encTerms, species, Databases.JENSENLAB.getAPIName());
}
results = null;
}
Expand Down Expand Up @@ -526,9 +550,9 @@ public List<String> combineIds(Map<String, String> reverseMap) {
return ids;
}

private void updateAnnotations(JSONObject results, String terms, Species species) {
private void updateAnnotations(JSONObject results, String terms, Species species, String useDATABASE) {
Map<String, List<Annotation>> newAnnotations = Annotation.getAnnotations(results,
terms, species);
terms, species, useDATABASE);
for (String newAnn : newAnnotations.keySet()) {
List<Annotation> allAnn = new ArrayList<Annotation>(newAnnotations.get(newAnn));
if (annotations.containsKey(newAnn)) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -194,7 +194,7 @@ public void run(TaskMonitor monitor) {
Map<String, String> args = new HashMap<>();
if (selectedType.equals(ModelUtils.COMPOUND)) {
useDatabase = Databases.STITCH.getAPIName();
args.put("filter", "CIDm%%");
args.put("filter", "CIDm%");
args.put("existing",existing.trim());
args.put("score", conf.toString());
args.put("database", database);
Expand All @@ -215,7 +215,7 @@ public void run(TaskMonitor monitor) {
} else {
useDatabase = Databases.STRING.getAPIName();
filterString = String.valueOf(selSpecies.getTaxId());
args.put("filter", filterString + ".%%");
args.put("filter", filterString + ".%");
args.put("existing",existing.trim());
args.put("score", conf.toString());
args.put("database", database);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -121,9 +121,9 @@ else if (useDATABASE.equals(Databases.STRINGDB.getAPIName()))
if (additionalNodes > 0) {
args.put("additional", Integer.toString(additionalNodes));
if (useDATABASE.equals(Databases.STRING.getAPIName())) {
args.put("filter", String.valueOf(species.getTaxId()) + ".%%");
args.put("filter", String.valueOf(species.getTaxId()) + ".%");
} else {
args.put("filter", String.valueOf(species.getTaxId()) + ".%%|CIDm%%");
args.put("filter", String.valueOf(species.getTaxId()) + ".%|CIDm%");
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -118,9 +118,9 @@ public void run(TaskMonitor monitor) {
if (additionalNodes > 0) {
args.put("additional", Integer.toString(additionalNodes));
if (useDATABASE.equals(Databases.STRING.getAPIName())) {
args.put("filter", taxString + ".%%");
args.put("filter", taxString + ".%");
} else if (useDATABASE.equals(Databases.STITCH.getAPIName())) {
args.put("filter", taxString + ".%%|CIDm%%");
args.put("filter", taxString + ".%|CIDm%");
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -506,14 +506,16 @@ private void showTableRow(JTable table, String term, Map<String, ResolveTableMod
TableRowSorter sorter = new TableRowSorter(tableModelMap.get(term));
sorter.setSortable(0, false);
sorter.setSortable(1, true);
sorter.setSortable(2, false);
sorter.setSortable(2, true);
sorter.setSortable(3, false);
table.setModel(tableModelMap.get(term));
table.setRowSorter(sorter);
table.setAutoResizeMode( JTable.AUTO_RESIZE_OFF );
table.getColumnModel().getColumn(2).setCellRenderer(new TextAreaRenderer());
table.getColumnModel().getColumn(3).setCellRenderer(new TextAreaRenderer());
table.getColumnModel().getColumn(0).setPreferredWidth(50);
table.getColumnModel().getColumn(1).setPreferredWidth(75);
table.getColumnModel().getColumn(2).setPreferredWidth(525);
table.getColumnModel().getColumn(2).setPreferredWidth(75);
table.getColumnModel().getColumn(3).setPreferredWidth(525);
}

public void cancel() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ public ResolveTableModel(GetTermsPanel wsClient, String term, List<Annotation> a
}

@Override
public int getColumnCount() { return 3; }
public int getColumnCount() { return 4; }

@Override
public int getRowCount() {
Expand All @@ -48,6 +48,8 @@ public String getColumnName(int columnIndex) {
case 1:
return "Name";
case 2:
return "ID";
case 3:
return "Description";
}
return "";
Expand All @@ -62,6 +64,8 @@ public Class<?> getColumnClass(int columnIndex) {
return String.class;
case 2:
return String.class;
case 3:
return String.class;
}
return null;
}
Expand All @@ -75,6 +79,8 @@ public Object getValueAt(int rowIndex, int columnIndex) {
case 1:
return ann.getPreferredName();
case 2:
return ann.getStringId();
case 3:
return ann.getAnnotation();
}
return null;
Expand Down

0 comments on commit 23cfcdc

Please sign in to comment.