// Copyright 2013 Calvin Morrison #include #include #include #include #include #include #include "kmer_utils.h" void help() { printf("usage: kmer_counts_per_sequence input_file kmer [kmer-file] ...\n\n" "count mers in each sequence of size k from a fasta file\n" "\n" " --input -i input fasta file to count\n" " --kmer -k size of mers to count\n" " --mer-file -m a file containing a list of mers you are interested\n" " in opening. this will enable output your results in\n" " a sparse format \n" " --sparse -s output values in a sparse format. output is in the\n" " order sequence_number, mer_index, value\n" "\n" "Report all bugs to mutantturkey@gmail.com\n" "\n" "Copyright 2014 Calvin Morrison, Drexel University.\n" "\n" "If you are using any dna-utils tool for a publication\n" "please cite your usage:\n\n" "dna-utils. Drexel University, Philadelphia USA, 2014;\n" "software available at www.github.com/EESI/dna-utils/\n"); } int main(int argc, char **argv) { // getdelim variables char *fn = NULL; FILE *fh = NULL; // for specific mers char *mer_fn = NULL; size_t num_desired_indicies = 0; size_t *desired_indicies = NULL; // for getdelim char *line = NULL; size_t len = 0; ssize_t read; // sizes unsigned int kmer = 0; size_t width = 0; bool sparse = false; bool kmer_set = false; bool specific_mers = false; static struct option long_options[] = { {"input", required_argument, 0, 'i'}, {"kmer", required_argument, 0, 'k'}, {"sparse", no_argument, 0, 's'}, {"mer-file", required_argument, 0, 'm'}, {"help", no_argument, 0, 'h'}, {0, 0, 0, 0} }; while (1) { int option_index = 0; int c = 0; c = getopt_long (argc, argv, "i:k:m:vsh", long_options, &option_index); if (c == -1) break; switch (c) { case 'i': fn = optarg; break; case 'k': kmer = atoi(optarg); kmer_set = true; break; case 's': sparse = true; break; case 'm': mer_fn = optarg; specific_mers = true; break; case 'h': help(); exit(EXIT_SUCCESS); break; case 'v': printf("dna-utils version " VERSION "\n"); exit(EXIT_SUCCESS); break; default: break; } } if(argc == 1) { help(); exit(EXIT_FAILURE); } if(fn == NULL) { fprintf(stderr, "no input file specified with -i, reading from stdin\n"); fh = stdin; } else { fh = fopen(fn, "r"); if(fh == NULL) { fprintf(stderr, "Could not open %s - %s\n", fn, strerror(errno)); exit(EXIT_FAILURE); } } if(!kmer_set) { fprintf(stderr, "Error: kmer (-k) must be supplied\n"); exit(EXIT_FAILURE); } if(kmer == 0) { fprintf(stderr, "Error: invalid kmer - '%d'.\n", kmer); exit(EXIT_FAILURE); } width = pow_four(kmer); if(specific_mers) { sparse = false; desired_indicies = malloc((width) * sizeof(size_t)); if(desired_indicies == NULL) exit(EXIT_FAILURE); num_desired_indicies = load_specific_mers_from_file(mer_fn, kmer, width, desired_indicies); if(num_desired_indicies == 0) { fprintf(stderr, "Error: no mers loaded from file"); exit(EXIT_FAILURE); } } unsigned long long *counts = malloc((width+ 1) * sizeof(unsigned long long)); if(counts == NULL) exit(EXIT_FAILURE); char *str = malloc(BUFSIZ); if(str == NULL) { fprintf(stderr, strerror(errno)); exit(EXIT_FAILURE); } size_t str_size = BUFSIZ; unsigned long long sequence = 0; while ((read = getdelim(&line, &len, '>', fh)) != -1) { long long i = 0; size_t k = 0; memset(counts, 0, width * sizeof(unsigned long long)); // find our first \n, this should be the end of the header char *start = strchr(line, '\n'); if(start == NULL) continue; // point to one past that. start = start + 1; size_t start_len = strlen(start); // if our current str buffer isn't big enough, realloc if(start_len + 1 > str_size + 1) { str = realloc(str, start_len + 1); if(str == NULL) { exit(EXIT_FAILURE); fprintf(stderr, strerror(errno)); } } // strip out all other newlines to handle multiline sequences str = strnstrip(start, str, '\n',start_len); size_t seq_length = strlen(str); // reset our count matrix to zero for(k = 0; k < seq_length; k++) { str[k] = alpha[(int)str[k]]; } for(i = 0; i < (signed long long)(seq_length - kmer + 1); i++) { size_t mer = num_to_index(&str[i],kmer, width, &i); counts[mer]++; } if(specific_mers) { for(k = 0; k < num_desired_indicies; k++) { if(desired_indicies[k] != 0) fprintf(stdout, "%llu\t%zu\t%llu\n", sequence, desired_indicies[k], counts[desired_indicies[k]]); } } else if(sparse) { for(k = 0; k < width; k++) { if(counts[k] != 0) fprintf(stdout, "%llu\t%zu\t%llu\n", sequence, k, counts[k]); } } else { for(k = 0; k < width - 1; k++) fprintf(stdout, "%llu\t", counts[k]); fprintf(stdout, "%llu\n", counts[width - 1]); } sequence++; } free(counts); free(line); free(desired_indicies); free(str); fclose(fh); return EXIT_SUCCESS; }