C语言计算fastq文件GC含量2

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

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
#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\n",str);
        exit(0);
    }
}
 
static u_llong* gcN(char base[buff])
{
    base[strlen(base)-1]='\0';
     
    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!\n",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\tA:%lld\nC:%lld\tT:%lld\nN:%lld\tsum:%lld\n",*p,*(p+1),*(p+2),*(p+3),*(p+4),sum);
    fprintf(stdout,"GC:%.2f%%\n",GClevel);
}
 
int main(int argc,const char *argv[])
{      
    usage(argc,argv[0]);
    calc(argv[1]);
 
    exit(0);
}

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

posted @   天使不设防  阅读(227)  评论(0编辑  收藏  举报
编辑推荐:
· SQL Server 2025 AI相关能力初探
· Linux系列:如何用 C#调用 C方法造成内存泄露
· AI与.NET技术实操系列(二):开始使用ML.NET
· 记一次.NET内存居高不下排查解决与启示
· 探究高空视频全景AR技术的实现原理
阅读排行:
· 阿里最新开源QwQ-32B,效果媲美deepseek-r1满血版,部署成本又又又降低了!
· 单线程的Redis速度为什么快?
· SQL Server 2025 AI相关能力初探
· AI编程工具终极对决:字节Trae VS Cursor,谁才是开发者新宠?
· 展开说说关于C#中ORM框架的用法!
点击右上角即可分享
微信分享提示