【发布时间】:2017-07-25 21:57:09
【问题描述】:
我是 Python 的初学者,我正在尝试各种方法来完成反向互补 DNA 或 RNA 序列以学习一些字符串函数等的简单任务。我的最新方法几乎可以工作,但对于我的一个小刺激找不到答案,可能是因为我正在使用一些我不正确理解的东西。 我的函数旨在编写一个空白文件(这有效!),然后打开一个包含序列的文件,一次循环一个字符,将其反向补码写入新文件。代码如下:
def func_rev_seq(in_path,out_path):
"""
Read file one character at a time and retrun the reverse complement of each nucleotide to a new file
"""
# Write a blank file (out_path)
fb = open(out_path,"w")
fb.write("")
fb.close()
# Dictionary where the key is the nucleotide and the value is its reverse complement
base = {"A":"T", "C":"G", "G":"C", "T":"A", "a":"t", "c":"g", "g":"c", "t":"a", "k":"m", "m":"k", "y":"r", "r":"y", "b":"v", "v":"b", "d":"h", "h":"d", "K":"M", "M":"K", "Y":"R", "R":"Y", "B":"V", "V":"B", "D":"H", "H":"D", "U":"A", "u":"a"}
# Open the source file (in_path) as fi
fi=open(in_path,"r")
i = fi.read(1)
# Loop through the source file one character at a time and write the reverse complement to the output file
while i != "":
i = fi.read(1)
if i in base:
b = base[i]
else:
b = i
with open(out_path, 'r+') as fo:
body = fo.read()
fo.seek(0, 0)
fo.write(b + body)
fi.close()
fo.close()
问题是当我运行该函数时,输出文件中的字符串首先被单个字符截断,其次是在我不想要的空行下方。 screen shot of input and output file examples 据我了解,带有 (0, 0) 的 seek 函数应该是指文件的开头,但我可能误解了。 非常感谢任何帮助,谢谢!
【问题讨论】:
-
顺便说一下,我的代码已经正确缩进,但在这里没有正确呈现,也许我也做错了!
-
空行下面的字符是什么?
-
它是原始序列中最后一个核苷酸的反向互补。因此,例如,如果原始序列是“AACCTCAGC”,那么它将是“G”。