@Override
 public Options getOptions(AnnotatedPluginDocument... documents)
     throws DocumentOperationException {
   Options options = new Options(this.getClass());
   options.addStringOption("query", "query", "PROJECT_NAME = \"BARCODE\"");
   return options;
 }
  private static void addMinMaxOptionsAndHideOriginal(Options options, SimpleListener listener) {
    for (Options.Option option : options.getOptions()) {
      if (option.isAdvanced()) {
        // At this stage we don't want to deal with batches of advanced options.
        // The advanced options at the moment are only for MUSCLE and there are a lot of them
        // which would bloat the interface too much if we added them all on one line as a
        // MultiValueOption
        option.setAdvanced(
            false); // add advanced option back to basic panel, since there is a layout issue on
                    // small screen
        continue;
      }

      MultiValueOption multiValueOption = null;
      if (option instanceof Options.IntegerOption) {
        multiValueOption = new IntegerMultiValueOption((Options.IntegerOption) option);
      } else if (option instanceof Options.DoubleOption) {
        multiValueOption = new DoubleMultiValueOption((Options.DoubleOption) option);
      }

      if (multiValueOption != null) {
        option.setVisible(false);
        options.addCustomOption(multiValueOption);
        multiValueOption.addChangeListener(listener);
      }
    }

    for (Options childOptions : options.getChildOptions().values()) {
      addMinMaxOptionsAndHideOriginal(childOptions, listener);
    }
  }
 private static int getBatchSize(Options options) {
   int total = 1;
   for (Option option : options.getOptions()) {
     if (option instanceof MultiValueOption<?>) {
       total *= ((MultiValueOption<?>) option).getValue().size();
     }
   }
   for (Options childOptions : options.getChildOptions().values()) {
     total *= getBatchSize(childOptions);
   }
   return total;
 }
 @Override
 Options.Option<Integer, ? extends JComponent> addOption(
     Options options, String name, String label, boolean useMinMax) {
   if (useMinMax) {
     return options.addIntegerOption(
         name,
         label,
         baseOption.getDefaultValue(),
         baseOption.getMinimum(),
         baseOption.getMaximum());
   } else {
     return options.addIntegerOption(name, label, 0);
   }
 }
 private static Map<String, MultiValueOption<?>> getMultiValueOptions(
     String prefix, Options options) {
   Map<String, MultiValueOption<?>> result = new HashMap<String, MultiValueOption<?>>();
   for (Option option : options.getOptions()) {
     if (option instanceof MultiValueOption) {
       result.put(
           prefix + option.getName().replace(MultiValueOption.SUFFIX, ""),
           (MultiValueOption) option);
     }
   }
   for (Map.Entry<String, Options> entry : options.getChildOptions().entrySet()) {
     result.putAll(getMultiValueOptions(prefix + entry.getKey() + ".", entry.getValue()));
   }
   return result;
 }
 @Override
 public Options getOptions(DocumentOperationInput operationInput)
     throws DocumentOperationException {
   Options options = new Options(GoToDocumentsOperation.class);
   options.addLabel("Unique IDs (URNs), one per line:", false, true);
   String defaultString = "";
   for (AnnotatedPluginDocument annotatedPluginDocument :
       operationInput.getInputDocumentsArray()) {
     defaultString += annotatedPluginDocument.getURN() + "\n";
   }
   options
       .addMultipleLineStringOption(URNS_OPTION_NAME, "", defaultString, 5, false)
       .setValue(defaultString);
   return options;
 }
 @Override
 public void performOperation(
     AnnotatedPluginDocument[] annotatedDocuments,
     ProgressListener progressListener,
     Options options,
     SequenceSelection sequenceSelection,
     OperationCallback callback)
     throws DocumentOperationException {
   try {
     String[] urnStrings = options.getValueAsString(URNS_OPTION_NAME).split("\n");
     List<URN> validUrns = new ArrayList<URN>();
     List<String> brokenUrns = new ArrayList<String>();
     List<URN> missingUrns = new ArrayList<URN>();
     for (String urnString : urnStrings) {
       try {
         String trimmedString = urnString.trim();
         if (trimmedString.isEmpty()) {
           continue;
         }
         URN urn = new URN(trimmedString);
         if (DocumentUtilities.getDocumentByURN(urn) == null) {
           missingUrns.add(urn);
           continue;
         }
         validUrns.add(urn);
       } catch (MalformedURNException e) {
         brokenUrns.add(urnString);
       }
     }
     if (!brokenUrns.isEmpty()) {
       if (!Dialogs.showContinueCancelDialog(
           "Some of the URNs you entered are invalid. They will be ignored:\n\n"
               + StringUtilities.join("\n", brokenUrns),
           "Invalid URNs",
           null,
           Dialogs.DialogIcon.INFORMATION)) {
         return;
       }
     }
     if (!missingUrns.isEmpty()) {
       if (!Dialogs.showContinueCancelDialog(
           "Some of the URNs you entered cannot be found. They will be ignored:\n\n"
               + StringUtilities.join("\n", missingUrns),
           "Missing URNs",
           null,
           Dialogs.DialogIcon.INFORMATION)) {
         return;
       }
     }
     DocumentUtilities.selectDocuments(validUrns);
   } finally {
     progressListener.setProgress(1.0);
   }
 }
  @Override
  public List<AnnotatedPluginDocument> performOperation(
      AnnotatedPluginDocument[] annotatedDocuments,
      ProgressListener progressListener,
      Options options)
      throws DocumentOperationException {
    String queryText = ((ExampleWorkflowOptions) options).getSearchQuery();
    // First create a composite progress to represent the 3 components of this example
    // 1) Getting the results from NCBI (80% of total time)
    // 2) Building the alignment (15% of total time)
    // 3) Building a tree (5% of total time)
    CompositeProgressListener compositeProgress =
        new CompositeProgressListener(progressListener, 0.8, 0.15, 0.05);

    // First get the ncbi database and search it:
    compositeProgress.beginSubtask("Searching NCBI");
    DatabaseService ncbiNucleotideService =
        (DatabaseService) PluginUtilities.getGeneiousService("NCBI_nucleotide_gbc");
    // The various services available are found by running this commented out code:
    // for (GeneiousService service : PluginUtilities.getGeneiousServices()) {
    //    System.out.println(service.getUniqueID());
    // }
    Query query = Query.Factory.createQuery(queryText);
    List<AnnotatedPluginDocument> sequences;
    try {
      sequences = ncbiNucleotideService.retrieve(query, compositeProgress);
    } catch (DatabaseServiceException e) {
      throw new DocumentOperationException("Failed to search NCBI database", e);
    }
    if (compositeProgress.isCanceled()) throw new DocumentOperationException.Canceled();
    if (sequences.size() < 3)
      throw new DocumentOperationException(
          "NCBI returned "
              + sequences.size()
              + " results, but we require at least 3 to build a tree");

    // Now get the alignment operation and perform the alignment:
    compositeProgress.beginSubtask("Build alignment");
    final DocumentOperation alignmentOperation =
        PluginUtilities.getCategoryOperation(GeneiousActionOptions.Category.Alignment);
    Options alignmentOptions =
        alignmentOperation.getOptions(
            sequences); // Get the options provided by the alignment operation.
    alignmentOptions.setValue(
        "operation", "MUSCLE_NUCLEOTIDE_"); // Tell it to use muscle for alignment.
    // To find out what options are available on the alignment do the following commented out line:
    // System.out.println(alignmentOptions.getDescriptionAndState());

    List<AnnotatedPluginDocument> alignment =
        alignmentOperation.performOperation(sequences, compositeProgress, alignmentOptions);
    if (compositeProgress.isCanceled()) throw new DocumentOperationException.Canceled();

    // Now get the tree operation and build a tree:
    compositeProgress.beginSubtask("Build tree");
    final DocumentOperation treeOperation =
        PluginUtilities.getCategoryOperation(GeneiousActionOptions.Category.TreeBuilding);
    Options treeOptions = treeOperation.getOptions(alignment);
    treeOptions.setValue("treeBuilding.buildMethod", "UPGMA");
    // To find out what options are available on the tree builder, do the following commented out
    // line:
    // System.out.println(treeOptions.getDescriptionAndState());

    final List<AnnotatedPluginDocument> treeResults =
        treeOperation.performOperation(alignment, compositeProgress, treeOptions);
    if (treeResults != null && treeResults.size() == 1) {
      treeResults.get(0).setName("Tree of " + queryText);
    }
    return treeResults;
  }
  @Override
  public List<AnnotatedPluginDocument> performOperation(
      AnnotatedPluginDocument[] annotatedDocuments,
      ProgressListener progressListener,
      Options options)
      throws DocumentOperationException {
    int PAGE_SIZE = 2;
    try {
      String query = options.getValueAsString("query");
      int count = 0;
      InputStream countInput = queryServer("query count " + query);
      // Get the response
      BufferedReader rd = new BufferedReader(new InputStreamReader(countInput));
      String line;
      while ((line = rd.readLine()) != null) {
        count = Integer.parseInt(line.trim());
      }
      rd.close();

      int numberOfPages = (int) Math.ceil(count / PAGE_SIZE);

      CompositeProgressListener composite =
          new CompositeProgressListener(progressListener, numberOfPages);

      for (int page = 0; page < numberOfPages; page++) {
        InputStream inputStream =
            queryServer("query page_size " + PAGE_SIZE + " page_number " + page + " text " + query);
        File outputFile =
            new File(
                System.getProperty("user.home")
                    + File.separator
                    + "traceData"
                    + File.separator
                    + "part"
                    + page
                    + ".tgz");
        BufferedReader in = new BufferedReader(new InputStreamReader(inputStream));
        BufferedOutputStream out = new BufferedOutputStream(new FileOutputStream(outputFile));
        List<String> ids = new ArrayList<String>();
        String line2 = null;
        while ((line2 = in.readLine()) != null) {
          if (composite.isCanceled()) {
            break;
          }
          System.out.println(line2);
          // out.write(n);
          ids.add(line2);
        }
        if (composite.isCanceled()) {
          break;
        }
        in.close();
        InputStream tgzIn = queryServer("retrieve_gz all " + StringUtilities.join(", ", ids));
        int n;
        int byteCount = 0;
        while ((n = tgzIn.read()) >= 0) {
          byteCount++;
          composite.setMessage("Downloaded " + byteCount + " bytes");
          if (composite.isCanceled()) {
            break;
          }
          // out.write(n);
        }
        tgzIn.close();
        out.close();
        if (composite.isCanceled()) {
          break;
        }
        composite.beginNextSubtask();
        break;
        // out.close();
      }

    } catch (IOException ex) {
      ex.toString();
      ex.printStackTrace();
      throw new DocumentOperationException(ex.getMessage(), ex);
    }
    return Collections.emptyList();
  }