[英]convert a fasta file to a tab-delimited file using python script
我是一名学生,目前正在学习如何用python编写脚本。 我一直在做以下练习。 我必须以以下格式转换fasta文件:
>header 1
AATCTGTGTGATAT
ATATA
AT
>header 2
AATCCTCT
到这个:
>header 1 AATCTGTGTGATATATATAAT
>header 2 AATCCTCT
我在摆脱空白方面遇到一些困难(使用line.strip()?)任何帮助将不胜感激...
这将基于>
字符创建一个新字符串,并将该字符串合并到下一个>
为止。 然后将其追加到运行列表。
# open file and iterate through the lines, composing each single line as we go
out_lines = []
temp_line = ''
with open('path/to/file','r') as fp:
for line in fp:
if line.startswith('>'):
out_lines.append(temp_line)
temp_line = line.strip() + '\t'
else:
temp_line += line.strip()
with open('path/to/new_file', 'w') as fp_out:
fp_out.write('\n'.join(out_lines))
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.