题目描述
Problem
In DNA strings, symbols ‘A’ and ‘T’ are complements of each other, as are ‘C’ and ‘G’.
The reverse complement of a DNA string s s s is the string s c s^{\textrm{c}} sc formed by reversing the symbols of s s s, then taking the complement of each symbol (e.g., the reverse complement of “GTCA” is “TGAC”).
Given: A DNA string s s s of length at most 1000 bp.
Return: The reverse complement s c s^{\textrm{c}} sc of s s s.
Sample Dataset
AAAACCCGGT
Sample Output
ACCGGGTTTT
题目大意
输出给定DNA序列的互补序列(均为按5’到3’顺序)
题解
利用Python sting.maketrans()和string.translate()方法,使用maketrans()方法创建字符映射转换表,之后translate()方法映射即可
也可使用BioPython库:使用Bio.Seq.reserve_complement()直接获取反向互补链
参考代码
直接用Python求解
with open("rosalind_revc.txt", "r") as f:
s = f.read()
s = s[::-1]
transtab = s.maketrans("ATCG", "TAGC")
with open("out.txt", "w") as fo:
fo.write(s.translate(transtab))
fo.close()
f.close()
使用BioPython
from Bio.Seq import Seq
from Bio.Alphabet import IUPAC
with open("rosalind_revc.txt", "r") as f:
s = f.read()
my_seq = Seq(s, IUPAC.unambiguous_dna)
ans = my_seq.reverse_complement()
with open("out_bp.txt", "w") as fo:
fo.write(str(ans))
fo.close()
f.close()