void CommandLineHandlerBase::parse( const int argc, const char* argv[], SuperLogger& logger) { ParseResults results; impl->m_parser.parse(argc, argv, results); if (impl->m_message_coloring.is_set()) logger.enable_message_coloring(); if (impl->m_message_verbosity.is_set()) set_verbosity(logger, impl->m_message_verbosity.value()); if (impl->m_version.is_set()) print_version_information(logger); if (impl->m_system.is_set()) print_system_information(logger); if (impl->m_help.is_set()) { const string program_name = filesystem::path(argv[0]).filename().string(); print_program_usage(program_name.c_str(), logger); exit(0); } if (impl->m_display_options.is_set()) { LOG_INFO(logger, "recognized options:"); impl->m_parser.print_recognized_options(logger); } results.m_messages.print(logger); if (results.m_errors > 0 || results.m_warnings > 0) { LOG( logger, results.m_errors > 0 ? LogMessage::Fatal : LogMessage::Warning, FMT_SIZE_T " error%s, " FMT_SIZE_T " warning%s encountered while parsing the command line.", results.m_errors, results.m_errors > 1 ? "s" : "", results.m_warnings, results.m_warnings > 1 ? "s" : ""); } }
void CommandLineHandlerBase::parse(const int argc, const char* argv[], SuperLogger& logger) { impl->m_executable_name = bf::path(argv[0]).filename().string(); impl->m_parser.parse(argc, argv, impl->m_parse_results); if (impl->m_help.is_set()) { print_program_usage(impl->m_executable_name.c_str(), logger); exit(EXIT_SUCCESS); } if (impl->m_message_coloring.is_set()) logger.enable_message_coloring(); if (impl->m_message_verbosity.is_set()) logger.set_verbosity_level_from_string(impl->m_message_verbosity.value().c_str()); }
int main (int argc, char *argv[]) { char *filename; char *divstring = NULL, *lasttypestring, *ptr; Univcoord_T univ_coordstart, univ_coordend; Chrpos_T coordstart, coordend, lastcoord = 0U; char Buffer[BUFLEN], nocomment[BUFLEN], query[BUFLEN], typestring[BUFLEN]; int typeint, *types, c; int nargs, ntypes, ndivs; int *value_matches = NULL, *matches = NULL; int n_value_matches = 0, nmatches = 0, i; int *leftflanks, *rightflanks, nleftflanks = 0, nrightflanks = 0; long int total; int n; IIT_T iit = NULL; Univ_IIT_T chromosome_iit = NULL; bool skipp, universalp; int opt; extern int optind; extern char *optarg; const char *long_name; int long_option_index = 0; while ((opt = getopt_long(argc,argv,"a:b:f:LCASUu:c:HRTZN",long_options,&long_option_index)) != -1) { switch (opt) { case 0: long_name = long_options[long_option_index].name; if (!strcmp(long_name,"version")) { print_program_version(); exit(0); } else if (!strcmp(long_name,"help")) { print_program_usage(); exit(0); } else if (!strcmp(long_name,"exact")) { exactp = true; } else { /* Shouldn't reach here */ fprintf(stderr,"Don't recognize option %s. For usage, run 'gsnap --help'",long_name); exit(9); } break; case 'a': lowval = atof(optarg); user_lowvalue_p = true; value_matches_p = true; break; case 'b': highval = atof(optarg); user_highvalue_p = true; value_matches_p = true; break; case 'f': fieldstring = optarg; break; case 'L': force_label_p = true; break; case 'C': force_coords_p = true; break; case 'A': annotationonlyp = true; break; case 'S': sortp = true; break; case 'U': signedp = false; break; case 'u': nflanking = atoi(optarg); break; case 'c': centerp = true; centerlength = atoi(optarg); break; case 'H': centeruc = true; break; case 'R': runlengthp = true; break; case 'T': tallyp = true; break; case 'Z': zeroesp = true; break; case 'N': statsp = true; break; case 'V': print_program_version(); exit(0); case '?': print_program_usage(); exit(0); default: exit(9); } } argc -= (optind - 1); argv += (optind - 1); if (argc <= 1) { fprintf(stderr,"Need to specify an iit file. Type \"iit_get --help\" for help.\n"); exit(9); } else { filename = argv[1]; } if (value_matches_p == true) { if ((iit = IIT_read(filename,/*name*/NULL,true,/*divread*/READ_ALL,/*divstring*/NULL, /*add_iit_p*/true,/*labels_read_p*/true)) == NULL) { if (Access_file_exists_p(filename) == false) { fprintf(stderr,"Cannot read file %s\n",filename); } else { fprintf(stderr,"File %s appears to be an invalid IIT file\n",filename); } exit(9); } else if (fieldstring != NULL) { if ((fieldint = IIT_fieldint(iit,fieldstring)) < 0) { fprintf(stderr,"No field %s defined in iit file.\n",fieldstring); exit(9); } } if (IIT_valuep(iit) == false) { fprintf(stderr,"Error: This IIT file does not have values stored\n"); exit(9); } if (user_lowvalue_p == true && user_highvalue_p == true) { if (lowval > highval) { fprintf(stderr,"Cannot have lowval %f > highval %f\n",lowval,highval); exit(9); } else { value_matches = IIT_get_values_between(&n_value_matches,iit,lowval,highval,/*sortp*/false); } } else if (user_lowvalue_p == true) { value_matches = IIT_get_values_above(&n_value_matches,iit,lowval,/*sortp*/false); } else { /* user_highvalue_p == true */ value_matches = IIT_get_values_below(&n_value_matches,iit,highval,/*sortp*/false); } } if (0 && statsp == true && argc == 2) { /* Want total over entire IIT */ if ((iit = IIT_read(filename,NULL,true,/*divread*/READ_ALL,/*divstring*/NULL,/*add_iit_p*/true, /*labels_read_p*/false)) == NULL) { if (Access_file_exists_p(filename) == false) { fprintf(stderr,"Cannot read file %s\n",filename); } else { fprintf(stderr,"File %s appears to be an invalid IIT file\n",filename); } exit(9); } else if (fieldstring != NULL) { if ((fieldint = IIT_fieldint(iit,fieldstring)) < 0) { fprintf(stderr,"No field %s defined in iit file.\n",fieldstring); exit(9); } } total = 0; n = 0; for (i = 0; i < IIT_total_nintervals(iit); i++) { debug(printf("index = %d\n",matches[i])); compute_totals_tally(&total,&n,/*coordstart*/0,/*coordend*/-1U,i,iit); } printf("counts:%ld non-zero-positions:%u mean-over-nonzero:%.3f\n",total,n,(double) total/(double) n); IIT_free(&iit); return 0; } else if ((universalp = IIT_universalp(filename,/*add_iit_p*/true)) == true) { chromosome_iit = Univ_IIT_read(filename,/*readonlyp*/true,/*add_iit_p*/true); if (argc != 3) { fprintf(stderr,"For chromosome IIT file, need to specify a query on the command line\n"); exit(9); } else { /* Try as 0:<iitfile> 1:<query> */ matches = get_matches_univ(&nmatches,&divstring,&univ_coordstart,&univ_coordend, &leftflanks,&nleftflanks,&rightflanks,&nrightflanks, argv[2],/*typestring*/NULL,&chromosome_iit,filename); for (i = 0; i < nmatches; i++) { debug(printf("\nindex = %d\n",matches[i])); print_interval_univ(univ_coordstart,univ_coordend,matches[i],chromosome_iit); } } if (divstring != NULL) { FREE(divstring); } Univ_IIT_free(&chromosome_iit); return 0; } else if (argc == 2 && value_matches_p == true) { /* Note: Could potentially handle input from stdin, but currently just deal with value_matches */ ndivs = IIT_ndivs(iit); for (i = 0; i < n_value_matches; i++) { debug(printf("\nindex = %d\n",matches[i])); print_interval(&lastcoord,/*total*/0,/*divstring*/NULL,/*coordstart*/0,/*coordend*/0, value_matches[i],iit,ndivs,fieldint); } FREE(value_matches); IIT_free(&iit); return 0; } else if (argc == 2) { debug(printf("Running under argc 2\n")); /* Expecting input from stdin */ if ((iit = IIT_read(filename,NULL,true,/*divread*/READ_ALL,/*divstring*/NULL,/*add_iit_p*/true, /*labels_read_p*/true)) == NULL) { if (Access_file_exists_p(filename) == false) { fprintf(stderr,"Cannot read file %s\n",filename); } else { fprintf(stderr,"File %s appears to be an invalid IIT file\n",filename); } exit(9); } else if (fieldstring != NULL) { if ((fieldint = IIT_fieldint(iit,fieldstring)) < 0) { fprintf(stderr,"No field %s defined in iit file.\n",fieldstring); exit(9); } } while (fgets(Buffer,BUFLEN,stdin) != NULL) { if ((ptr = rindex(Buffer,'\n')) != NULL) { *ptr = '\0'; } strcpy(nocomment,Buffer); if ((ptr = rindex(nocomment,'#')) != NULL) { *ptr = '\0'; } skipp = false; if ((nargs = sscanf(nocomment,"%s %s",query,typestring)) == 2) { debug(printf("typestring is %s\n",typestring)); matches = get_matches(&nmatches,&divstring,&univ_coordstart,&univ_coordend, &leftflanks,&nleftflanks,&rightflanks,&nrightflanks, query,typestring,&iit,filename); coordstart = (Chrpos_T) univ_coordstart; coordend = (Chrpos_T) univ_coordend; } else if (nargs == 1) { debug(printf("typestring is NULL\n")); matches = get_matches(&nmatches,&divstring,&univ_coordstart,&univ_coordend, &leftflanks,&nleftflanks,&rightflanks,&nrightflanks, query,/*typestring*/NULL,&iit,filename); coordstart = (Chrpos_T) univ_coordstart; coordend = (Chrpos_T) univ_coordend; } else { fprintf(stderr,"Can't parse line %s. Ignoring.\n",nocomment); skipp = true; } total = 0; if (skipp == false) { fprintf(stdout,"# Query: %s\n",Buffer); ndivs = IIT_ndivs(iit); if (nflanking > 0) { for (i = nleftflanks-1; i >= 0; i--) { debug(printf("\nleft index = %d\n",leftflanks[i])); print_interval(&lastcoord,/*total*/0,divstring,coordstart,coordend,leftflanks[i],iit,ndivs,fieldint); } printf("====================\n"); FREE(leftflanks); } lastcoord = coordstart; for (i = 0; i < nmatches; i++) { debug(printf("\nindex = %d\n",matches[i])); total = print_interval(&lastcoord,total,divstring,coordstart,coordend,matches[i],iit,ndivs,fieldint); } if (nflanking > 0) { printf("====================\n"); for (i = 0; i < nrightflanks; i++) { debug(printf("\nright index = %d\n",rightflanks[i])); print_interval(&lastcoord,/*total*/0,divstring,coordstart,coordend,rightflanks[i],iit,ndivs,fieldint); } FREE(rightflanks); } if (zeroesp == true) { while (lastcoord <= coordend) { printf("%s\t%u\t%d\n",divstring,lastcoord,0); lastcoord++; } } } if (divstring != NULL) { FREE(divstring); } FREE(matches); printf("%ld\n",total); fprintf(stdout,"# End\n"); fflush(stdout); } IIT_free(&iit); return 0; } else { /* Get coordinates/type from command line */ if (argc == 3) { /* Try as 0:<iitfile> 1:<query> */ matches = get_matches(&nmatches,&divstring,&univ_coordstart,&univ_coordend, &leftflanks,&nleftflanks,&rightflanks,&nrightflanks, argv[2],/*typestring*/NULL,&iit,filename); coordstart = (Chrpos_T) univ_coordstart; coordend = (Chrpos_T) univ_coordend; } else if (argc == 4) { /* Try as 0:<iitfile> 1:<query> 2:<type> */ debug(printf("Running under argc 4\n")); matches = get_matches(&nmatches,&divstring,&univ_coordstart,&univ_coordend, &leftflanks,&nleftflanks,&rightflanks,&nrightflanks, argv[2],argv[3],&iit,filename); coordstart = (Chrpos_T) univ_coordstart; coordend = (Chrpos_T) univ_coordend; } else { types = (int *) CALLOC(argc-3,sizeof(int)); for (c = 3, ntypes = 0; c < argc; c++) { if ((typeint = IIT_typeint(iit,argv[c])) < 0) { fprintf(stderr,"No such type as %s. Ignoring the type.\n",argv[c]); } else { types[ntypes++] = typeint; lasttypestring = argv[c]; } } if (ntypes == 0) { matches = get_matches(&nmatches,&divstring,&univ_coordstart,&univ_coordend, &leftflanks,&nleftflanks,&rightflanks,&nrightflanks, argv[2],/*typestring*/NULL,&iit,filename); coordstart = (Chrpos_T) univ_coordstart; coordend = (Chrpos_T) univ_coordend; } else if (ntypes == 1) { matches = get_matches(&nmatches,&divstring,&univ_coordstart,&univ_coordend, &leftflanks,&nleftflanks,&rightflanks,&nrightflanks, argv[2],lasttypestring,&iit,filename); coordstart = (Chrpos_T) univ_coordstart; coordend = (Chrpos_T) univ_coordend; } else { matches = get_matches_multiple_typed(&nmatches,&divstring,&univ_coordstart,&univ_coordend, &leftflanks,&nleftflanks,&rightflanks,&nrightflanks, argv[2],types,ntypes,&iit,filename); coordstart = (Chrpos_T) univ_coordstart; coordend = (Chrpos_T) univ_coordend; } } if (value_matches_p == true) { matches = match_intersection(&nmatches,/*matches1*/matches,/*nmatches1*/nmatches, /*matches2*/value_matches,/*nmatches2*/n_value_matches); FREE(value_matches); } #if 0 if (centerp == true) { print_spaces(centerlength); printf("*"); print_spaces(centerlength-1); printf("\n"); } #endif if (statsp == true) { total = 0; n = 0; for (i = 0; i < nmatches; i++) { debug(printf("index = %d\n",matches[i])); compute_totals_tally(&total,&n,coordstart,coordend,matches[i],iit); } n = coordend - coordstart + 1; printf("counts:%ld width:%u mean:%.3f\n",total,n,(double)total/(double) n); #if 0 } else if (geomeanp == true) { logtotal = 0.0; total = 0; n = 0; for (i = 0; i < nmatches; i++) { debug(printf("index = %d\n",matches[i])); logtotal = compute_logtotal_tally(&total,&n,coordstart,coordend,matches[i],iit); } printf("geomean:%f totalcounts:%ld posrange:%d\n", exp(logtotal/(double) (coordend - coordstart + 1)) - 1.0,total,n); #endif } else { ndivs = IIT_ndivs(iit); if (nflanking > 0) { for (i = nleftflanks-1; i >= 0; i--) { debug(printf("\nleft index = %d\n",leftflanks[i])); print_interval(&lastcoord,/*total*/0,divstring,coordstart,coordend,leftflanks[i],iit,ndivs,fieldint); } printf("====================\n"); FREE(leftflanks); } lastcoord = coordstart; for (i = 0; i < nmatches; i++) { debug(printf("\nindex = %d\n",matches[i])); print_interval(&lastcoord,/*total*/0,divstring,coordstart,coordend,matches[i],iit,ndivs,fieldint); } if (nflanking > 0) { printf("====================\n"); for (i = 0; i < nrightflanks; i++) { debug(printf("\nright index = %d\n",rightflanks[i])); print_interval(&lastcoord,/*total*/0,divstring,coordstart,coordend,rightflanks[i],iit,ndivs,fieldint); } FREE(rightflanks); } } if (divstring != NULL) { FREE(divstring); } FREE(matches); IIT_free(&iit); return 0; } }