now really final, submitting...
[sws1-1415.git] / ass1 / mart / sws1-s4109503-s4202015 / exercise2 / parsegenome.c
1 #include <stdint.h> /* We save the all the frequencies in a 64 bit int */
2 #include <stdio.h> /* We extract/add them using bitshifts */
3 #include <stdlib.h> /* When no filepath is specified assume stdin */
4 #include <string.h>
5
6 int main(int argc, char* argv[])
7 {
8 char *alphabet = "AGTC";
9 char c;
10 int charsperline = 0;
11 int numlines = 0;
12 uint64_t occs = 0;
13 FILE *f = argc != 2 ? stdin : fopen(argv[1], "r");
14 if(f == NULL)
15 return 2;
16 while((c = fgetc(f)) != EOF)
17 if(c == '\n' && charsperline == 100 && numlines++ < 500)
18 charsperline = 0;
19 else if(strchr(alphabet, c) && charsperline++ < 100)
20 occs += 1LL<<((int)(strchr(alphabet, c)-alphabet)*16);
21 else
22 break;
23 if(f != stdin)
24 fclose(f);
25 if(numlines != 500)
26 return -1;
27 for(unsigned i=0; i<strlen(alphabet); i++)
28 printf("%c: %u\n", alphabet[i], (uint16_t)(occs >> (16*i)));
29 return 0;
30 }