aboutsummaryrefslogtreecommitdiff
path: root/kmer_total_count.c
blob: 48e64b5fe3cb225a196837d6130c9b470ff6b5fa (plain)
1
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
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
// Copyright 2013 Calvin Morrison
#include <math.h>
#include <stdint.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>

#include "kmer_utils.h"

int main(int argc, char **argv) {

  char *line = NULL;
  size_t len = 0;
  ssize_t read;
  unsigned int kmer = 0;
  unsigned long i = 0;
  unsigned long width = 0;
  unsigned long long *counts;

  if(argc != 3) {
    printf("Please supply a filename and a kmer\n");
    exit(EXIT_FAILURE);
  }

  FILE *fh = fopen(argv[1], "r" );
  if(fh == NULL) {
    fprintf(stderr, "Couldn't open: %s\n", argv[1]);
    exit(EXIT_FAILURE);
  }

  // second argument is the kmer
  kmer = atoi(argv[2]);

  // width is 4^kmer 
  width = (int)pow(4, kmer);

  // malloc our counts matrix
  counts = malloc((width+ 1) * sizeof(unsigned long long));
  if(counts == NULL) 
    exit(EXIT_FAILURE);

  while ((read = getline(&line, &len, fh)) != -1) {
    if(line[0] != '>')   {
      for(i = 0; i < strlen(line) - kmer; i++) {
        counts[convert_kmer_to_index(&line[i],kmer, width)]++;
      }
    }
  }

  for(i = 0; i < width; i++)
    printf("%llu\n", counts[i]);


  return EXIT_SUCCESS;
}