微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

可以在循环内多次使用 getline() 吗? - Cython,文件读取

如何解决可以在循环内多次使用 getline() 吗? - Cython,文件读取

我想读取一个 4 行 4 行的文件(这是一个带有 DNA 序列的 fastq 文件)。
当我一行一行或两行读取文件时,没有问题,但是当我一次读取 3 或 4 行时,我的代码崩溃了(内核似乎在 jupyter notebook 上死了)。 (取消注释最后一部分,或 4 个 getline() 中的任意 3 个。
我尝试使用双字符数组 (char**) 来存储行,但遇到了同样的问题。

知道可能是什么原因吗?

使用 Python 3.7.3、Cython 0.29,更新了所有其他库。正在读取的文件大约为 1.3GB,机器有 8GB,ubuntu 16.04。 代码改编自 https://gist.github.com/pydemo/0b85bd5d1c017f6873422e02aeb9618a

%%cython
from libc.stdio cimport FILE,fopen,fclose,getline
    
def fastq_reader(early_stop=10):
    cdef const char* fname = b'/path/to/file'
    cdef FILE* cfile
    cfile = fopen(fname,"rb")

    cdef:
        char * line_0 = NULL
        char * line_1 = NULL
        char * line_2 = NULL
        char * line_3 = NULL
        size_t seed = 0
        ssize_t length_line
        unsigned long long line_nb = 0

    while True:
        length_line = getline(&line_0,&seed,cfile)
        if length_line < 0: break
        
        length_line = getline(&line_1,cfile)
        if length_line < 0: break
        
#         length_line = getline(&line_2,cfile)
#         if length_line < 0: break
        
#         length_line = getline(&line_3,cfile)
#         if length_line < 0: break

        line_nb += 4
        if line_nb > early_stop:
            break

    fclose(cfile)
    return line_nb

fastq_reader(early_stop=20000)

解决方法

根本问题是我对getline() getline() c reference

的误解

要将行存储在不同的变量中,每个行指针 n 需要关联的 *lineptr

如果在调用前 *lineptr 设置为 NULL 并且 *n 设置为 0,则 getline() 将分配一个缓冲区来存储该行。

或者,在调用 getline() 之前,*lineptr 可以包含一个 指向 malloc(3) 分配的缓冲区 *n 字节大小的指针。如果 缓冲区不足以容纳该行,getline() 调整它的大小 使用 realloc(3),根据需要更新 *lineptr 和 *n。

n(或我代码中的 seed)将保存为指针分配的缓冲区大小,其中 getline() 放置传入行。当我为不同的指针设置相同的缓冲区变量时,getline 得到了错误的字符大小信息* line_xxx。


因为 fastq 文件通常是这样的:

@read_id_usually_short
CTATACCACCAAGGCTGGAAATTGTAAAACACACCGCCTGACATATCAATAAGGTGTCAAATTCCCTTTTCTCTAGCTTTCGTACT_very_long
+
-///.)/.-/)//-//..-*...-.&%&.--%#(++*/.//////,/*//+(.///..,%&-#&)..,)/.,.._same_length_as_line_2

具有相同缓冲区长度的一两个 getline() 没有错误,因为缓冲区太小并且 getline 增大了指针的大小。
但是当使用 3 或 4 个 getlines() 时,调用 length_line = getline(&line_2,&seed,cfile) 被要求存储长度为 2 ('+\n') 的 char*,同时获取(错误信息)指针 line_2 已经足够大(line_1 的大小)。


所以(简单)解决方案是

%%cython
from libc.stdio cimport FILE,fopen,fclose,getline
    
def fastq_reader(early_stop=10):
    cdef const char* fname = b'/path/to/file'
    cdef FILE* cfile
    cfile = fopen(fname,"rb")

    cdef:
        char * line_0 = NULL
        char * line_1 = NULL
        char * line_2 = NULL
        char * line_3 = NULL
        # One variable for each line pointer
        size_t n_0 = 0
        size_t n_1 = 0
        size_t n_2 = 0
        size_t n_3 = 0
        ssize_t length_line
        unsigned long long line_nb = 0

    while True:
        # Reading the same file (same cfile),but line_x and n_x by pairs)
        length_line = getline(&line_0,&n_0,cfile)  
        if length_line < 0: break
        
        length_line = getline(&line_1,&n_1,cfile)
        if length_line < 0: break
        
        length_line = getline(&line_2,&n_2,cfile)
        if length_line < 0: break
        
        length_line = getline(&line_3,&n_3,cfile)
        if length_line < 0: break

        line_nb += 4
        if line_nb > early_stop:
            break

    fclose(cfile)
    return line_nb

fastq_reader(early_stop=20000)

感谢您指出我的错误。

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。