aboutsummaryrefslogtreecommitdiff
path: root/count_nucleobases.c
blob: 1b432da0cf2e4e3f3dfe2fd5491c51d69b87f230 (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
// Copyright 2013 Calvin Morrison
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <stdint.h>
#include <stdbool.h>
int main() {

  size_t len = 0;

  unsigned long long counts[256] = {0};

	char buffer[4096];
	bool header = false;

	len =	fread(&buffer, 1, 1, stdin);

	if(len != NULL) {
		if(buffer[0] == '>') {
			header = true;
			while((len = fread(&buffer, 1, 4096, stdin)) != NULL) {
				unsigned int i = 0;
				for(i = 0; i < len; i++) {
 					if(buffer[i] == '>') {
						header = true;
						continue;
					}
					else if(buffer[i] == '\n' && header == true) 
						header = false;
					if(header == false ) {
						counts[(int)buffer[i]]++;
					}
				}
			}
		}  
		else { 
			fprintf(stderr, "this does not look like a fasta file\n"); 
			exit(EXIT_FAILURE);
		}
	} else { 
		fprintf(stderr, "could not read file\n"); 
		exit(EXIT_FAILURE);
	}

  printf("A:%llu\nC:%llu\nG:%llu\nT:%llu\n",
         counts['a'] + counts['A'],
         counts['c'] + counts['C'],
         counts['g'] + counts['G'],
         counts['t'] + counts['T']);

  return EXIT_SUCCESS;
}