Skip to content

Instantly share code, notes, and snippets.

@JervenBolleman
Created August 12, 2014 11:51
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save JervenBolleman/e1dfc0556802e7f0706a to your computer and use it in GitHub Desktop.
Save JervenBolleman/e1dfc0556802e7f0706a to your computer and use it in GitHub Desktop.
GC count in c but allocating the correc file size
#include <stdio.h>
#include <stdlib.h>
#define MAXFLEN 2600000000 /* Larger than the file. */
#include <sys/stat.h>
int main()
{
char tablegc[256];
char tableat[256];
int gc=0;
int at=0;
char fn[] = "Homo_sapeins.GR37.dba_rm.chromsome.Y.fa";
FILE *f=fopen(fn,"r");
struct stat st;
int len=MAXFLEN;
if (stat(fn, &st) == 0)
len = st.st_size;
char *m=malloc(len);
int items=fread(m,1,len,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