PythonでDNA、RNA配列を相補鎖に変換
状況によって使い分けれる3パターンのまとめ
best_choice.py
# 一番お手軽、biopythonを使う
# DNAでもRNAでも使える
from Bio.Seq import Seq
seq='ATGCatgc--Nn'
seq_c=str(Seq(seq).reverse_complement())
print(seq_c)
# nN--gcatGCAT
use_string_module.py
# あえてbiopythonを使わない
import string
# DNA
seq='ATGCatgc--Nn'
seq_c=seq.translate(str.maketrans('ATGCatgc', 'TACGtagc'))[::-1] # python3
print(seq_c)
# nN--gcatGCAT
# RNAの場合
seq_c=seq.translate(str.maketrans('AUGCaugc', 'UACGuagc'))[::-1] # python3
DIY.py
# どうしても自分でやりたい時
# DNA
def complement_dna(string):
comp=''
for char in string:
if char == 'A': comp += 'T'
elif char == 'T': comp += 'A'
elif char == 'G': comp += 'C'
elif char == 'C': comp += 'G'
elif char == 'a': comp += 't'
elif char == 't': comp += 'a'
elif char == 'g': comp += 'c'
elif char == 'c': comp += 'g'
else: comp += char
return comp[::-1]
seq='ATGCatgc--Nn'
seq_c=complement_dna(seq)
print(seq_c)
# nN--gcatGCAT
# RNAの場合
def complement_rna(string):
comp=''
for char in string:
if char == 'A': comp += 'U'
elif char == 'U': comp += 'A'
elif char == 'G': comp += 'C'
elif char == 'C': comp += 'G'
elif char == 'a': comp += 'u'
elif char == 'u': comp += 'a'
elif char == 'g': comp += 'c'
elif char == 'c': comp += 'g'
else: comp += char
return comp[::-1]
環境
Python 3.7.1
biopython 1.72