@Override
  public QueryResult next(String chromosome, int position, QueryOptions options) {

    String featureType = options.getString("featureType", null);
    String featureClass = options.getString("featureClass", null);

    BasicDBList chunksId = new BasicDBList();
    String chunkId =
        chromosome
            + "_"
            + getChunkId(position, regulatoryRegionChunkSize)
            + "_"
            + regulatoryRegionChunkSize / 1000
            + "k";
    chunksId.add(chunkId);

    // TODO: Add query to find next item considering next chunk
    // db.regulatory_region.find({ "chromosome" : "19" , "start" : { "$gt" : 62005} , "featureType"
    // : "TF_binding_site_motif"}).sort({start:1}).limit(1)

    QueryBuilder builder;
    if (options.getString("strand") == null
        || (options.getString("strand").equals("1") || options.getString("strand").equals("+"))) {
      // db.core.find({chromosome: "1", start: {$gt: 1000000}}).sort({start: 1}).limit(1)
      builder =
          QueryBuilder.start("_chunkIds")
              .in(chunksId)
              .and("chromosome")
              .is(chromosome)
              .and("start")
              .greaterThan(position);
      options.put("sort", new BasicDBObject("start", 1));
      options.put("limit", 1);
    } else {
      builder =
          QueryBuilder.start("_chunkIds")
              .in(chunksId)
              .and("chromosome")
              .is(chromosome)
              .and("end")
              .lessThan(position);
      options.put("sort", new BasicDBObject("end", -1));
      options.put("limit", 1);
    }

    if (featureType != null) {
      builder.and("featureType").is(featureType);
    }
    if (featureClass != null) {
      builder.and("featureClass").is(featureClass);
    }
    System.out.println(builder.get());
    return executeQuery("result", builder.get(), options);
  }
  @GET
  @Path("/{fileId}/fetch")
  @Produces("application/json")
  public Response fetch(
      @PathParam("fileId") @DefaultValue("") String fileId,
      @QueryParam("backend") String backend,
      @QueryParam("dbName") String dbName,
      @QueryParam("bioformat") @DefaultValue("") String bioformat,
      @QueryParam("region") String region,
      @QueryParam("path") @DefaultValue("") String path,
      @QueryParam("view_as_pairs") @DefaultValue("false") boolean view_as_pairs,
      @QueryParam("include_coverage") @DefaultValue("true") boolean include_coverage,
      @QueryParam("process_differences") @DefaultValue("true") boolean process_differences,
      @QueryParam("histogram") @DefaultValue("false") boolean histogram,
      @QueryParam("interval") @DefaultValue("2000") int interval) {

    try {
      switch (bioformat) {
        case "vcf":
          break;
        case "bam":
          AlignmentStorageManager sm = StorageManagerFactory.getAlignmentStorageManager(backend);
          ObjectMap params = new ObjectMap();
          AlignmentDBAdaptor dbAdaptor = sm.getDBAdaptor(dbName, params);

          QueryOptions options = new QueryOptions();
          if (path != null && !path.isEmpty()) {
            String rootDir =
                OpenCGAStorageService.getInstance()
                    .getProperties()
                    .getProperty(
                        "OPENCGA.STORAGE.ROOTDIR",
                        "/home/cafetero/opencga/catalog/users/jcoll/projects/1/1/");
            options.put(
                AlignmentDBAdaptor.QO_BAM_PATH,
                Paths.get(rootDir, path.replace(":", "/")).toString());
          }
          options.put(AlignmentDBAdaptor.QO_FILE_ID, fileId);
          options.put(AlignmentDBAdaptor.QO_VIEW_AS_PAIRS, view_as_pairs);
          options.put(AlignmentDBAdaptor.QO_INCLUDE_COVERAGE, include_coverage);
          options.put(AlignmentDBAdaptor.QO_PROCESS_DIFFERENCES, process_differences);
          options.put(AlignmentDBAdaptor.QO_INTERVAL_SIZE, interval);
          options.put(AlignmentDBAdaptor.QO_HISTOGRAM, histogram);
          //                    options.put(AlignmentDBAdaptor.QO_COVERAGE_CHUNK_SIZE, chunkSize);

          QueryResult queryResult;
          if (histogram) {
            queryResult = dbAdaptor.getAllIntervalFrequencies(new Region(region), options);
          } else {
            queryResult =
                dbAdaptor.getAllAlignmentsByRegion(Arrays.asList(new Region(region)), options);
          }

          return createOkResponse(queryResult);
        default:
          return createErrorResponse("Unknown bioformat " + bioformat);
      }
    } catch (Exception e) {
      e.printStackTrace();
      return createErrorResponse(e.toString());
    }
    return createErrorResponse("Unimplemented!");
  }