示例#1
0
void vcf_hdrtxt_append_commands(cJSON *command, StrBuf *hdr, const char *path)
{
  bool first;
  for(; command != NULL; command = command->next)
  {
    cJSON *key  = json_hdr_get(command, "key",    cJSON_String,  path);
    cJSON *cmd  = json_hdr_get(command, "cmd",    cJSON_Array,   path);
    cJSON *cwd  = json_hdr_get(command, "cwd",    cJSON_String,  path);
    cJSON *prev = json_hdr_get(command, "prev",   cJSON_Array,   path);
    cJSON *ver  = json_hdr_try(command, "mccortex",cJSON_String, path);

    prev = prev->child; // result could be NULL
    if(prev && prev->type != cJSON_String) die("Invalid 'prev' field");
    strbuf_append_str(hdr, "##mccortex_");
    strbuf_append_str(hdr, key->valuestring);
    strbuf_append_str(hdr, "=<prev=\"");
    strbuf_append_str(hdr, prev ? prev->valuestring : "NULL");

    if(prev) {
      while((prev = prev->next) != NULL) {
        strbuf_append_str(hdr, ";");
        strbuf_append_str(hdr, prev->valuestring);
      }
    }
    strbuf_append_str(hdr, "\",cmd=\"");
    for(first = true, cmd = cmd->child; cmd; cmd = cmd->next, first = false) {
      if(!first) strbuf_append_char(hdr, ' ');
      strbuf_append_str(hdr, cmd->valuestring);
    }
    strbuf_append_str(hdr, "\",cwd=\"");
    strbuf_append_str(hdr, cwd->valuestring);
    strbuf_append_str(hdr, "\"");
    if(ver) {
      strbuf_append_str(hdr, ",version=\"");
      strbuf_append_str(hdr, ver->valuestring);
      strbuf_append_str(hdr, "\"");
    }
    strbuf_append_str(hdr, ">\n");
  }
}
示例#2
0
static void print_vcf_header(cJSON *json, bool is_breakpoint, FILE *fout)
{
  ctx_assert(json != NULL);

  char datestr[9];
  time_t date = time(NULL);
  strftime(datestr, 9, "%Y%m%d", localtime(&date));

  fprintf(fout, "##fileformat=VCFv4.1\n##fileDate=%s\n", datestr);

  // Print commands used to generate header
  cJSON *commands = json_hdr_get(json, "commands", cJSON_Array, input_path);
  cJSON *command = commands->child;

  // Print this command
  char keystr[8];
  char *prevstr = NULL;
  size_t i;

  if(command) {
    cJSON *key = json_hdr_get(command, "key", cJSON_String, input_path);
    prevstr = key->valuestring;
  }

  // Print command entry for this command
  fprintf(fout, "##mccortex_%s=<prev=\"%s\",cmd=\"%s\",cwd=\"%s\",version="CTX_VERSION">\n",
          hex_rand_str(keystr, sizeof(keystr)),
          prevstr ? prevstr : "NULL",
          cmd_get_cmdline(), cmd_get_cwd());

  // Print previous commands
  for(; command != NULL; command = command->next)
  {
    cJSON *key  = json_hdr_get(command, "key",    cJSON_String,  input_path);
    cJSON *cmd  = json_hdr_get(command, "cmd",    cJSON_Array,   input_path);
    cJSON *cwd  = json_hdr_get(command, "cwd",    cJSON_String,  input_path);
    cJSON *prev = json_hdr_get(command, "prev",   cJSON_Array,   input_path);
    cJSON *ver  = json_hdr_try(command, "mccortex",cJSON_String, input_path);

    prev = prev->child; // result could be NULL
    if(prev && prev->type != cJSON_String) die("Invalid 'prev' field");
    fprintf(fout, "##mccortex_%s=<prev=\"%s", key->valuestring,
                  prev ? prev->valuestring : "NULL");
    if(prev) {
      while((prev = prev->next) != NULL) fprintf(fout, ";%s", prev->valuestring);
    }
    fprintf(fout, "\",cmd=\"");
    for(i = 0, cmd = cmd->child; cmd; cmd = cmd->next, i++) {
      if(i > 0) fputc(' ', fout);
      fputs(cmd->valuestring, fout);
    }
    fprintf(fout, "\",cwd=\"%s\"", cwd->valuestring);
    if(ver) { fprintf(fout, ",version=\"%s\"", ver->valuestring); }
    fprintf(fout, ">\n");
  }

  // Print field definitions
  if(is_breakpoint)
    fprintf(fout, "##INFO=<ID=BRKPNT,Number=1,Type=String,Description=\"Breakpoint call\">\n");
  else
    fprintf(fout, "##INFO=<ID=BUBBLE,Number=1,Type=String,Description=\"Bubble call\">\n");

  fprintf(fout, "##INFO=<ID=K%zu,Number=0,Type=Flag,Description=\"Found at k=%zu\">\n", kmer_size, kmer_size);

  fprintf(fout, "##FORMAT=<ID=GT,Number=1,Type=String,Description=\"Genotype\">\n");
  fprintf(fout, "##FILTER=<ID=PASS,Description=\"All filters passed\">\n");

  // Print reference paths
  fprintf(fout, "##reference=%s", ref_paths[0]);
  for(i = 1; i < num_ref_paths; i++) printf(",%s", ref_paths[i]);
  fprintf(fout, "\n");

  // Print contigs lengths
  for(i = 0; i < chroms.len; i++) {
    fprintf(fout, "##contig=<ID=%s,length=%zu>\n",
            chroms.b[i].name.b, chroms.b[i].seq.end);
  }

  // Print VCF column header
  fputs("#CHROM\tPOS\tID\tREF\tALT\tQUAL\tFILTER\tINFO\tFORMAT", fout);

  if(is_breakpoint)
  {
    // Print a column for each sample
    cJSON *graph_json   = json_hdr_get(json,       "graph",   cJSON_Object, input_path);
    cJSON *colours_json = json_hdr_get(graph_json, "colours", cJSON_Array,  input_path);
    cJSON *colour_json  = colours_json->child;
    if(colour_json == NULL) die("Missing colours");
    for(; colour_json; colour_json = colour_json->next)
    {
      if(!json_hdr_colour_is_ref(colour_json)) {
        cJSON *sample_json = json_hdr_get(colour_json, "sample", cJSON_String, input_path);
        fputc('\t', fout);
        fputs(sample_json->valuestring, fout);
      }
    }
  }

  fputc('\n', fout);
}