use of cbit.vcell.xml.ExternalDocInfo in project vcell by virtualcell.
the class BioModelsNetJPanel method searchImport.
private void searchImport() {
AsynchClientTask searchTask = new AsynchClientTask("Search BioModels", AsynchClientTask.TASKTYPE_NONSWING_BLOCKING) {
@Override
public void run(Hashtable<String, Object> hashTable) throws Exception {
Hashtable<String, String> vcellCompatibleBioModels_ID_Name_Hash = readVCellCompatibleBioModels_ID_Name_Hash();
ClientTaskStatusSupport clientTaskStatusSupport = getClientTaskStatusSupport();
// clientTaskStatusSupport.setProgress(0);
clientTaskStatusSupport.setMessage("Initializing BioModels Search...");
BioModelsWebServicesServiceLocator bioModelsWebServicesServiceLocator = new BioModelsWebServicesServiceLocator();
BioModelsWebServices bioModelsWebServices = bioModelsWebServicesServiceLocator.getBioModelsWebServices();
// clientTaskStatusSupport.setProgress(10);
clientTaskStatusSupport.setMessage("Executing BioModels Search...");
TreeSet<String> modelIDsMergedSearch = new TreeSet<String>();
boolean bHasModelEntryID = modelEntryIDTextField.getText() != null && modelEntryIDTextField.getText().length() > 0;
if (bHasModelEntryID) {
clientTaskStatusSupport.setMessage("Executing BioModels Search (Model ID matches)...");
// String[] modelIDsAll = bioModelsWebServices.getAllModelsId();
String[] modelIDsAll = vcellCompatibleBioModels_ID_Name_Hash.keySet().toArray(new String[0]);
for (int i = 0; i < modelIDsAll.length; i++) {
if (modelIDsAll[i].toUpperCase().indexOf(modelEntryIDTextField.getText().toUpperCase()) != -1) {
modelIDsMergedSearch.add(modelIDsAll[i]);
}
}
}
boolean bHasModelName = modelNameTextField.getText() != null && modelNameTextField.getText().length() > 0;
if (bHasModelName) {
clientTaskStatusSupport.setMessage("Executing BioModels Search (Model Name matches)...");
TreeSet<String> modelIDsByNameSet = new TreeSet<String>();
// String[] modelIDsByName = bioModelsWebServices.getModelsIdByName(modelNameTextField.getText());
String[] modelIDsAll = vcellCompatibleBioModels_ID_Name_Hash.keySet().toArray(new String[0]);
for (int i = 0; i < modelIDsAll.length; i++) {
if (vcellCompatibleBioModels_ID_Name_Hash.get(modelIDsAll[i]).toUpperCase().indexOf(modelNameTextField.getText().toUpperCase()) != -1) {
modelIDsByNameSet.add(modelIDsAll[i]);
}
}
// }
if (bHasModelEntryID) {
// intersection
modelIDsMergedSearch.retainAll(modelIDsByNameSet);
} else {
modelIDsMergedSearch = modelIDsByNameSet;
}
}
boolean bHasModelPublication = publicationTextField.getText() != null && publicationTextField.getText().length() > 0;
if (bHasModelPublication) {
clientTaskStatusSupport.setMessage("Executing BioModels Search (Publication matches)...");
TreeSet<String> modelIDsByPublicationSet = new TreeSet<String>();
String[] modelIDsbyPublication = bioModelsWebServices.getModelsIdByPublication(publicationTextField.getText());
for (int i = 0; i < modelIDsbyPublication.length; i++) {
modelIDsByPublicationSet.add(modelIDsbyPublication[i]);
}
if (bHasModelEntryID || bHasModelName) {
// intersection
modelIDsMergedSearch.retainAll(modelIDsByPublicationSet);
} else {
modelIDsMergedSearch = modelIDsByPublicationSet;
}
}
if (!(bHasModelEntryID || bHasModelName || bHasModelPublication)) {
// Get all models
// modelIDsMergedSearch.addAll(Arrays.asList(bioModelsWebServices.getAllModelsId()));
modelIDsMergedSearch.addAll(Arrays.asList(vcellCompatibleBioModels_ID_Name_Hash.keySet().toArray(new String[0])));
}
// clientTaskStatusSupport.setProgress(COLLECTION_PROGRESS);
if (modelIDsMergedSearch.size() > 0) {
// Intersect with supported BioModel IDs
modelIDsMergedSearch.retainAll(Arrays.asList(vcellCompatibleBioModels_ID_Name_Hash.keySet().toArray(new String[0])));
clientTaskStatusSupport.setMessage("Found " + modelIDsMergedSearch.size() + " VCell compatible BioModels");
String[] modelIDMergedSearchArr = modelIDsMergedSearch.toArray(new String[0]);
TreeMap<String, String> model_ID_Name_Map = new TreeMap<String, String>();
String nameNotfound = "NameNotFound_0";
for (int i = 0; i < modelIDMergedSearchArr.length; i++) {
if (clientTaskStatusSupport.isInterrupted()) {
// DialogUtils.showWarningDialog(BioModelsNetJPanel.this, "Search Cancelled");
throw UserCancelException.CANCEL_GENERIC;
}
try {
// clientTaskStatusSupport.setMessage("Retrieving "+modelIDMergedSearchArr[i]+" "+(i+1)+" of "+modelIDMergedSearchArr.length);
// String modelName = bioModelsWebServices.getModelNameById(modelIDMergedSearchArr[i]);
String modelName = vcellCompatibleBioModels_ID_Name_Hash.get(modelIDMergedSearchArr[i]);
// Assume model names are unique
model_ID_Name_Map.put(modelName, modelIDMergedSearchArr[i]);
} catch (Exception e) {
// For some reason Web Service API sometimes throws java.util.NoSuchElementException
// if id exists but name can't be found.
e.printStackTrace();
model_ID_Name_Map.put(nameNotfound, modelIDMergedSearchArr[i]);
nameNotfound = TokenMangler.getNextEnumeratedToken(nameNotfound);
}
}
// Make name,id rowdata for tablelist to allow user selection
String[] sortedModelNames = model_ID_Name_Map.keySet().toArray(new String[0]);
final String[][] rowData = new String[sortedModelNames.length][2];
for (int i = 0; i < sortedModelNames.length; i++) {
rowData[i][0] = sortedModelNames[i];
rowData[i][1] = model_ID_Name_Map.get(sortedModelNames[i]);
}
// Allow user to select model for opening
final String importNow = "Import";
final String cancel = "Cancel";
DialogUtils.TableListResult result = DialogUtils.showComponentOptionsTableList(BioModelsNetJPanel.this, "Select a BioModel to import", new String[] { "Model Names", "BioModels Entry ID" }, rowData, ListSelectionModel.SINGLE_SELECTION, null, new String[] { importNow, cancel }, importNow, new Comparator<Object>() {
public int compare(Object o1, Object o2) {
if (o1 instanceof String && o2 instanceof String) {
return ((String) o1).compareTo((String) o2);
}
throw new RuntimeException("row data should only be String");
}
});
if (result.selectedOption != null && result.selectedOption.equals(importNow)) {
// Close Dialog showing "this" panel so ProgressPopup not obscured during openDocument
SwingUtilities.invokeAndWait(new Runnable() {
public void run() {
disposeParentDialog();
}
});
closeClientTaskStatusSupport(clientTaskStatusSupport);
// Download and open (in new window) SBML document selected by user
String bioModelSBML = bioModelsWebServices.getModelSBMLById(rowData[result.selectedTableRows[0]][1]);
final ExternalDocInfo externalDocInfo = new ExternalDocInfo(bioModelSBML, sortedModelNames[result.selectedTableRows[0]]);
new Thread(new Runnable() {
public void run() {
documentWindow.getTopLevelWindowManager().getRequestManager().openDocument(externalDocInfo, documentWindow.getTopLevelWindowManager(), true);
}
}).start();
}
} else {
closeClientTaskStatusSupport(clientTaskStatusSupport);
DialogUtils.showWarningDialog(BioModelsNetJPanel.this, "No BioModels found matching current search criteria.");
throw UserCancelException.CANCEL_GENERIC;
}
}
};
ClientTaskDispatcher.dispatch(BioModelsNetJPanel.this, new Hashtable<String, Object>(), new AsynchClientTask[] { searchTask }, true, true, null, true);
}
use of cbit.vcell.xml.ExternalDocInfo in project vcell by virtualcell.
the class BioModelsNetPanel method importFromBioModelsNet.
public void importFromBioModelsNet() {
Object obj = tree.getLastSelectedPathComponent();
if (obj == null || !(obj instanceof BioModelNode)) {
return;
}
BioModelNode selectedNode = (BioModelNode) obj;
Object userObject = selectedNode.getUserObject();
if (!(userObject instanceof BioModelsNetModelInfo)) {
return;
}
BioModelsNetModelInfo bioModelsNetInfo = (BioModelsNetModelInfo) userObject;
String name = bioModelsNetInfo.getName();
String id = bioModelsNetInfo.getId();
AsynchClientTask task1 = new AsynchClientTask("Importing " + name, AsynchClientTask.TASKTYPE_NONSWING_BLOCKING) {
@Override
public void run(Hashtable<String, Object> hashTable) throws Exception {
ExternalDocInfo externalDocInfo = download(name, id);
if (externalDocInfo != null) {
hashTable.put("externalDocInfo", externalDocInfo);
}
System.out.println("done downloading");
}
};
AsynchClientTask task2 = new AsynchClientTask("Opening", AsynchClientTask.TASKTYPE_NONSWING_BLOCKING) {
@Override
public void run(Hashtable<String, Object> hashTable) throws Exception {
ExternalDocInfo externalDocInfo = (ExternalDocInfo) hashTable.get("externalDocInfo");
if (externalDocInfo == null) {
return;
}
documentWindowManager.getRequestManager().openDocument(externalDocInfo, documentWindowManager, true);
}
};
ClientTaskDispatcher.dispatch(documentWindowManager.getComponent(), new Hashtable<String, Object>(), new AsynchClientTask[] { task1, task2 }, false);
}
use of cbit.vcell.xml.ExternalDocInfo in project vcell by virtualcell.
the class BioModelsNetPanel method download.
private ExternalDocInfo download(String name, String id) throws Exception {
// C:\Users\vasilescu\.vcell\simdata
String simDataDir = ResourceUtil.getLocalRootDir().getAbsolutePath();
String tempDir = simDataDir + File.separator + "temp";
String destDirectory = tempDir + File.separator + id;
String zipFilePath = destDirectory + ".zip";
Path tempDirPath = Paths.get(tempDir);
// temp may not be there, we make it
Files.createDirectories(tempDirPath);
byte[] responseContent = null;
URL url = new URL(BeanUtils.getDynamicClientProperties().getProperty(PropertyLoader.BMDB_DOWNLOAD_URL) + id);
ByteArrayOutputStream baos = new ByteArrayOutputStream();
InputStream is = null;
try {
try {
is = url.openStream();
} catch (Exception e) {
e.printStackTrace();
if (is != null) {
is.close();
}
// Try with http instead of https
String newUrlString = url.toString().replaceFirst("^https", "http");
url = new URL(newUrlString);
is = url.openStream();
}
// Or whatever size you want to read in at a time.
byte[] byteChunk = new byte[4096];
int n;
while ((n = is.read(byteChunk)) > 0) {
baos.write(byteChunk, 0, n);
}
responseContent = baos.toByteArray();
} catch (IOException e) {
System.err.printf("Failed while reading bytes from %s: %s", url.toExternalForm(), e.getMessage());
// e.printStackTrace ();
throw new RuntimeException("Failed while reading bytes from: " + url.toExternalForm());
} finally {
if (is != null) {
is.close();
}
}
if (responseContent == null) {
throw new RuntimeException("Failed while reading bytes from: " + url.toExternalForm());
}
try {
File file = new File(zipFilePath);
Files.write(file.toPath(), responseContent, StandardOpenOption.CREATE, StandardOpenOption.WRITE, StandardOpenOption.TRUNCATE_EXISTING);
UnzipUtility uu = new UnzipUtility();
uu.unzip(zipFilePath, destDirectory);
} catch (IOException e) {
e.printStackTrace();
}
String unzippedPath = destDirectory + File.separator + id + ".xml";
String bioModelSBML = new String(Files.readAllBytes(Paths.get(unzippedPath)), StandardCharsets.UTF_8);
// bioModelSBML = bioModelSBML.replace("<notanumber/>", "<ci> a </ci>");
try {
// the original zip file
Files.deleteIfExists(Paths.get(zipFilePath));
// the unzipped SBML file
Files.deleteIfExists(Paths.get(unzippedPath));
// its directory
Files.deleteIfExists(Paths.get(destDirectory));
// Files.deleteIfExists(Paths.get(prettyXML)); // the pretty xml
} catch (IOException e) {
e.printStackTrace();
}
ExternalDocInfo externalDocInfo = ExternalDocInfo.createBioModelsNetExternalDocInfo(bioModelSBML, name);
return externalDocInfo;
}
use of cbit.vcell.xml.ExternalDocInfo in project vcell by virtualcell.
the class BioModelsNetPanel method importFromBioModelsNetTest.
public void importFromBioModelsNetTest() {
try {
int i = 731;
String name = "tested" + i;
String id = "BIOMD0000000" + i;
AsynchClientTask task1 = new AsynchClientTask("Importing " + name, AsynchClientTask.TASKTYPE_NONSWING_BLOCKING) {
@Override
public void run(Hashtable<String, Object> hashTable) throws Exception {
ExternalDocInfo externalDocInfo = download(name, id);
if (externalDocInfo != null) {
hashTable.put("externalDocInfo", externalDocInfo);
}
System.out.println("done downloading: " + id);
}
};
AsynchClientTask task2 = new AsynchClientTask("Opening", AsynchClientTask.TASKTYPE_NONSWING_BLOCKING) {
@Override
public void run(Hashtable<String, Object> hashTable) throws Exception {
ExternalDocInfo externalDocInfo = (ExternalDocInfo) hashTable.get("externalDocInfo");
if (externalDocInfo == null) {
return;
}
documentWindowManager.getRequestManager().openDocument(externalDocInfo, documentWindowManager, true);
System.out.println("opened: " + id);
}
};
ClientTaskDispatcher.dispatch(documentWindowManager.getComponent(), new Hashtable<String, Object>(), new AsynchClientTask[] { task1, task2 }, false);
System.out.println("task dispatched for " + id);
} catch (Exception e) {
System.err.println(e.getMessage());
}
}
use of cbit.vcell.xml.ExternalDocInfo in project vcell by virtualcell.
the class BioModelsNetPropertiesPanel method importFromBioModelsNet.
public void importFromBioModelsNet() {
AsynchClientTask task1 = new AsynchClientTask("Importing " + bioModelsNetModelInfo.getName(), AsynchClientTask.TASKTYPE_NONSWING_BLOCKING) {
@Override
public void run(Hashtable<String, Object> hashTable) throws Exception {
BioModelsWebServicesServiceLocator bioModelsWebServicesServiceLocator = new BioModelsWebServicesServiceLocator();
BioModelsWebServices bioModelsWebServices = bioModelsWebServicesServiceLocator.getBioModelsWebServices();
String bioModelSBML = bioModelsWebServices.getModelSBMLById(bioModelsNetModelInfo.getId());
ExternalDocInfo externalDocInfo = new ExternalDocInfo(bioModelSBML, bioModelsNetModelInfo.getName());
if (externalDocInfo != null) {
hashTable.put("externalDocInfo", externalDocInfo);
}
}
};
AsynchClientTask task2 = new AsynchClientTask("Opening", AsynchClientTask.TASKTYPE_NONSWING_BLOCKING) {
@Override
public void run(Hashtable<String, Object> hashTable) throws Exception {
ExternalDocInfo externalDocInfo = (ExternalDocInfo) hashTable.get("externalDocInfo");
if (externalDocInfo == null) {
return;
}
documentWindowManager.getRequestManager().openDocument(externalDocInfo, documentWindowManager, true);
}
};
ClientTaskDispatcher.dispatch(documentWindowManager.getComponent(), new Hashtable<String, Object>(), new AsynchClientTask[] { task1, task2 }, false);
}
Aggregations