| 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 |
#include <htslib/hts.h> |
| 29 |
#include <htslib/sam.h> |
| 30 |
#include <htslib/faidx.h> |
| 31 |
#include <htslib/khash.h> |
| 32 |
#include <htslib/kseq.h> |
| 33 |
#include <samtools.h> |
| 34 |
#include <stdio.h> |
| 35 |
#include <stdlib.h> |
| 36 |
#include <unistd.h> |
| 37 |
#include <getopt.h> |
| 38 |
|
| 39 |
KHASH_MAP_INIT_STR(sm, int) |
| 40 |
|
| 41 |
|
| 42 |
|
| 43 |
|
| 44 |
typedef struct FaidxPath { |
| 45 |
|
| 46 |
char* filename; |
| 47 |
|
| 48 |
faidx_t* faidx; |
| 49 |
struct FaidxPath* next; |
| 50 |
} FaidxPath; |
| 51 |
|
| 52 |
|
| 53 |
typedef struct Params { |
| 54 |
|
| 55 |
FILE* out; |
| 56 |
|
| 57 |
char tag[3]; |
| 58 |
|
| 59 |
FaidxPath* faidx; |
| 60 |
|
| 61 |
int test_index; |
| 62 |
} Params; |
| 63 |
|
| 64 |
|
| 65 |
static void usage_samples(FILE *write_to) { |
| 66 |
fprintf(write_to, |
| 67 |
"Usage: samtools samples [options] <input> [...]\n" |
| 68 |
" samtools samples [options] -X f1.bam f2.bam f1.bam.bai f2.bai \n" |
| 69 |
" find dir1 dir2 -type f \\(-name \"*.bam\" -o -name \"*.cram\" \\) | samtools samples [options]\n" |
| 70 |
" find dir1 dir2 -type f \\(-name \"*.bam\" -o -name \"*.bai\" \\) | sort | paste - - | samtools samples -X [options]\n" |
| 71 |
"\n" |
| 72 |
"Options:\n" |
| 73 |
" -? print help and exit\n" |
| 74 |
" -h add the columns header before printing the results\n" |
| 75 |
" -i test if the file is indexed.\n" |
| 76 |
" -T <tag> provide the sample tag name from the @RG line [SM].\n" |
| 77 |
" -o <file> output file [samtools_stdout].\n" |
| 78 |
" -f <file.fa> load an indexed fasta file in the collection of references. Can be used multiple times.\n" |
| 79 |
" -F <file.txt> read a file containing the paths to indexed fasta files. One path per line.\n" |
| 80 |
" -X use a custom index file.\n" |
| 81 |
"\n" |
| 82 |
" Using -f or -F will add a column containing the path to the reference or \".\" if the reference was not found.\n" |
| 83 |
"\n" |
| 84 |
); |
| 85 |
} |
| 86 |
|
| 87 |
|
| 88 |
|
| 89 |
static int load_dictionary(struct Params* params, const char* filename) { |
| 90 |
FaidxPath* head = params->faidx; |
| 91 |
FaidxPath* ptr = (FaidxPath*)malloc(sizeof(FaidxPath)); |
| 92 |
if (ptr == NULL) { |
| 93 |
print_error_errno("samples", "Out of memory"); |
| 94 |
return EXIT_FAILURE; |
| 95 |
} |
| 96 |
ptr->filename = strdup(filename); |
| 97 |
if (ptr->filename == NULL) { |
| 98 |
free(ptr); |
| 99 |
print_error_errno("samples", "Out of memory"); |
| 100 |
return EXIT_FAILURE; |
| 101 |
} |
| 102 |
ptr->faidx = fai_load(filename); |
| 103 |
if (ptr->faidx == NULL) { |
| 104 |
free(ptr->filename); |
| 105 |
free(ptr); |
| 106 |
print_error_errno("samples", "Cannot load index from \"%s\"", filename); |
| 107 |
return EXIT_FAILURE; |
| 108 |
} |
| 109 |
|
| 110 |
params->faidx = ptr; |
| 111 |
ptr->next = head; |
| 112 |
return EXIT_SUCCESS; |
| 113 |
} |
| 114 |
|
| 115 |
|
| 116 |
static int load_dictionaries(Params* params, const char* filename) { |
| 117 |
int ret; |
| 118 |
htsFile* in; |
| 119 |
int status = EXIT_SUCCESS; |
| 120 |
|
| 121 |
in = hts_open(filename, "r"); |
| 122 |
if (in == NULL) { |
| 123 |
print_error_errno("samples", "Cannot open \"%s\"", filename); |
| 124 |
status = EXIT_FAILURE; |
| 125 |
} else { |
| 126 |
kstring_t ks = KS_INITIALIZE; |
| 127 |
while ((ret = hts_getline(in, KS_SEP_LINE, &ks)) >= 0) { |
| 128 |
if (load_dictionary(params, ks_str(&ks)) != EXIT_SUCCESS) { |
| 129 |
status = EXIT_FAILURE; |
| 130 |
break; |
| 131 |
} |
| 132 |
} |
| 133 |
ks_free(&ks); |
| 134 |
hts_close(in); |
| 135 |
} |
| 136 |
return status; |
| 137 |
} |
| 138 |
|
| 139 |
|
| 140 |
static int print_sample( |
| 141 |
Params* params, |
| 142 |
sam_hdr_t *header, |
| 143 |
int has_index, |
| 144 |
const char* sample, |
| 145 |
const char* fname) { |
| 146 |
fputs(sample, params->out); |
| 147 |
fputc('\t', params->out); |
| 148 |
fputs(fname, params->out); |
| 149 |
if (params->test_index) { |
| 150 |
fprintf(params->out, "\t%c", has_index ? 'Y' : 'N'); |
| 151 |
} |
| 152 |
if (params->faidx != NULL) { |
| 153 |
FaidxPath* ref = NULL; |
| 154 |
FaidxPath* curr = params->faidx; |
| 155 |
while (curr != NULL) { |
| 156 |
|
| 157 |
if (faidx_nseq(curr->faidx) == header->n_targets) { |
| 158 |
int i; |
| 159 |
for (i = 0; i < faidx_nseq(curr->faidx); i++) { |
| 160 |
|
| 161 |
if (strcmp(faidx_iseq(curr->faidx, i), header->target_name[i]) != 0) break; |
| 162 |
|
| 163 |
if (faidx_seq_len(curr->faidx, faidx_iseq(curr->faidx, i)) != header->target_len[i]) break; |
| 164 |
} |
| 165 |
|
| 166 |
if (i == faidx_nseq(curr->faidx)) { |
| 167 |
ref = curr; |
| 168 |
break; |
| 169 |
} |
| 170 |
} |
| 171 |
curr = curr->next; |
| 172 |
} |
| 173 |
fputc('\t', params->out); |
| 174 |
if (ref == NULL) { |
| 175 |
fputc('.', params->out); |
| 176 |
} else { |
| 177 |
fputs(curr->filename, params->out); |
| 178 |
} |
| 179 |
} |
| 180 |
fputc('\n', params->out); |
| 181 |
return 0; |
| 182 |
} |
| 183 |
|
| 184 |
|
| 185 |
static int print_samples(Params* params, const char* fname, const char* baifname) { |
| 186 |
samFile *in = 0; |
| 187 |
sam_hdr_t *header = NULL; |
| 188 |
int n_rg; |
| 189 |
int status = EXIT_SUCCESS; |
| 190 |
khash_t(sm) *sample_set = NULL; |
| 191 |
khint_t k; |
| 192 |
int count_samples = 0; |
| 193 |
int has_index = 0; |
| 194 |
|
| 195 |
if ((sample_set = kh_init(sm)) == NULL) { |
| 196 |
print_error("samples", "Failed to initialise sample hash"); |
| 197 |
status = EXIT_FAILURE; |
| 198 |
goto end_print; |
| 199 |
} |
| 200 |
|
| 201 |
if ((in = sam_open_format(fname, "r", NULL)) == 0) { |
| 202 |
print_error_errno("samples", "Failed to open \"%s\" for reading", fname); |
| 203 |
status = EXIT_FAILURE; |
| 204 |
goto end_print; |
| 205 |
} |
| 206 |
if ((header = sam_hdr_read(in)) == 0) { |
| 207 |
print_error("samples", "Failed to read the header from \"%s\"", fname); |
| 208 |
status = EXIT_FAILURE; |
| 209 |
goto end_print; |
| 210 |
} |
| 211 |
|
| 212 |
|
| 213 |
if (params->test_index) { |
| 214 |
hts_idx_t *bam_idx; |
| 215 |
|
| 216 |
if (baifname != NULL) { |
| 217 |
bam_idx = sam_index_load3(in, fname, baifname, HTS_IDX_SILENT_FAIL); |
| 218 |
} |
| 219 |
|
| 220 |
else { |
| 221 |
bam_idx = sam_index_load3(in, fname, NULL, HTS_IDX_SILENT_FAIL); |
| 222 |
} |
| 223 |
has_index = bam_idx != NULL; |
| 224 |
if (bam_idx != NULL) hts_idx_destroy(bam_idx); |
| 225 |
|
| 226 |
} |
| 227 |
|
| 228 |
|
| 229 |
n_rg = sam_hdr_count_lines(header, "RG"); |
| 230 |
if (n_rg > 0) { |
| 231 |
int i, r, ret; |
| 232 |
char* sample; |
| 233 |
kstring_t sm_val = KS_INITIALIZE; |
| 234 |
for (i = 0; i < n_rg; i++) { |
| 235 |
r = sam_hdr_find_tag_pos(header, "RG", i, params->tag, &sm_val); |
| 236 |
if (r < 0) continue; |
| 237 |
k = kh_get(sm, sample_set, ks_str(&sm_val)); |
| 238 |
if (k != kh_end(sample_set)) continue; |
| 239 |
sample = strdup(ks_str(&sm_val)); |
| 240 |
if (sample == NULL) { |
| 241 |
print_error_errno("samples", "Out of memory"); |
| 242 |
status = EXIT_FAILURE; |
| 243 |
goto end_print; |
| 244 |
} |
| 245 |
kh_put(sm, sample_set, sample, &ret); |
| 246 |
if (ret < 0) { |
| 247 |
print_error("samples", "Failed to insert key '%s' into sample_set", sample); |
| 248 |
free(sample); |
| 249 |
status = EXIT_FAILURE; |
| 250 |
goto end_print; |
| 251 |
} |
| 252 |
++count_samples; |
| 253 |
} |
| 254 |
ks_free(&sm_val); |
| 255 |
} |
| 256 |
if (count_samples == 0) { |
| 257 |
print_sample(params, header, has_index, ".", fname); |
| 258 |
} else { |
| 259 |
for (k = kh_begin(sample_set); k != kh_end(sample_set); ++k) { |
| 260 |
if (kh_exist(sample_set, k)) { |
| 261 |
char* sample = (char*)kh_key(sample_set, k); |
| 262 |
print_sample(params, header, has_index, sample, fname); |
| 263 |
} |
| 264 |
} |
| 265 |
} |
| 266 |
|
| 267 |
end_print: |
| 268 |
if (sample_set != NULL) { |
| 269 |
for (k = kh_begin(sample_set); k != kh_end(sample_set); ++k) { |
| 270 |
if (kh_exist(sample_set, k)) { |
| 271 |
char* sample = (char*)kh_key(sample_set, k); |
| 272 |
free(sample); |
| 273 |
} |
| 274 |
} |
| 275 |
kh_destroy(sm, sample_set); |
| 276 |
} |
| 277 |
if (header != NULL) sam_hdr_destroy(header); |
| 278 |
if (in != NULL) sam_close(in); |
| 279 |
|
| 280 |
return status; |
| 281 |
} |
| 282 |
|
| 283 |
|
| 284 |
int main_samples(int argc, char** argv) { |
| 285 |
int status = EXIT_SUCCESS; |
| 286 |
int print_header = 0; |
| 287 |
int has_index_file = 0; |
| 288 |
Params params; |
| 289 |
char* out_filename = NULL; |
| 290 |
FaidxPath* fai; |
| 291 |
|
| 292 |
strcpy(params.tag, "SM"); |
| 293 |
params.faidx = NULL; |
| 294 |
params.test_index =0; |
| 295 |
|
| 296 |
int opt; |
| 297 |
while ((opt = getopt_long(argc, argv, "?hiXo:f:F:T:", NULL, NULL)) != -1) { |
| 298 |
switch (opt) { |
| 299 |
case 'h': |
| 300 |
print_header = 1; |
| 301 |
break; |
| 302 |
case 'o': |
| 303 |
out_filename = optarg; |
| 304 |
break; |
| 305 |
case 'i': |
| 306 |
params.test_index = 1; |
| 307 |
break; |
| 308 |
case 'f': |
| 309 |
if (load_dictionary(¶ms, optarg) != EXIT_SUCCESS) { |
| 310 |
return EXIT_FAILURE; |
| 311 |
} |
| 312 |
break; |
| 313 |
case 'F': |
| 314 |
if (load_dictionaries(¶ms, optarg) != EXIT_SUCCESS) { |
| 315 |
return EXIT_FAILURE; |
| 316 |
} |
| 317 |
break; |
| 318 |
case 'T': |
| 319 |
if (strlen(optarg) != 2) { |
| 320 |
print_error("samples", "Length of tag \"%s\" is not 2.", optarg); |
| 321 |
return EXIT_FAILURE; |
| 322 |
} |
| 323 |
strcpy(params.tag, optarg); |
| 324 |
break; |
| 325 |
case '?': |
| 326 |
usage_samples(samtools_stdout); |
| 327 |
return EXIT_SUCCESS; |
| 328 |
case 'X': |
| 329 |
has_index_file = 1; |
| 330 |
break; |
| 331 |
default: |
| 332 |
usage_samples(samtools_stderr); |
| 333 |
return EXIT_FAILURE; |
| 334 |
} |
| 335 |
} |
| 336 |
|
| 337 |
|
| 338 |
if (argc == optind && isatty(STDIN_FILENO)) { |
| 339 |
usage_samples(samtools_stderr); |
| 340 |
return EXIT_FAILURE; |
| 341 |
} |
| 342 |
|
| 343 |
if (out_filename != NULL) { |
| 344 |
params.out = fopen(out_filename, "w"); |
| 345 |
if (params.out == NULL) { |
| 346 |
print_error_errno("samples", "Cannot open \"%s\" for writing", out_filename); |
| 347 |
return EXIT_FAILURE; |
| 348 |
} |
| 349 |
} else { |
| 350 |
params.out = samtools_stdout; |
| 351 |
} |
| 352 |
|
| 353 |
if (print_header) { |
| 354 |
fprintf(params.out, "#%s\tPATH", params.tag); |
| 355 |
if (params.test_index) fprintf(params.out, "\tINDEX"); |
| 356 |
if (params.faidx != NULL) fprintf(params.out, "\tREFERENCE"); |
| 357 |
fprintf(params.out, "\n"); |
| 358 |
} |
| 359 |
|
| 360 |
|
| 361 |
if (argc == optind) { |
| 362 |
htsFile* fp = hts_open("-", "r"); |
| 363 |
if (fp == NULL) { |
| 364 |
print_error_errno("samples", "Cannot read from stdin"); |
| 365 |
status = EXIT_FAILURE; |
| 366 |
} else { |
| 367 |
kstring_t ks = KS_INITIALIZE; |
| 368 |
int ret; |
| 369 |
while ((ret = hts_getline(fp, KS_SEP_LINE, &ks)) >= 0) { |
| 370 |
char* bai_path = NULL; |
| 371 |
if (has_index_file) { |
| 372 |
|
| 373 |
char* tab = strchr(ks_str(&ks), '\t'); |
| 374 |
if (tab == NULL || *(tab+1) == '\0') { |
| 375 |
print_error_errno("samples", "Expected path-to-bam(tab)path-to-index but got \"%s\"", ks_str(&ks)); |
| 376 |
status = EXIT_FAILURE; |
| 377 |
break; |
| 378 |
} |
| 379 |
*tab=0; |
| 380 |
bai_path = (tab + 1); |
| 381 |
} |
| 382 |
if (print_samples(¶ms, ks_str(&ks), bai_path) != EXIT_SUCCESS) { |
| 383 |
status = EXIT_FAILURE; |
| 384 |
break; |
| 385 |
} |
| 386 |
} |
| 387 |
ks_free(&ks); |
| 388 |
hts_close(fp); |
| 389 |
} |
| 390 |
} |
| 391 |
|
| 392 |
else if (has_index_file) { |
| 393 |
|
| 394 |
if ((argc - optind) % 2 != 0) { |
| 395 |
print_error("samples","Odd number of filenames detected! Each BAM file should have an index file"); |
| 396 |
status = EXIT_FAILURE; |
| 397 |
} else { |
| 398 |
int i; |
| 399 |
int n = (argc - optind ) / 2; |
| 400 |
for (i = 0; i < n; i++) { |
| 401 |
if (print_samples(¶ms, argv[optind+i], argv[optind+i+n]) != EXIT_SUCCESS) { |
| 402 |
status = EXIT_FAILURE; |
| 403 |
break; |
| 404 |
} |
| 405 |
} |
| 406 |
} |
| 407 |
} else { |
| 408 |
int i; |
| 409 |
for (i = optind; i < argc; i++) { |
| 410 |
if (print_samples(¶ms, argv[i], NULL) != EXIT_SUCCESS) { |
| 411 |
status = EXIT_FAILURE; |
| 412 |
break; |
| 413 |
} |
| 414 |
} |
| 415 |
} |
| 416 |
|
| 417 |
fai = params.faidx; |
| 418 |
while (fai != NULL) { |
| 419 |
FaidxPath* next = fai -> next; |
| 420 |
free(fai->filename); |
| 421 |
fai_destroy(fai->faidx); |
| 422 |
free(fai); |
| 423 |
fai = next; |
| 424 |
} |
| 425 |
|
| 426 |
if (fflush(params.out) != 0) { |
| 427 |
print_error_errno("samples", "Cannot flush output"); |
| 428 |
status = EXIT_FAILURE; |
| 429 |
} |
| 430 |
if (out_filename != NULL) { |
| 431 |
fclose(params.out); |
| 432 |
} |
| 433 |
|
| 434 |
return status; |
| 435 |
} |