如何一次读多行?(练习稿)

2024-10-04 01:32:31 发布

您现在位置:Python中文网/ 问答频道 /正文

我正在制作一个.fasta解析器脚本。(我知道一些.fasta文件的解析器已经存在,但我需要练习处理大型文件,并认为这是一个好的开始)

该程序的目标是:获取一个包含多个序列的非常大的.fasta文件,并在一个新文件中返回每个序列的反向补码

我有一个脚本,一次读取一行,但在一个普通的.fasta文件中,每行只有大约50字节。因此,一次只缓冲一行是不必要的。是否有一种方法可以设置一次要缓冲的行数

对于不知道fasta是什么的人来说:.fasta文件是一个文本文件,包含DNA序列,每个序列在DNA/RNA/蛋白质序列前面有一个标题行,前面用“>;”标记。例如:

>Sequence_1
ATG
TATA
>Sequence_2
TATA
GACT
ATG

我的代码概述:

通过查找“>;”,读取每行的第一个字节以映射文件中序列的位置美国

使用此映射逐行向后读取每个序列(这是我想要更改的)

  • 逆转
  • 补充字符串中的碱基对(例如G->;C)
  • 将此行写入新文件

下面是所有相关代码的内容,以便您可以看到我试图更改的行正在调用的函数:(可能可以跳过)

def get_fasta_seqs(BIGFILE): #Returns returns an object containing all of seq locations
  f = open(BIGFILE)
  cnt = 0
  seq_name = []
  seq_start = []
  seq_end = []
  seqcount = 0
  #print(line)
  #for loop skips first line check for seq name
  if f.readline(1) == '>':
    seq_name.append(cnt)
    seq_start.append(cnt+1)
    seqcount =+ 1
  for line in f:
    cnt += 1
    if f.readline(1) == '>':
      seq_name.append(cnt)
      seq_start.append(cnt+1)
      seqcount += 1
      if seqcount > 1:
        seq_end.append(cnt-1)
  seq_end.append(cnt-1) #add location of final line

  seqs = fileseq(seq_name,seq_start,seq_end,seqcount) #This class only has a __init__ function for these lists
  return seqs

def fasta_rev_compliment(fasta_read,fasta_write = "default",NTtype = "DNA"):
  if fasta_write == 'default':
    fasta_write = fasta_read[:-6] + "_RC.fasta"
  seq_map = get_fasta_seqs(fasta_read)
  print(seq_map.seq_name)
  f = open(fasta_write,'a')
  for i in range(seq_map.seqcount): #THIS IS WHAT I WANT TO CHANGE
    line = getline(fasta_read,seq_map.seq_name[i]+1) #getline is reading it as 1 indexed?
    f.write(line)
    my_fasta_seqs = get_fasta_seqs(fasta_read)
    for seqline in reversed(range(seq_map.seq_start[i],seq_map.seq_end[i]+1)):
      seq = getline(fasta_read,seqline+1)
      seq = seq.replace('\n','')
      seq = reverse_compliment(seq,NTtype = NTtype) #this function just returns the reverse compliment for that line.
      seq = seq + '\n'
      f.write(seq)
  f.close()

fasta_rev_compliment('BIGFILE.fasta')

我想更改的主要代码如下:

for i in range(seq_map.seqcount): #THIS IS WHAT I WANT TO CHANGE
  line = getline(fasta_read,seq_map.seq_name[i]+1) #getline is reading it as 1 indexed?
  f.write(line)
  my_fasta_seqs = get_fasta_seqs(fasta_read)
  for seqline in reversed(range(seq_map.seq_start[i],seq_map.seq_end[i]+1)):
    seq = getline(fasta_read,seqline+1)

我想要这样的东西:

def fasta_rev_compliment(fasta_read,fasta_write = "default",NTtype = "DNA",lines_to_record_before_flushing = 5):
  ###MORE CODE###
  #i want something like this

  for i in range(seq_map.seqcount): #THIS IS WHAT I WANT TO CHANGE
    #is their a way to load
    line = getline(fasta_read,seq_map.seq_name[i]+1) #getline is reading it as 1 indexed?
    f.write(line)
    my_fasta_seqs = get_fasta_seqs(fasta_read)
    for seqline in reversed(range(seq_map.seq_start[i],seq_map.seq_end[i]+1)):
      seq = getline(fasta_read,seqline+1)
    #Repeat n = 5 (or other specified number) times until flushing ram.

我遇到的问题是,我需要反向读取文件。当您试图将其应用于向后读取文件时,我能找到的所有方法都不能很好地工作。有没有什么东西可以把一个文件分块向后读

或者:对于低内存设置,任何其他可以使其更有效的方法。目前,它几乎不使用任何内存,但对于一个100kB、大约12000行的文件,需要21秒,但使用file.readlines()方法立即处理该文件


Tags: 文件namemapforreadline序列start
1条回答
网友
1楼 · 发布于 2024-10-04 01:32:31

下面是获取fasta文件的反向补码的示例。也许你可以从中得到一些想法

import re

file = """\
>Sequence_1  
ATG
TATA
>Sequence_2 
TATA
GACT
ATG""".splitlines()

s = ''
for line in file:
    line = line.rstrip()
    if line.startswith('>'):
        if len(s):
            # complement the sequence of fasta 'TAGC' to 'ATCG'
            # T to A, A to T, G to C, C to G
            s = s.translate(str.maketrans('TAGC', 'ATCG'))
            # reverse the string, 's[::-1]'
            # Also, print up to 50 fasta per line to the end of the sequence
            s = re.sub(r'(.{1,50})', r'\1\n', s[::-1])
            print(s, end='')
            s = ''
        print(line)
    else:
        s += line

# print last sequence
s = s.translate(str.maketrans('TAGC', 'ATCG'))
s = re.sub(r'(.{1,50})', r'\1\n', s[::-1])
print(s, end='')

印刷品:

>Sequence_1  
TATACAT
>Sequence_2 
CATAGTCTATA

相关问题 更多 >