Create a gist now

Instantly share code, notes, and snippets.

What would you like to do?
Daniel Spångber's C code, when reading whole file, and using table to count, and parallellizing it with OpenMP. See http://saml.rilspace.org/node/248
#include <stdio.h>
#include <stdlib.h>
#define MAXFLEN 70000000 /* Larger than the file. */
int main()
{
char *m=malloc(MAXFLEN);
char tablegc[256];
char tableat[256];
int gc=0;
int at=0;
FILE *f=fopen("Homo_sapiens.GRCh37.67.dna_rm.chromosome.Y.fa","r");
int items=fread(m,1,MAXFLEN,f); /* Read the whole file into memory. */
char *ptr=m;
int nums, i;
while (*ptr++!='\n'); /* Find end of first line. */
for (i=0; i<256; i++)
tablegc[i]=0;
for (i=0; i<256; i++)
tableat[i]=0;
tableat['A']=1;
tableat['T']=1;
tablegc['C']=1;
tablegc['G']=1;
nums=items-(ptr-m);
#pragma omp parallel for private(i),reduction(+:gc),reduction(+:at)
for (i=0; i<nums; i++) {
char c=ptr[i];
at+=tableat[(int)c];
gc+=tablegc[(int)c];
}
fclose(f);
free(m);
int total = at + gc;
printf("%.10f\n",(100.*gc)/total);
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment