package com.google.cloud.genomics.utils;

import java.io.IOException;
import java.security.GeneralSecurityException;
import java.util.List;

import com.google.api.services.genomics.Genomics;
import com.google.api.services.genomics.model.CallSet;
import com.google.api.services.genomics.model.ReadGroupSet;
import com.google.api.services.genomics.model.ReferenceBound;
import com.google.api.services.genomics.model.SearchCallSetsRequest;
import com.google.api.services.genomics.model.SearchReadGroupSetsRequest;
import com.google.api.services.genomics.model.SearchVariantSetsRequest;
import com.google.api.services.genomics.model.VariantSet;
import com.google.common.collect.Lists;

/**
 * Convenience routines for fetching ids in the hierarchy of data within the Genomics API and other data lookups.
 */
public class GenomicsUtils {
  
  /**
   * Gets ReadGroupSetIds from a given datasetId using the Genomics API.
   */
  public static List<String> getReadGroupSetIds(String datasetId, GenomicsFactory.OfflineAuth auth)
      throws IOException, GeneralSecurityException {
    List<String> output = Lists.newArrayList();
    Iterable<ReadGroupSet> rgs = Paginator.ReadGroupSets.create(
        auth.getGenomics(auth.getDefaultFactory()))
        .search(new SearchReadGroupSetsRequest().setDatasetIds(Lists.newArrayList(datasetId)),
            "readGroupSets/id,nextPageToken");
    for (ReadGroupSet r : rgs) {
      output.add(r.getId());
    }
    if (output.isEmpty()) {
      throw new IOException("Dataset " + datasetId + " does not contain any ReadGroupSets");
    }
    return output;
  }
  
  /**
   * Gets VariantSetIds from a given datasetId using the Genomics API.
   */
  public static List<String> getVariantSetIds(String datasetId, GenomicsFactory.OfflineAuth auth)
      throws IOException, GeneralSecurityException {
    List<String> output = Lists.newArrayList();
    Iterable<VariantSet> vs = Paginator.Variantsets.create(
        auth.getGenomics(auth.getDefaultFactory()))
        .search(new SearchVariantSetsRequest().setDatasetIds(Lists.newArrayList(datasetId)),
            "variantSets/id,nextPageToken");
    for (VariantSet v : vs) {
      output.add(v.getId());
    }
    if (output.isEmpty()) {
      throw new IOException("Dataset " + datasetId + " does not contain any VariantSets");
    }
    return output;
  }
  
  /**
   * Gets CallSets Names for a given variantSetId using the Genomics API.
   */
  public static List<String> getCallSetsNames(String variantSetId, GenomicsFactory.OfflineAuth auth)
      throws IOException, GeneralSecurityException {
    List<String> output = Lists.newArrayList();
    Iterable<CallSet> cs = Paginator.Callsets.create(
        auth.getGenomics(auth.getDefaultFactory()))
        .search(new SearchCallSetsRequest().setVariantSetIds(Lists.newArrayList(variantSetId)),
            "callSets/name,nextPageToken");
    for (CallSet c : cs) {
      output.add(c.getName());
    }
    if (output.isEmpty()) {
      throw new IOException("VariantSet " + variantSetId + " does not contain any CallSets");
    }
    return output;
  }

  /**
   * Gets the ReferenceBounds for a given variantSetId using the Genomics API.
   */
  public static List<ReferenceBound> getReferenceBounds(String variantSetId, GenomicsFactory.OfflineAuth auth)
      throws IOException, GeneralSecurityException {
    Genomics genomics = auth.getGenomics(auth.getDefaultFactory());
    VariantSet variantSet = genomics.variantsets().get(variantSetId).execute();
    return variantSet.getReferenceBounds();
  }

}

@@ -900,38 +900,318 @@
 e Genomics API.%0A
+   *%0A   * @param datasetId The id of the dataset to query.%0A   * @param auth The OfflineAuth for the API request.%0A   * @return The list of readGroupSetIds in the dataset.%0A   * @throws IOException If dataset does not contain any readGroupSets.%0A   * @throws GeneralSecurityException%0A
    */%0A
-
   public static 
@@ -1944,32 +1944,308 @@
 e Genomics API.%0A
+   *%0A   * @param datasetId The id of the dataset to query.%0A   * @param auth The OfflineAuth for the API request.%0A   * @return The list of variantSetIds in the dataset.%0A   * @throws IOException If dataset does not contain any variantSets.%0A   * @throws GeneralSecurityException%0A
    */%0A  public s
@@ -2971,32 +2971,319 @@
 e Genomics API.%0A
+   * %0A   * @param variantSetId The id of the variantSet to query.%0A   * @param auth The OfflineAuth for the API request.%0A   * @return The list of callSet names in the variantSet.%0A   * @throws IOException If variantSet does not contain any CallSets.%0A   * @throws GeneralSecurityException%0A%0A
    */%0A  public s
@@ -3938,24 +3938,24 @@
 %0A  %7D%0A%0A  /**%0A
-
    * Gets th
@@ -4017,24 +4017,267 @@
 nomics API.%0A
+   *%0A   * @param variantSetId The id of the variantSet to query.%0A   * @param auth The OfflineAuth for the API request.%0A   * @return The list of reference bounds in the variantSet.%0A   * @throws IOException%0A   * @throws GeneralSecurityException%0A
    */%0A  publ

Add javadocs for code that came from dataflow-java.