/**
   * parses an answer object into the json exchange format for an answer
   *
   * @param answer the answer object that should be translated
   * @return json string in the exchange format between frontend and middleware
   */
  public static String answerQuery(QueryAnswer answer) {
    JSONObject obj = new JSONObject();
    obj.put("source", answer.source);
    // add chromosome attribute to json answer
    obj.put("chromosome", answer.chromosome);
    JSONObject from_to = new JSONObject();
    from_to.put("from", answer.position[0]);
    from_to.put("to", answer.position[1]);
    obj.put("position", from_to);
    String response;
    if (answer.hasDetail) {
      JSONObject details = new JSONObject();
      details.put("refseq", answer.refseq);
      // create json_array for found mutations
      JSONArray mutations_array = new JSONArray();
      for (IntervalST.Mutation x : answer.mutations) {
        // json object for single mutation, that was found
        JSONObject mut = new JSONObject();

        // name of mutation, need to convert from byte array to string
        mut.put("name", new String(x.b_RefName));

        // json object for position of mutation's interval, with "to" and "from" as attributes
        JSONObject mut_pos = new JSONObject();
        mut_pos.put("from", x.i_Low);
        mut_pos.put("to", x.i_High);
        mut.put("position", mut_pos);

        // json object for metadata
        JSONObject meta = new JSONObject();

        // json object for gender
        JSONObject gender = new JSONObject();
        // -1 for no filter on gender, 0 for male and 1 for female
        if (x.s_Gender == -1) {
          gender.put("m", true);
          gender.put("w", true);
        } else if (x.s_Gender == 0) {
          gender.put("m", true);
          gender.put("w", false);
        } else if (x.s_Gender == 1) {
          gender.put("m", false);
          gender.put("w", true);
        } else {
          gender = null;
        }
        meta.put("gender", gender);

        // add country
        meta.put("origin", IndexController.getCountry(x.s_Country));

        // downloadtime
        meta.put("downloadtime", x.s_Date);

        // add metadata json object to mutation json object
        mut.put("metadata", meta);

        // add sequence of basis pairs, first need to convert integer to string
        if (answer.chromosome == 23) {
          mut.put("mutationseq", Chromosome_reader.retrieve_sequence(x.i_Low, x.i_High, "X"));
        } else if (answer.chromosome == 24) {
          mut.put("mutationseq", Chromosome_reader.retrieve_sequence(x.i_Low, x.i_High, "Y"));
        } else if (answer.chromosome == 25) {
          mut.put("mutationseq", Chromosome_reader.retrieve_sequence(x.i_Low, x.i_High, "MT"));
        } else {
          mut.put(
              "mutationseq",
              Chromosome_reader.retrieve_sequence(x.i_Low, x.i_High, answer.chromosome.toString()));
        }
        // add json object for mutation x to json array for all mutations that were found
        mutations_array.add(mut);
      }
      details.put("mutations", mutations_array);
      obj.put("details", details);
      StringWriter out = new StringWriter();
      try {
        obj.writeJSONString(out);
      } catch (IOException e) {
        e.printStackTrace();
      }
      response = out.toString();

    } else {
      obj.put("details", null);
      // function to count occurrences of mutations in subintervals
      Integer[] counts = count_mutations(answer);
      JSONArray mutation_counts = new JSONArray();
      for (int i = 0; i < counts.length; i++) {
        mutation_counts.add(counts[i]);
      }
      obj.put("graph", mutation_counts);
      StringWriter out = new StringWriter();
      try {
        obj.writeJSONString(out);
      } catch (IOException e) {
        e.printStackTrace();
      }
      response = out.toString();
    }
    if (response != null) {
      return response;
    } else {
      return "Something went wrong while creating answer";
    }
  }