/** * this is the primary method that sends a query to the CIPRes REST service. It expects to receive * an XML file, which it returns in Document if the root tag matc hes xmlRootTag */ public Document cipresQuery(HttpClient httpclient, String URL, String xmlRootTag) { if (StringUtil.blank(URL)) return null; try { HttpGet httpget = new HttpGet(URL); httpget.addHeader("cipres-appkey", CIPRESkey); try { HttpResponse response = httpclient.execute(httpget); HttpEntity responseEntity = response.getEntity(); InputStream instream = responseEntity.getContent(); BufferedReader br = new BufferedReader(new InputStreamReader(instream)); String line = ""; StringBuffer sb = new StringBuffer(); while ((line = br.readLine()) != null) { sb.append(line + StringUtil.lineEnding()); } Document cipresResponseDoc = loadXMLFile(xmlRootTag, sb.toString()); if (cipresResponseDoc != null && verbose) { ownerModule.logln(sb.toString()); } if (cipresResponseDoc == null) { Document errorDoc = loadXMLFile(sb.toString()); if (errorDoc != null) reportError(errorDoc, "Error in communicating with CIPRes", true); } EntityUtils.consume(response.getEntity()); return cipresResponseDoc; } catch (IOException e) { Debugg.printStackTrace(e); } catch (Exception e) { Debugg.printStackTrace(e); } } catch (Exception e) { Debugg.printStackTrace(e); } return null; }
/*................................................................................................*/ private boolean hireFillers(String directoryPath) { String[] files = directory.list(); String treePath; for (int i = 0; i < files.length; i++) { if (files[i] != null) { String fileLowerCase = files[i].toLowerCase(); if (fileLowerCase.endsWith(".nex") || fileLowerCase.endsWith(".nexus")) { treePath = directoryPath + MesquiteFile.fileSeparator + files[i]; File treeFile = new File(treePath); String treeFileName = treeFile.getName(); if (StringUtil.blank(treeFileName)) { return false; } TreeBlockFiller newFiller; newFiller = (TreeBlockFiller) hireNamedEmployee(TreeBlockFiller.class, "#SampleOneTreeFromFile"); // treePath); if (newFiller != null) { if (((SampleOneTreeFromFile) newFiller).setFilePath(treePath) && ((SampleOneTreeFromFile) newFiller).processFile()) { fillerTasks.addElement(newFiller); } else Debugg.println( "Filler " + i + " processFile = " + ((SampleOneTreeFromFile) newFiller).processFile()); } } } } return true; }
/*.................................................................................................................*/ public void getResults(HttpClient httpclient, String jobURL) { String resultsUri = getResultsDirectory(jobURL); if (StringUtil.notEmpty(resultsUri)) { Document cipresResponseDoc = cipresQuery(httpclient, resultsUri, "results"); if (cipresResponseDoc != null) { CipresJobFile[] cipresJobFiles = processFilesDocument(cipresResponseDoc); for (int job = 0; job < cipresJobFiles.length; job++) { Debugg.println("fileName: " + cipresJobFiles[job].getFileName()); Debugg.println(" downloadURL: " + cipresJobFiles[job].getDownloadURL()); Debugg.println(" downloadTitle: " + cipresJobFiles[job].getDownloadTitle()); Debugg.println(" length: " + cipresJobFiles[job].getLength()); } previousCipresJobFiles = cipresJobFiles.clone(); } } }
/*.................................................................................................................*/ public void deleteJob(HttpClient httpclient, String URL) { HttpDelete httpdelete = new HttpDelete(URL); httpdelete.addHeader("cipres-appkey", CIPRESkey); try { HttpResponse response = httpclient.execute(httpdelete); } catch (IOException e) { Debugg.printStackTrace(e); } }
/*.................................................................................................................*/ public void cipresDownload(HttpClient httpclient, String URL, String filePath) { HttpGet httpget = new HttpGet(URL); httpget.addHeader("cipres-appkey", CIPRESkey); try { HttpResponse response = httpclient.execute(httpget); HttpEntity responseEntity = response.getEntity(); BufferedInputStream bis = new BufferedInputStream(responseEntity.getContent()); BufferedOutputStream bos = new BufferedOutputStream(new FileOutputStream(new File(filePath))); int inByte; while ((inByte = bis.read()) != -1) bos.write(inByte); bis.close(); bos.close(); EntityUtils.consume(response.getEntity()); } catch (IOException e) { Debugg.printStackTrace(e); } }
/** The core method that initiates a job on CIPRes. */ public boolean postJob( HttpClient httpclient, MultipartEntityBuilder builder, MesquiteString jobURL) { if (builder == null) return false; String URL = baseURL + "/job/" + username; HttpPost httppost = new HttpPost(URL); httppost.addHeader("cipres-appkey", CIPRESkey); // some of this from // http://stackoverflow.com/questions/18964288/upload-a-file-through-an-http-form-via-multipartentitybuilder-with-a-progress HttpEntity cipresEntity = builder.build(); httppost.setEntity(cipresEntity); try { HttpResponse response = httpclient.execute(httppost); HttpEntity responseEntity = response.getEntity(); InputStream instream = responseEntity.getContent(); BufferedReader br = new BufferedReader(new InputStreamReader(instream)); StringBuffer sb = new StringBuffer(); String line = ""; while ((line = br.readLine()) != null) { sb.append(line + StringUtil.lineEnding()); } Document cipresResponseDoc = loadXMLFile("jobstatus", sb.toString()); // let's see how it went boolean success = false; if (cipresResponseDoc != null) { processJobSubmissionResponse(cipresResponseDoc, jobURL); if (verbose) ownerModule.logln(sb.toString()); if (jobURL != null) success = StringUtil.notEmpty(jobURL.getValue()); else success = true; } else { cipresResponseDoc = loadXMLFile(sb.toString()); reportError(cipresResponseDoc, "Error with CIPRes run", false); } EntityUtils.consume(response.getEntity()); return success; } catch (IOException e) { Debugg.printStackTrace(e); } return false; }
/*.................................................................................................................*/ public Object doCommand(String commandName, String arguments, CommandChecker checker) { if (checker.compare( this.getClass(), "Returns the matrix source", null, commandName, "getMatrixSource")) { return matrixSourceTask; } else if (checker.compare( this.getClass(), "Copies the data for selected taxon", null, commandName, "copyData")) { if (observedStates == null) return null; CharacterData data = observedStates.getParentData(); if (data == null) return null; int it = MesquiteInteger.fromString(parser.getFirstToken(arguments)); if (MesquiteInteger.isCombinable(it)) { StringBuffer sb = new StringBuffer(); data.copyDataFromRowIntoBuffer(it, sb); if (StringUtil.notEmpty(sb.toString())) { localCopyDataClipboard = sb.toString(); localCopyData = data; localCopyDataTaxon = data.getTaxa().getTaxonName(it); } else { localCopyDataClipboard = null; localCopyData = null; localCopyDataTaxon = null; } } return null; } else if (checker.compare( this.getClass(), "Pastes the data for selected taxon", null, commandName, "pasteData")) { if (observedStates == null) return null; CharacterData data = observedStates.getParentData(); if (data == null) return null; int it = MesquiteInteger.fromString(parser.getFirstToken(arguments)); if (MesquiteInteger.isCombinable(it) && StringUtil.notEmpty(localCopyDataClipboard)) { data.pasteDataFromStringIntoTaxon(it, localCopyDataClipboard); } return null; } else if (checker.compare( this.getClass(), "Pastes the data for selected taxon", null, commandName, "deleteDataTouched")) { if (observedStates == null) return null; CharacterData data = observedStates.getParentData(); if (data == null) return null; int it = MesquiteInteger.fromString(parser.getFirstToken(arguments)); Debugg.println("prepare to delete row: " + it); if (MesquiteInteger.isCombinable(it)) { if (!AlertDialog.query( containerOfModule(), "Delete Data?", "Are you sure you want to delete the data for taxon " + data.getTaxa().getTaxonName(it) + " in the matrix \"" + data.getName() + "\"", "No", "Yes")) { zapData(data, it); } } return null; } else if (checker.compare( this.getClass(), "Deletes the data for selected taxa", null, commandName, "deleteData")) { if (observedStates == null) return null; captureCharacterDataFromObservedStates(); if (data == null) return null; if (!AlertDialog.query( containerOfModule(), "Delete Data?", "Are you sure you want to delete the data for these taxa in the matrix \"" + data.getName() + "\"", "No", "Yes")) zapData(data); return null; } else if (checker.compare( this.getClass(), "deleteds () and anything between", null, commandName, "deletePrepended")) { if (observedStates == null || taxa == null) return null; boolean anySelected = taxa.anySelected(); int myColumn = -1; if (getEmployer() instanceof ListModule) { myColumn = ((ListModule) getEmployer()).getMyColumn(this); if (table != null) anySelected = anySelected || table.anyCellSelectedInColumnAnyWay(myColumn); } for (int it = 0; it < taxa.getNumTaxa(); it++) { if ((!anySelected || selected(taxa, it, myColumn))) { String note = getNote(it); while (!StringUtil.blank(note) && note.indexOf("(") >= 0) { int start = note.indexOf("("); int end = note.indexOf(")"); String firstBit = ""; if (start > 0) firstBit = note.substring(0, start); note = firstBit + note.substring(end + 1, note.length()); } setNote(it, note); } } outputInvalid(); parametersChanged(); return null; } else if (checker.compare(this.getClass(), "deletes *", null, commandName, "deleteStar")) { if (observedStates == null || taxa == null) return null; boolean anySelected = taxa.anySelected(); int myColumn = -1; if (getEmployer() instanceof ListModule) { myColumn = ((ListModule) getEmployer()).getMyColumn(this); if (table != null) anySelected = anySelected || table.anyCellSelectedInColumnAnyWay(myColumn); } for (int it = 0; it < taxa.getNumTaxa(); it++) { if ((!anySelected || selected(taxa, it, myColumn))) { String note = getNote(it); while (!StringUtil.blank(note) && note.indexOf("*") >= 0) { int start = note.indexOf("*"); String firstBit = ""; if (start > 0) firstBit = note.substring(0, start); note = firstBit + note.substring(start + 1, note.length()); } setNote(it, note); } } outputInvalid(); parametersChanged(); return null; } else if (checker.compare( this.getClass(), "Prepends to the note the sequence length (including N\'s and ?\'s) for the selected taxa", null, commandName, "prependLength")) { if (observedStates == null || taxa == null) return null; boolean anySelected = taxa.anySelected(); int myColumn = -1; if (getEmployer() instanceof ListModule) { myColumn = ((ListModule) getEmployer()).getMyColumn(this); if (table != null) anySelected = anySelected || table.anyCellSelectedInColumnAnyWay(myColumn); } for (int it = 0; it < taxa.getNumTaxa(); it++) { if (hasData(it) && (!anySelected || selected(taxa, it, myColumn))) { String note = getNote(it); if (StringUtil.blank(note)) note = "(" + sequenceLength(it) + ")"; else note = "(" + sequenceLength(it) + ") " + note; setNote(it, note); } } outputInvalid(); parametersChanged(); return null; } else if (checker.compare( this.getClass(), "Prepends to the note the number of non-missing sites (not including N\'s and ?\'s) for the selected taxa", null, commandName, "prependNumSites")) { if (observedStates == null || taxa == null) return null; boolean anySelected = taxa.anySelected(); int myColumn = -1; if (getEmployer() instanceof ListModule) { myColumn = ((ListModule) getEmployer()).getMyColumn(this); if (table != null) anySelected = anySelected || table.anyCellSelectedInColumnAnyWay(myColumn); } for (int it = 0; it < taxa.getNumTaxa(); it++) { if (hasData(it) && (!anySelected || selected(taxa, it, myColumn))) { String note = getNote(it); if (StringUtil.blank(note)) note = "(" + numSites(it) + ")"; else note = "(" + numSites(it) + ") " + note; setNote(it, note); } } outputInvalid(); parametersChanged(); return null; } else if (checker.compare( this.getClass(), "Deletes the notes for the selected taxa", null, commandName, "deleteAnnotation")) { if (observedStates == null || taxa == null) return null; boolean anySelected = taxa.anySelected(); int myColumn = -1; if (getEmployer() instanceof ListModule) { myColumn = ((ListModule) getEmployer()).getMyColumn(this); if (table != null) anySelected = anySelected || table.anyCellSelectedInColumnAnyWay(myColumn); } for (int it = 0; it < taxa.getNumTaxa(); it++) { if (hasData(it) && (!anySelected || selected(taxa, it, myColumn))) { setNote(it, null); } } outputInvalid(); parametersChanged(); return null; } else return super.doCommand(commandName, arguments, checker); }