aboutsummaryrefslogtreecommitdiff
path: root/count_nucleobases.c
blob: 0396ea6c12e41352ca911f3161a10aa7a518cacb (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
// Copyright 2013 Calvin Morrison
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <stdint.h>
int main(int argc, char **argv) {

  long unsigned long a = 0;
  long unsigned long c = 0;
  long unsigned long g = 0;
  long unsigned long t = 0;

  if(argc != 2) {
    printf("Please supply a filename, and only a filename\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);
  }

  char line[8192];
  while (fgets(line, 8192, fh) != NULL) {

    int i = 0;

    for(i = 0; i < strlen(line); i++) {
      switch(line[i]) {
        case 'A':
        case 'a':
          a++;
          break;
        case 'C':
        case 'c':
          c++;
          break;
        case 'G':
        case 'g':
          g++;
          break;
        case 'T':
        case 't':
          t++;
          break;
      } 
    }
  }

  printf("A:%llu\nC:%llu\nG:%llu\nT:%llu\n", a, c, g, t);

  return EXIT_SUCCESS;
}