From 6f558c8da992cde99d221d428ab503a960ae93ca Mon Sep 17 00:00:00 2001 From: Georgi Tushev Date: Wed, 7 Sep 2016 22:12:20 +0200 Subject: [PATCH] add user configuration --- config.c | 171 +++++++++++++++++++++++++++++++++++++++++++++++++++++++ config.h | 47 +++++++++++++++ 2 files changed, 218 insertions(+) create mode 100644 config.c create mode 100644 config.h diff --git a/config.c b/config.c new file mode 100644 index 0000000..c7499a2 --- /dev/null +++ b/config.c @@ -0,0 +1,171 @@ +// +// config.c +// PASSFinder +// +// Created by Georgi Tushev on 07/09/16. +// Copyright © 2016 Scientific Computing. All rights reserved. +// + +#include "config.h" + +const char VERSION[] = "v0.01"; +const char PROGRAM_NAME[] = "FastPASSFinder"; + +int parseSettings(int argc, char *argv[], Settings *config) +{ + // initialize default values + config->input = NULL; + config->maskpoly = NULL; + config->output = NULL; + config->window = DEFAULT_WINDOW; + config->depth = DEFAULT_DEPTH; + config->sizepoly = DEFAULT_SIZEPOLY; + + if(argc < 5) + { + fprintf(stderr,"Error:: provide input and output streams!\n"); + return EXIT_FAILURE; + } + + // do some parsing + for(int i = 1; i < argc; i++) + { + // current paramter length + uint64_t parameterLength = strlen(argv[i]); + + // check each parameter and its value + if((PARAMETER_CHECK("-h", 2, parameterLength)) || + (PARAMETER_CHECK("--help", 6, parameterLength))) + { + help(); + } + else if((PARAMETER_CHECK("-v", 2, parameterLength)) || + (PARAMETER_CHECK("--version", 9, parameterLength))) + { + version(); + } + else if((PARAMETER_CHECK("-w", 2, parameterLength)) || (PARAMETER_CHECK("--window", 8, parameterLength))) + { + i += 1; + config->window = strtol(argv[i], NULL, 10); + + if (config->window <= 0) + { + fprintf(stderr, "Error:: window size should be >0!\n"); + return EXIT_FAILURE; + } + } + else if((PARAMETER_CHECK("-d", 2, parameterLength)) || + (PARAMETER_CHECK("--depth", 7, parameterLength))) + { + i += 1; + config->depth = strtol(argv[i], NULL, 10); + + if(config->depth <= 0) + { + fprintf(stderr,"Error:: base depth should be >0!\n"); + return EXIT_FAILURE; + } + + } + else if((PARAMETER_CHECK("-p", 2, parameterLength)) || + (PARAMETER_CHECK("--polysize", 10, parameterLength))) + { + i += 1; + config->sizepoly = strtol(argv[i], NULL, 10); + + if(config->sizepoly <= 0) + { + fprintf(stderr,"Error:: read poly tail/head size should be >0!\n"); + return EXIT_FAILURE; + } + + } + else if((PARAMETER_CHECK("-i", 2, parameterLength)) || + (PARAMETER_CHECK("--input", 7, parameterLength))) + { + i += 1; + + config->input = argv[i]; + + if (config->input == NULL) + { + fprintf(stderr,"Error:: could not open input stream!\n"); + return EXIT_FAILURE; + } + } + else if((PARAMETER_CHECK("-m", 2, parameterLength)) || + (PARAMETER_CHECK("--maskpoly", 10, parameterLength))) + { + i += 1; + + config->maskpoly = argv[i]; + + if (config->maskpoly == NULL) + { + fprintf(stderr, "Error:: could not open genome poly mask file!\n"); + return EXIT_FAILURE; + } + + } + else if((PARAMETER_CHECK("-o", 2, parameterLength)) || + (PARAMETER_CHECK("--output", 8, parameterLength))) + { + i += 1; + + config->output = argv[i]; + + if (config->output == NULL) + { + fprintf(stderr, "Error:: could not open file output stream!\n"); + return EXIT_FAILURE; + } + + } + else + { + fprintf(stderr, "Error:: Unknown parameter %s\n", argv[i]); + return EXIT_FAILURE; + } + + + } + + return 0; +} + +int version(void) +{ + fprintf(stderr, "FastPASSFinder %s\n", VERSION); + return 0; +} + +int help(void) +{ + + fprintf(stderr, "\nTool: FastPASSFinder\n"); + fprintf(stderr, "Version: %s\n", VERSION); + fprintf(stderr, "Summary:\n"); + fprintf(stderr, "\tCluster PolyA supported sites from 3'End sequencing.\n"); + fprintf(stderr, "\nUsage:\t%s [OPTIONS] -i/--input -m/--maskpoly -o/--output \n", PROGRAM_NAME); + fprintf(stderr, "Options:\n"); + fprintf(stderr, "\t-i/--input BAM/stdin file\n"); + fprintf(stderr, "\t-o/--output BED/stdout file\n"); + fprintf(stderr, "\t-m/--maskpoly BED/stdin file with genomic poly regions\n"); + fprintf(stderr, "\t-w/--window\tsliding window size\n"); + fprintf(stderr, "\t-d/--depth\tminimum number of polyA reads to define PASS\n"); + fprintf(stderr, "\t-p/--polysize\tminimum number of As for tail and Ts for head to define poly stretch\n"); + fprintf(stderr, "\t-h/--help\tprint help message\n"); + fprintf(stderr, "\t-v/--version\tprint current version\n"); + return 1; +} + +int chompNewLine(char *line_buf) +{ + size_t line_length; + line_length = strlen(line_buf) - 1; + if(line_buf[line_length] == '\n') + line_buf[line_length] = '\0'; + + return 0; +} \ No newline at end of file diff --git a/config.h b/config.h new file mode 100644 index 0000000..d0c0b8c --- /dev/null +++ b/config.h @@ -0,0 +1,47 @@ +// +// config.h +// PASSFinder +// +// Created by Georgi Tushev on 07/09/16. +// Copyright © 2016 Scientific Computing. All rights reserved. +// + +#ifndef config_h +#define config_h + +#include +#include +#include + +// define some constants +#define MAX_LENGTH 1024 +#define DEFAULT_WINDOW 200 +#define DEFAULT_DEPTH 5 +#define DEFAULT_SIZEPOLY 3 + +// define MIN & MAX macros +#define MIN(a,b) (((a)<(b))?(a):(b)) +#define MAX(a,b) (((a)>(b))?(a):(b)) + +// define our parameter checking macro +#define PARAMETER_CHECK(param, paramLen, actualLen) (strncmp(argv[i], param, MIN(actualLen, paramLen))== 0) && (actualLen == paramLen) + + +// define parameters +typedef struct _Settings +{ + char *input; + char *maskpoly; + char *output; + int64_t window; + int64_t depth; + int64_t sizepoly; +} Settings; + +// function declarations +int chompNewLine(char *); +int parseSettings(int, char **, Settings *); +int help(void); +int version(void); + +#endif /* config_h */