Upgrade to Pro
— share decks privately, control downloads, hide ads and more …
Speaker Deck
Features
Speaker Deck
PRO
Sign in
Sign up for free
Search
Search
【第3回】ゼロから始めるゲノム解析(Python編)
Search
nkimoto
November 26, 2021
Programming
0
430
【第3回】ゼロから始めるゲノム解析(Python編)
2021/11/26 (金) 【第3回】ゼロから始めるゲノム解析(Python編) 資料
nkimoto
November 26, 2021
Tweet
Share
More Decks by nkimoto
See All by nkimoto
Location Restriction Sites: Using, Testing, and Sharing Code
nkimoto
0
330
Finding a Protein Motif: Fetching Data and Using Regular Expressions
nkimoto
0
300
Overlap Graphs: Sequence Assembly Using Shared K-mers
nkimoto
0
190
Computing GC Content: Parsing FASTA and Analyzing Sequences
nkimoto
0
280
【第5回】ゼロから始めるゲノム解析(Python編)
nkimoto
0
670
【第1回】ゼロから始めるゲノム解析(Python編).pdf
nkimoto
0
850
【第7回】ゼロから始めるゲノム解析.pdf
nkimoto
0
460
【第5回】ゼロから始めるゲノム解析(R編)
nkimoto
0
560
【第3回】ゼロから始めるゲノム解析(R編)
nkimoto
0
1.6k
Other Decks in Programming
See All in Programming
Introducing ReActionView: A new ActionView-compatible ERB Engine @ Rails World 2025, Amsterdam
marcoroth
0
640
CloudflareのChat Agent Starter Kitで簡単!AIチャットボット構築
syumai
2
470
ProxyによるWindow間RPC機構の構築
syumai
3
1.1k
CJK and Unicode From a PHP Committer
youkidearitai
PRO
0
110
Tool Catalog Agent for Bedrock AgentCore Gateway
licux
6
2.3k
AIを活用し、今後に備えるための技術知識 / Basic Knowledge to Utilize AI
kishida
21
5.6k
請來的 AI Agent 同事們在寫程式時,怎麼用 pytest 去除各種幻想與盲點
keitheis
0
110
Navigating Dependency Injection with Metro
zacsweers
3
230
MCPでVibe Working。そして、結局はContext Eng(略)/ Working with Vibe on MCP And Context Eng
rkaga
5
2.2k
Ruby×iOSアプリ開発 ~共に歩んだエコシステムの物語~
temoki
0
270
Design Foundational Data Engineering Observability
sucitw
3
190
テストコードはもう書かない:JetBrains AI Assistantに委ねる非同期処理のテスト自動設計・生成
makun
0
250
Featured
See All Featured
Keith and Marios Guide to Fast Websites
keithpitt
411
22k
A better future with KSS
kneath
239
17k
Performance Is Good for Brains [We Love Speed 2024]
tammyeverts
12
1.1k
The Cult of Friendly URLs
andyhume
79
6.6k
The Language of Interfaces
destraynor
161
25k
Save Time (by Creating Custom Rails Generators)
garrettdimon
PRO
32
1.5k
Optimizing for Happiness
mojombo
379
70k
Reflections from 52 weeks, 52 projects
jeffersonlam
352
21k
How to Ace a Technical Interview
jacobian
279
23k
Chrome DevTools: State of the Union 2024 - Debugging React & Beyond
addyosmani
7
840
10 Git Anti Patterns You Should be Aware of
lemiorhan
PRO
656
61k
What’s in a name? Adding method to the madness
productmarketing
PRO
23
3.7k
Transcript
【第3回】ゼロから始めるゲノム解析 (Python編) Reverse Complement of DNA: String Manipulation @kimoton
本勉強会の概要・目的 書籍名 対象者/目的 Mastering Python for Bioinformatics Python・バイオインフォ知識ほぼゼロの人 を対象に、正しいPythonのコーディング手 法について学ぶ
頻度 毎週〜隔週開催予定 登壇者 募集中!
Rosalindとは • 問題解決を通じてバイオインフォマティク ス、プログラミング、およびアルゴリズムを 学習するためのプラットフォーム • 大学やハッカソン、就職の面接にも 600回 以上の採用実績あり 参考:https://qiita.com/_kimoton/items/d534d0fa9b83dd7dc412
概要
環境構築 - 必要パッケージ群のインストール # 公開されているレポジトリからファイル群を取得 $ 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
本日のお題 与えられたDNA配列の相補鎖を作成せよ http://rosalind.info/problems/revc/
本日学ぶこと • 動的な文字列生成の方法 • reversed() 関数の使い方 • リスト内包表記の使い方 • str.maketrans()
及び str.translate() の使い方 • Biopythonの Bio.Seqモジュールの使い方
解法編
逆相補鎖の作成 Step1 文字列を反転 Step2 相補鎖の生成 今回は、文字列の反転→相補鎖の生成という手順をとる
文字列を反転(リストのindexを使用) >>> dna = 'AAAACCCGGT' >>> dna[:2] 'AA' >>> dna[-2:]
'GT' 終了位置を指定しないと、最終文字列までの指定になる 開始位置を指定しないと、最初文字列からの指定になる >>> dna[::-1] 'TGGCCCAAAA' 第3の引数としてstep_sizeを指定可能。-1を指定すると、反転方向に並び替えることができる
文字列を反転(reversed() 関数を使用) >>> reversed(dna) <reversed object at 0x7ff4e5c2bf70> イテレータから取り出した反転文字列を空文字列を結合文字に用いて結合 reversed()
関数はイテレータを返す >>> ''.join(dna) 'TGGCCCAAAA' イテレータとは for 文などで要素を1つずつ取り出せるオ ブジェクトを指す
文字列を反転(forループを使用) >>> rev = '' >>> for base in reversed(dna):
... rev += base ... >>> rev 'TGGCCCAAAA' 1 2 3 2 反転文字列のイテレータから1文字 ずつbaseに格納 3 rev変数に追加 1 相補鎖を格納するrev変数を初期化
文字列を反転 → 相補鎖の生成 revc = '' for base in reversed(dna):
if base == 'A': revc += 'T' elif base == 'T': revc += 'A' elif base == 'G': revc += 'C' elif base == 'C': revc += 'G' elif base == 'a': revc += 't' elif base == 't': revc += 'a' elif base == 'g': revc += 'c' elif base == 'c': revc += 'g' else: revc += base 1 相補鎖を格納するrevc変数を初期化 2 反転文字列のイテレータから1文字 ずつbaseに格納 3 大文字/小文字の塩基に該当すれば相 補的な塩基をrevc変数に追加 1 2 3
解法 1 .forループとif/elifによる分岐を使用 • 塩基配列のlookupにif/elif/elseではなく辞書 型を使う • for文ではなくリスト内包表記を使用する • str.translate()
メソッドを使用する • Bio.Seq オブジェクトと付随する相補鎖生成 のためのメソッドを使用する 改善案
改善編
解法 2 辞書型を用いたlookup 1 相補鎖としてどの塩基を返すかを示した 辞書 2 3 2 相補鎖を格納するcomplement変数を初
期化 3 引数で与えたDNA文字列を1文字ずつ baseに格納 1 4 5 4 baseを相補鎖に変換した塩基を complement変数に追加 5 complement変数の文字列を反転した結 果を出力
リスト内包表記 for base in args.dna: trans.get(base, base) [trans.get(base, base) for
base in arg.dna] リスト内包表記を用いるとリスト内の要素に特定の処理を行った結果を一行で取得する ことができる。
解法 3 forループの代わりにリスト内包表記を使用 1 forループをリスト内包表記で置き換え 1
str.translate() メソッド >>> trans = { ... 'A': 'T', 'C':
'G', 'G': 'C', 'T': 'A', ... 'a': 't', 'c': 'g', 'g': 'c', 't': 'a' ... } >>> str.maketrans(trans) {65: 'T', 67: 'G', 71: 'C', 84: 'A', 97: 't', 99: 'g', 103: 'c', 116: 'a'} >>> 'AAAACCCGGT'.translate(str.maketrans(trans)) 'TTTTGGGCCA' str.maketrans() メソッドはkeyがASCIIコードとなった辞書型を作成する str.translate() メソッドはkeyがASCIIコードとなった辞書型を引数にとり、文字列を変換する # 文字 → ASCIIコード >>> ord('A') 65 # ASCIIコード → 文字 >>> chr(65) 'A'
(参考)ASCIIコード表
解法 4 str.translate() メソッドを使用 1 str.translate()メソッドに 必要な変換テーブルを作成 2 transテーブルを用いてDNA文 字列を補完
2 1
Bio.Seqクラス $ python3 -m pip install biopython biopythonパッケージのインストール >>> from
Bio import Seq Seqクラスをimport >>> Seq.reverse_complement('AAAACCCGGT') 'ACCGGGTTTT' Seq.reverse_complement() メソッドを使用して変換
解法 5 Bio.Seqクラスを使用 1 Seq.reverse_complement メソッドを使用して変換 1
(参考)ベンチマーキング 100万塩基長の配列を生成 demo >>> with open("sample.dna.large", "w") as wf: ...
wf.write(''.join([random.choice('ATGC') for i in range(10000000)])) $ for py in ./solution*; echo $py && time $py sample.dna.large ; end 100万塩基長の配列に対して各手法を適用し、 timeコマンドにより実行時間を測定
1 2 3 4 5 1 2 3 4 5
Bio.Seqクラスを用いた 実行時間 forループとif/elifを 用いた実行時間 str.translate() メ ソッドを用いた実行時間 リスト内包表記を使用し た実行時間 辞書型のlookupを用いた 実行時間
本日学んだこと • if/elseを用いる/辞書を用いたlookupにより決定木を実装できる • 文字列とリストはいずれもforループでイテレーションできる、+=オペレータで追加できる 点で似ている • reversed() 関数はイテレータを返す遅延評価関数 •
str.maketrans() 及び str.translate() を使うと文字列の置換を効率的に行うことができ る • Biopythonはバイオインフォマティクスに利用される関数群が含まれたパッケージ • Bio.Seq.reverse_complementメソッドを用いると相補鎖を作成することができる