4
4

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

Pythonで塩基配列を相補鎖に変換[3パターン]

Posted at

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

4
4
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
4
4

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?