Beispiel #1
0
int
wells_mask_view_main(int argc, char *argv[])
{
  FILE *fp_in;
  int32_t i;
  int c;
  int32_t min_row, max_row, min_col, max_col;
  wells_mask_t *mask = NULL;

  min_row = max_row = min_col = max_col = -1;

  while((c = getopt(argc, argv, "r:c:h")) >= 0) {
      switch(c) {
        case 'r':
          if(ion_parse_range(optarg, &min_row, &max_row) < 0) {
              ion_error(__func__, "-r : format not recognized", Exit, OutOfRange);
          }
          break;
        case 'c':
          if(ion_parse_range(optarg, &min_col, &max_col) < 0) {
              ion_error(__func__, "-c : format not recognized", Exit, OutOfRange);
          }
          break;
        case 'h':
        default:
          return usage();
      }
  }

  if(argc == optind) {
      return usage();
  }
  else {
      for(i=optind;i<argc;i++) {
          if(!(fp_in = fopen(argv[i], "rb"))) {
              fprintf(stderr, "** Could not open %s for reading. **\n", argv[i]);
              ion_error(__func__, argv[i], Exit, OpenFileError);
          }
          if(NULL == (mask = wells_mask_read(fp_in))) {
              ion_error(__func__, argv[i], Exit, ReadFileError);
          }
          fclose(fp_in);
          fp_in = NULL;

          wells_mask_print(stdout, mask);

          wells_mask_destroy(mask);
      }
  }

  return 0;
}
Beispiel #2
0
Datei: sff.c Projekt: golharam/TS
int
sff_view_main(int argc, char *argv[])
{
  int i, c;
  sff_file_t *sff_file_in=NULL, *sff_file_out=NULL;
  sff_iter_t *sff_iter = NULL;
  sff_t *sff = NULL;
  char *fn_names = NULL;
  char **names = NULL;
  int32_t names_num = 0, names_mem = 0;
  int32_t out_mode, min_row, max_row, min_col, max_col;

  out_mode = 0;
  min_row = max_row = min_col = max_col = -1;

  while((c = getopt(argc, argv, "r:c:R:bqh")) >= 0) {
      switch(c) {
        case 'r':
          if(ion_parse_range(optarg, &min_row, &max_row) < 0) {
              ion_error(__func__, "-r : format not recognized", Exit, OutOfRange);
          }
          break;
        case 'c':
          if(ion_parse_range(optarg, &min_col, &max_col) < 0) {
              ion_error(__func__, "-c : format not recognized", Exit, OutOfRange);
          }
          break;
        case 'R':
          free(fn_names);
          fn_names = strdup(optarg); break;
        case 'q':
          out_mode |= 1;
          break;
        case 'b':
          out_mode |= 2;
          break;
        case 'h': 
        default: 
          return usage();
      }
  }
  if(argc != 1+optind) {
      return usage();
  }
  else {
      sff_header_t *header = NULL;
      if(3 == out_mode) {
          ion_error(__func__, "options -b and -q cannot be used together", Exit, CommandLineArgument);
      }

      // open the input SFF
      if(-1 != min_row || -1 != max_row || -1 != min_col || -1 != max_col) {
          sff_file_in = sff_fopen(argv[optind], "rbi", NULL, NULL);
      }
      else {
          sff_file_in = sff_fopen(argv[optind], "rb", NULL, NULL);
      }

      header = sff_header_clone(sff_file_in->header); /* copy header, but update n_reads if using index or names */

      // read in the names
      if(NULL != fn_names) {
          FILE *fp = NULL;
          char name[1024]="\0"; // lets hope we don't exceed this length
          names_num = names_mem = 0;
          names = NULL;
          if(!(fp = fopen(fn_names, "rb"))) {
              fprintf(stderr, "** Could not open %s for reading. **\n", fn_names);
              ion_error(__func__, fn_names, Exit, OpenFileError);
          }
          while(EOF != fscanf(fp, "%s", name)) {
              while(names_num == names_mem) {
                  if(0 == names_mem) names_mem = 4;
                  else names_mem *= 2;
                  names = ion_realloc(names, sizeof(char*) * names_mem, __func__, "names");
              }
              names[names_num] = strdup(name);
              if(NULL == names[names_num]) {
                  ion_error(__func__, name, Exit, MallocMemory);
              }
              names_num++;
          }
          names = ion_realloc(names, sizeof(char*) * names_num, __func__, "names");
          fclose(fp);
          header->n_reads = names_num;
      }
      else {
	// if using index, then iterate once through the index to count the entries
	// so we can set the count correctly in the header
	if (-1 != min_row || -1 != max_row || -1 != min_col || -1 != max_col) {
	  int entries = 0;
          sff_iter = sff_iter_query(sff_file_in, min_row, max_row, min_col, max_col);
	  while (NULL != (sff = sff_iter_read(sff_file_in, sff_iter)))
	    entries++;
	  header->n_reads = entries;
	  /* reset sff_iter */
	  sff_iter_destroy(sff_iter);
	  sff_iter = sff_iter_query(sff_file_in, min_row, max_row, min_col, max_col);
	}
      }

      // print the header
      switch(out_mode) {
        case 0:
          sff_header_print(stdout, header);
          break;
        case 1:
          // do nothing: FASTQ
          break;
        case 2:
          sff_file_out = sff_fdopen(fileno(stdout), "wb", header, NULL);
          break;
      }


      while(1) {
          int32_t to_print = 1;
          if(-1 != min_row || -1 != max_row || -1 != min_col || -1 != max_col) {
              if(NULL == (sff = sff_iter_read(sff_file_in, sff_iter))) {
                  break;
              }
          }
          else {
              if(NULL == (sff = sff_read(sff_file_in))) {
                  break;
              }
          }
          if(0 < names_mem) {
              to_print = 0;
              for(i=0;i<names_num;i++) {
                  if(0 == strcmp(names[i], sff_name(sff))) {
                      to_print = 1;
                      break;
                  }
              }
              // shift down
              if(1 == to_print) { // i < names_num
                  free(names[i]);
                  names[i] = NULL;
                  for(;i<names_num-1;i++) {
                      names[i] = names[i+1];
                      names[i+1] = NULL;
                  }
                  names_num--;
              }
          }
          if(1 == to_print) {
              switch(out_mode) {
                case 0:
                  sff_print(stdout, sff);
                  break;
                case 1:
                  if(fprintf(stdout, "@%s\n%s\n+\n",
                             sff->rheader->name->s,
                             sff->read->bases->s + sff->gheader->key_length) < 0) {
                      ion_error(__func__, "stdout", Exit, WriteFileError);
                  }
                  for(i=sff->gheader->key_length;i<sff->read->quality->l;i++) {
                      if(fputc(QUAL2CHAR(sff->read->quality->s[i]), stdout) < 0) {
                          ion_error(__func__, "stdout", Exit, WriteFileError);
                      }
                  }
                  if(fputc('\n', stdout) < 0) {
                      ion_error(__func__, "stdout", Exit, WriteFileError);
                  }
                  break;
                case 2:
                  sff_write(sff_file_out, sff);
                  break;
              }
          }
          sff_destroy(sff);
      }

      sff_fclose(sff_file_in);
      if(2 == out_mode) {
          sff_fclose(sff_file_out);
      }
      if(-1 != min_row || -1 != max_row || -1 != min_col || -1 != max_col) {
          sff_iter_destroy(sff_iter);
      }

      if(0 != names_num) {
          fprintf(stderr, "** Did not find all the reads with (-R). **\n");
          ion_error(__func__, fn_names, Exit, OutOfRange);
      }

      sff_header_destroy(header);

  }
  if(NULL != names && 0 < names_num) {
      free(names);
  }
  free(fn_names);
  return 0;
}
Beispiel #3
0
int
wells_combine_main(int argc, char *argv[])
{
  FILE **fps_in = NULL;
  wells_chip_t **chips = NULL;
  int c;
  int32_t i, j, k, l, n;
  int32_t nonzero, min_row, max_row, min_col, max_col;

  min_row = max_row = min_col = max_col = -1;
  nonzero=0; 

  while((c = getopt(argc, argv, "r:c:zh")) >= 0) {
      switch(c) {
        case 'r':
          if(ion_parse_range(optarg, &min_row, &max_row) < 0) {
              ion_error(__func__, "-r : format not recognized", Exit, OutOfRange);
          }
          break;
        case 'c':
          if(ion_parse_range(optarg, &min_col, &max_col) < 0) {
              ion_error(__func__, "-c : format not recognized", Exit, OutOfRange);
          }
          break;
        case 'z':
          nonzero = 1;
          break;
        case 'h':
        default:
          return usage();
      }
  }

  if(argc - optind < 2) {
      return usage();
  }
  else {
      n = argc - optind;

      // open the files
      fps_in = ion_calloc(n, sizeof(FILE*), __func__, "fps_in");
      for(i=optind;i<argc;i++) {
          if(!(fps_in[i-optind] = fopen(argv[i], "rb"))) {
              fprintf(stderr, "** Could not open %s for reading. **\n", argv[i]);
              ion_error(__func__, argv[i], Exit, OpenFileError);
          }
      }

      // read in the headers
      chips = ion_calloc(n, sizeof(wells_chip_t*), __func__, "chips");
      for(i=0;i<n;i++) {
          // NB: reads in both wells files
          if(NULL == (chips[i] = wells_chip_read1(fps_in[i]))) {
              ion_error(__func__, argv[i+optind], Exit, ReadFileError);
          }
      }

      // check we can combine the data
      for(i=1;i<n;i++) {
          if(chips[i-1]->header->num_wells != chips[i]->header->num_wells) {
              ion_error(__func__, "# of wells did not match", Exit, OutOfRange);
          }
          else if(chips[i-1]->header->num_flows != chips[i]->header->num_flows) {
              ion_error(__func__, "# of flows did not match", Exit, OutOfRange);
          }
          else if(0 != strcmp(chips[i-1]->header->flow_order, chips[i]->header->flow_order)) {
              ion_error(__func__, "flow order did not match", Exit, OutOfRange);
          }
          else if(chips[i-1]->num_rows != chips[i]->num_rows) {
              ion_error(__func__, "# of rows did not match", Exit, OutOfRange);
          }
          else if(chips[i-1]->num_cols != chips[i]->num_cols) {
              ion_error(__func__, "# of columns did not match", Exit, OutOfRange);
          }
      }

      // write the header
      if(0 == wells_header_write(stdout, chips[0]->header)) {
          ion_error(__func__, "Could not write to the output file", Exit, WriteFileError);
      }

      // combine the data
      // NB: modifies the wells for the first chip
      for(k=0;k<chips[0]->num_cols;k++) { // cols
          for(j=0;j<chips[0]->num_rows;j++) { // rows
              wells_data_t data_out;
              // make room for the new flow values
              data_out.flow_values = ion_calloc(chips[0]->header->num_flows, sizeof(float), __func__, "data_out->flow_values");
              for(i=0;i<n;i++) { // chip
                  wells_data_t *data_in = NULL;
                  // read data in
                  if(NULL == (data_in = wells_data_read(fps_in[i], chips[i]->header))) {
                      ion_error(__func__, argv[i+optind], Exit, ReadFileError);
                  }
                  // copy over relevant data
                  if(0 == i) {
                      data_out.x = data_in->x;
                      data_out.y = data_in->y;
                      data_out.rank = data_in->rank;
                  }
                  for(l=0;l<chips[i]->header->num_flows;l++) { // flows
                      // update sum
                      data_out.flow_values[l] += data_in->flow_values[l];
                  }
                  // destroy
                  wells_data_destroy(data_in);
              }
              // update
              for(l=0;l<chips[0]->header->num_flows;l++) { // flows
                  data_out.flow_values[l] /= n;
              }
              // write
              if(0 == wells_data_write(stdout, chips[0]->header, &data_out)) {
                  ion_error(__func__, "Could not write to the output file", Exit, WriteFileError);
              }
              // destroy
              free(data_out.flow_values);
          }
      }

      // close the files
      for(i=0;i<n;i++) {
          fclose(fps_in[i]);
      }

      // free
      free(fps_in);
      free(chips);
  }

  return 0;
}