3faf91c30c46e8c55a6644ba75c8f682724e6a0a
[sws1-1415.git] / ass1 / mart / sws1-s4109503-s4202015 / exercise2 / parsegenome.c
1 #include <stdio.h>
2 #include <stdlib.h>
3 #include <string.h>
4
5 int main(int argc, char* argv[])
6 {
7 char c;
8 char charsperline = 0;
9 short numlines = 0;
10 long long unsigned int occs = 0;
11 /* When the filepath is not given or "-" assume stdin */
12 FILE *f = (argc!=2 || strcmp(argv[1], "-")==0) ? stdin : fopen(argv[1], "r");
13 if(f == NULL)
14 {
15 printf("Unable to open file or stdin...\n");
16 return 2;
17 }
18 while((c = fgetc(f)) != EOF)
19 {
20 if(c == '\n' && charsperline == 100 && numlines <= 500)
21 {
22 numlines += 1;
23 charsperline = 0;
24 }
25 else if((c == 'A' || c=='C' || c=='G' || c=='T') && charsperline < 100)
26 { /*Fancy polynomial that translates the ascii value to a bitshift*/
27 occs += 1LL<<(int)(0.026514*c*c*c-6.049*c*c+459.96*c-11621.2);
28 charsperline += 1;
29 }
30 else
31 break;
32 }
33 if(f != stdout)
34 fclose(f);
35 if(numlines != 500)
36 return -1;
37 printf("A: %llu\nC: %llu\nG: %llu\nT: %llu\n", occs & 0xFFFF,
38 (occs >> 16) & 0xFFFF, (occs >> 32) & 0xFFFF, (occs >> 48) & 0xFFFF);
39 return 0;
40 }