zoukankan      html  css  js  c++  java
  • C语言计算fastq文件GC含量2

    改进了一下,利用zlib可以读取gz格式的压缩文件,也可以直接计算非压缩格式

    #include <stdio.h>
    #include <stdlib.h>
    #include <string.h>
    #include <zlib.h>
    
    #define buff 1024
    
    typedef unsigned long long int u_llong;
    
    static void usage(int num,const char *str)
    {
    	if(num !=2)
    	{
    		fprintf(stderr,"usage: %s fqFile
    ",str);
    		exit(0);
    	}
    }
    
    static u_llong* gcN(char base[buff])
    {
    	base[strlen(base)-1]=''; 
    	
    	int i;
    	static u_llong gactn[]={0,0,0,0,0};
    	for(i=0; i<strlen(base); i++)
    	{
    		if(base[i]=='G')
    			gactn[0]++; 
    		if(base[i]=='A')
    			gactn[1]++; 
    		if(base[i]=='C')
    			gactn[2]++;
    		if(base[i]=='T')
    			gactn[3]++; 
    		if(base[i]=='N')
    			gactn[4]++;
    	}
    	return gactn;
    }
    
    static void calc(const char *fqfile)
    {
    	//FILE *fq;
    	gzFile fq;
    	if((fq=gzopen(fqfile,"r")) == NULL)
    	{
    		perror("fopen");
    		exit(1);
    	}
    	//fprintf(stderr,"fq file <%s> open suceed!
    ",fqfile);
    	
    	char base[buff];
    	char qual=0;
    	u_llong *p=NULL;
    	while((gzgets(fq,base,buff))!= NULL)  //  这里用 gzgets 替代 fgets
    	{
    		if(base[0]=='@')
    		{
    			continue;
    		}
    		if(base[0]=='+')
    		{
    			qual=1;
    			continue;
    		}
    		if(qual==1)
    		{
    			qual=0;
    			continue;
    		}
    		
    		p=gcN(base); // G A C T N
    	}
    
    	float GClevel; 
    	u_llong sum=0;
    	for(int i=0; i<5; i++)
    	{
    		sum+=*(p+i);
    	}
    	GClevel=(float)(*p+*(p+2)) / sum * 100;
    	
    	fprintf(stdout,"G:%lld	A:%lld
    C:%lld	T:%lld
    N:%lld	sum:%lld
    ",*p,*(p+1),*(p+2),*(p+3),*(p+4),sum);
    	fprintf(stdout,"GC:%.2f%%
    ",GClevel);
    }
    
    int main(int argc,const char *argv[])
    {		
    	usage(argc,argv[0]);
    	calc(argv[1]);
    
    	exit(0);
    }
    

     备注: gcc编译记得添加参数 -lz 

    本文版权归作者和博客园共有,欢迎转载,但未经作者同意必须保留此段声明,且在文章页面明显位置给出原文连接,否则保留追究法律责任的权利.
  • 相关阅读:
    jquery插件
    Bash Shell实用快捷键
    Cisco SG300系列交换机划分VLan与普通路由器连接配置
    PostgreSQL用户角色及其属性介绍
    Ubuntu 10.04 32位桌面版+OpnERP 6.1.1
    Postgresql 帐号密码修改方法
    linux查找日志技巧
    Python 黑魔法 --- 描述器(descriptor)
    Nginx如何保留真实IP和获取前端IP
    Nginx 配置 SSL 证书 + 搭建 HTTPS 网站教程
  • 原文地址:https://www.cnblogs.com/mmtinfo/p/13705734.html
Copyright © 2011-2022 走看看