Skip to content

Instantly share code, notes, and snippets.

@samuell
Created May 10, 2013 17:12
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
Star You must be signed in to star a gist
Save samuell/5555848 to your computer and use it in GitHub Desktop.
Daniel Spångber's C code, when reading whole file, and using table to count. 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);
for (i=0; i<nums; i++) {
char c=*ptr++;
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