| 1 |
#include "samtools.pysam.h" |
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
|
| 19 |
|
| 20 |
|
| 21 |
|
| 22 |
|
| 23 |
|
| 24 |
|
| 25 |
|
| 26 |
|
| 27 |
#include <config.h> |
| 28 |
|
| 29 |
#include <stdio.h> |
| 30 |
#include <stdlib.h> |
| 31 |
#include <string.h> |
| 32 |
#include "sam_opts.h" |
| 33 |
|
| 34 |
|
| 35 |
|
| 36 |
|
| 37 |
|
| 38 |
|
| 39 |
|
| 40 |
|
| 41 |
|
| 42 |
|
| 43 |
|
| 44 |
|
| 45 |
|
| 46 |
|
| 47 |
int parse_sam_global_opt(int c, const char *optarg, const struct option *lopt, |
| 48 |
sam_global_args *ga) { |
| 49 |
int r = 0; |
| 50 |
|
| 51 |
while (lopt->name) { |
| 52 |
if (c != lopt->val) { |
| 53 |
lopt++; |
| 54 |
continue; |
| 55 |
} |
| 56 |
|
| 57 |
if (strcmp(lopt->name, "input-fmt") == 0) { |
| 58 |
r = hts_parse_format(&ga->in, optarg); |
| 59 |
break; |
| 60 |
} else if (strcmp(lopt->name, "input-fmt-option") == 0) { |
| 61 |
r = hts_opt_add((hts_opt **)&ga->in.specific, optarg); |
| 62 |
break; |
| 63 |
} else if (strcmp(lopt->name, "output-fmt") == 0) { |
| 64 |
r = hts_parse_format(&ga->out, optarg); |
| 65 |
break; |
| 66 |
} else if (strcmp(lopt->name, "output-fmt-option") == 0) { |
| 67 |
r = hts_opt_add((hts_opt **)&ga->out.specific, optarg); |
| 68 |
break; |
| 69 |
} else if (strcmp(lopt->name, "reference") == 0) { |
| 70 |
char *ref = malloc(10 + strlen(optarg) + 1); |
| 71 |
|
| 72 |
if (!ref) { |
| 73 |
fprintf(samtools_stderr, "Unable to allocate memory in " |
| 74 |
"parse_sam_global_opt.\n"); |
| 75 |
|
| 76 |
return -1; |
| 77 |
} |
| 78 |
|
| 79 |
sprintf(ref, "reference=%s", optarg); |
| 80 |
|
| 81 |
if (!(ga->reference = strdup(optarg))) { |
| 82 |
fprintf(samtools_stderr, "Unable to allocate memory in " |
| 83 |
"parse_sam_global_opt.\n"); |
| 84 |
|
| 85 |
return -1; |
| 86 |
} |
| 87 |
|
| 88 |
r = hts_opt_add((hts_opt **)&ga->in.specific, ref); |
| 89 |
r |= hts_opt_add((hts_opt **)&ga->out.specific, ref); |
| 90 |
free(ref); |
| 91 |
break; |
| 92 |
} else if (strcmp(lopt->name, "threads") == 0) { |
| 93 |
ga->nthreads = atoi(optarg); |
| 94 |
break; |
| 95 |
} else if (strcmp(lopt->name, "write-index") == 0) { |
| 96 |
ga->write_index = 1; |
| 97 |
break; |
| 98 |
} else if (strcmp(lopt->name, "verbosity") == 0) { |
| 99 |
hts_verbose = atoi(optarg); |
| 100 |
break; |
| 101 |
} |
| 102 |
} |
| 103 |
|
| 104 |
if (!lopt->name) { |
| 105 |
fprintf(samtools_stderr, "Unexpected global option.\n"); |
| 106 |
return -1; |
| 107 |
} |
| 108 |
|
| 109 |
|
| 110 |
|
| 111 |
|
| 112 |
if (ga->out.format == sam) { |
| 113 |
hts_opt *opts = (hts_opt *)ga->out.specific; |
| 114 |
while (opts) { |
| 115 |
if (opts->opt == HTS_OPT_COMPRESSION_LEVEL) |
| 116 |
ga->out.compression = bgzf; |
| 117 |
opts = opts->next; |
| 118 |
} |
| 119 |
} |
| 120 |
|
| 121 |
return r; |
| 122 |
} |
| 123 |
|
| 124 |
|
| 125 |
|
| 126 |
|
| 127 |
|
| 128 |
|
| 129 |
|
| 130 |
|
| 131 |
|
| 132 |
|
| 133 |
|
| 134 |
void sam_global_opt_help(FILE *fp, const char *shortopts) { |
| 135 |
int i = 0; |
| 136 |
|
| 137 |
static const struct option lopts[] = { |
| 138 |
SAM_OPT_GLOBAL_OPTIONS(0,0,0,0,0,0), |
| 139 |
{ NULL, 0, NULL, 0 } |
| 140 |
}; |
| 141 |
|
| 142 |
for (i = 0; shortopts && shortopts[i] && lopts[i].name; i++) { |
| 143 |
if (shortopts[i] == '-') |
| 144 |
continue; |
| 145 |
|
| 146 |
if (shortopts[i] == '.') |
| 147 |
fprintf(fp, " --"); |
| 148 |
else |
| 149 |
fprintf(fp, " -%c, --", shortopts[i]); |
| 150 |
|
| 151 |
if (strcmp(lopts[i].name, "input-fmt") == 0) |
| 152 |
fprintf(fp,"input-fmt FORMAT[,OPT[=VAL]]...\n" |
| 153 |
" Specify input format (SAM, BAM, CRAM)\n"); |
| 154 |
else if (strcmp(lopts[i].name, "input-fmt-option") == 0) |
| 155 |
fprintf(fp,"input-fmt-option OPT[=VAL]\n" |
| 156 |
" Specify a single input file format option in the form\n" |
| 157 |
" of OPTION or OPTION=VALUE\n"); |
| 158 |
else if (strcmp(lopts[i].name, "output-fmt") == 0) |
| 159 |
fprintf(fp,"output-fmt FORMAT[,OPT[=VAL]]...\n" |
| 160 |
" Specify output format (SAM, BAM, CRAM)\n"); |
| 161 |
else if (strcmp(lopts[i].name, "output-fmt-option") == 0) |
| 162 |
fprintf(fp,"output-fmt-option OPT[=VAL]\n" |
| 163 |
" Specify a single output file format option in the form\n" |
| 164 |
" of OPTION or OPTION=VALUE\n"); |
| 165 |
else if (strcmp(lopts[i].name, "reference") == 0) |
| 166 |
fprintf(fp,"reference FILE\n" |
| 167 |
" Reference sequence FASTA FILE [null]\n"); |
| 168 |
else if (strcmp(lopts[i].name, "threads") == 0) |
| 169 |
fprintf(fp,"threads INT\n" |
| 170 |
" Number of additional threads to use [0]\n"); |
| 171 |
else if (strcmp(lopts[i].name, "write-index") == 0) |
| 172 |
fprintf(fp,"write-index\n" |
| 173 |
" Automatically index the output files [off]\n"); |
| 174 |
else if (strcmp(lopts[i].name, "verbosity") == 0) |
| 175 |
fprintf(fp,"verbosity INT\n" |
| 176 |
" Set level of verbosity\n"); |
| 177 |
} |
| 178 |
} |
| 179 |
|
| 180 |
void sam_global_args_init(sam_global_args *ga) { |
| 181 |
if (!ga) |
| 182 |
return; |
| 183 |
|
| 184 |
memset(ga, 0, sizeof(*ga)); |
| 185 |
} |
| 186 |
|
| 187 |
void sam_global_args_free(sam_global_args *ga) { |
| 188 |
if (ga->in.specific) |
| 189 |
hts_opt_free(ga->in.specific); |
| 190 |
|
| 191 |
if (ga->out.specific) |
| 192 |
hts_opt_free(ga->out.specific); |
| 193 |
|
| 194 |
if (ga->reference) |
| 195 |
free(ga->reference); |
| 196 |
} |