Upgrade to Pro — share decks privately, control downloads, hide ads and more …

Find a Motif in DNA: Exploring Sequence Similarity

onouyek
January 21, 2022

Find a Motif in DNA: Exploring Sequence Similarity

onouyek

January 21, 2022
Tweet

More Decks by onouyek

Other Decks in Programming

Transcript

  1. 環境構築 - 必要パッケージ群のインストール # 公開されているレポジトリからファイル群を取得 $ git clone https://github.com/kyclark/biofx_python $

    cd biofx_python # requirements.txt に記載のパッケージをインストール $ pip3 install -r requirements.txt # pylintの設定ファイルをホームディレクトリに移動 $ cp pylintrc ~/.pylintrc # mypyの設定ファイルをホームディレクトリに移動 $ cp mypy.ini ~/.mypy.ini
  2. in演算子 >>> seq = 'GATATATGCATATACTT' >>> subseq = 'ATAT' seqとsubseqを定義する

    >>> subseq in seq 1 seqにsubseqが存在するかはinで確認できるが位置情報は分からない
  3. str.index() 第2引数で開始位置を指定できる >>> if subseq in seq: >>> seq.index(subseq) 1

    str.find()と同様にマッチする最小の位置が求められる >>> if subseq in seq[2:]: >>> seq.index(subseq, 2) 3 存在しなければValueErrorが返される >>> seq.index(subseq, 10) ValueError: substring not found
  4. reモジュール >>> import re >>> re.findall(subseq, seq) ['ATAT', 'ATAT'] re.findall()でseqに存在するsubseqを見つけることができる

    >>> list(re.finditer(subseq, seq)) [<re.Match object; span=(1, 5), match='ATAT'>, <re.Match object; span=(9, 13), match='ATAT'>] re.finditer()でseqに存在するsubseqの位置も分かる 1つめのsubseqと重なる開始位置4のsubseqは見つけられない
  5. Solution 2: str.index() >>> ' '.join(found) Traceback (most recent call

    last): File "<input>", line 1, in <module> ' '.join(found) TypeError: sequence item 0: expected str instance, int found ①subseqが見つからなくなったら終了 ②lastを開始位置の+1の位置に更新 ③str.join()を使う場合はstringに変換 ① ② ③
  6. Solution 3: 高階関数 ① lambda n: 0 <= n ②

    lambda n: seq.find(subseq, n) ③ lambda n: n + 1 ① ② ③
  7. Solution 4: k-mers seqからすべてのkmerを作成してsubseqにマッチするものだけに絞り込む >>> kmers = [seq[i:i + k]

    for i in range(len(seq) - k + 1)] >>> kmers ['GATA', 'ATAT', 'TATA', 'ATAT', 'TATG', 'ATGC', 'TGCA', 'GCAT', 'CATA', 'ATAT', 'TATA', 'ATAC', 'TACT', 'ACTT']