Upgrade to Pro
— share decks privately, control downloads, hide ads and more …
Speaker Deck
Sign up for free
Menu
Search
Features
All features
Private URLs
Password Protection
Custom URLS
Scheduled publishing
Remove Branding
Restrict embedding
Deck Collections
Notes
Features
All features
Private URLs
Password Protection
Custom URLS
Scheduled publishing
Remove Branding
Restrict embedding
Deck Collections
Notes
Explore
Featured decks
Featured speakers
Programming
Technology
Storyboards
Explore
Featured decks
Featured speakers
Programming
Technology
Storyboards
Pricing
Search
Sign in
Sign up for free
Finding the Hamming Distance: Counting Point Mu...
Search
Sponsored
·
Ship Features Fearlessly
Turn features on and off without deploys. Used by thousands of Ruby developers.
→
onouyek
January 07, 2022
Programming
470
0
Share
Embed
Copy iframe code
Copy JS code
Copy link
Start on current slide
Finding the Hamming Distance: Counting Point Mutations
【第6回】ゼロから始めるゲノム解析(Python編)
onouyek
January 07, 2022
More Decks by onouyek
See All by onouyek
Finding Open Reading Frames
onouyek
0
400
Inferring mRNA from Protein: Products and Reductions of Lists
onouyek
0
390
Finding the Longest Shared Subsequence: Finding K-mers, Writing Functions, and Using Binary Search
onouyek
0
310
Find a Motif in DNA: Exploring Sequence Similarity
onouyek
0
360
Creating the Fibonacci Sequence: Writing, Testing, and Benchmarking Algorithms
onouyek
1
510
Transcribing DNA into mRNA: Mutating Strings, Reading and Writing Files
onouyek
0
560
DNA methylation analysis using bisulfite sequencing data
onouyek
0
930
Operations on Genomic Intervals and Genome Arithmetic
onouyek
0
370
Exploratory Data Analysis with Unsupervised Machine Learning
onouyek
0
330
Other Decks in Programming
See All in Programming
JRuby: Past, Present, and Future
headius
0
220
RAG の “R” を Swift で覗いてみる 〜「意味から探す」検索の仕組み〜
nao_randd
0
120
SREの越境 / SRE Collaboration
y0hgi
2
300
wkhtmltopdfの次どうするか問題2026
willnet
2
1.9k
なぜCTOを降りてFDEを選んだのか?〜なぜプロダクト企業がFDEで顧客の現場に踏み込むのか〜
gonta
1
160
Augmenting AI with the Power of Jakarta EE
ivargrimstad
0
490
月間100万人のサービス刷新で、 技術選定をHonoに振り切った話
yutobara
5
4.6k
パーソナライズPodcastを作った話
repunit11
0
130
Verilogで学ぶCPU自作入門
uyuki234
8
4k
Augmenting AI with the Power of Jakarta EE
ivargrimstad
0
570
AWSに止められる覚悟してますか?
morizo_1984
2
520
すこし踏み込む CancellationToken
htkym
2
1.6k
Featured
See All Featured
Test your architecture with Archunit
thirion
2
2.4k
Building Better People: How to give real-time feedback that sticks.
wjessup
370
20k
[RailsConf 2023 Opening Keynote] The Magic of Rails
eileencodes
31
10k
We Are The Robots
honzajavorek
0
390
Marketing to machines
jonoalderson
1
5.9k
HU Berlin: Industrial-Strength Natural Language Processing with spaCy and Prodigy
inesmontani
PRO
0
740
Un-Boring Meetings
codingconduct
0
440
A Tale of Four Properties
chriscoyier
163
24k
Fireside Chat
paigeccino
43
4.1k
More Than Pixels: Becoming A User Experience Designer
marktimemedia
3
550
How To Speak Unicorn (iThemes Webinar)
marktimemedia
1
590
Conquering PDFs: document understanding beyond plain text
inesmontani
PRO
4
3.2k
Transcript
【第6回】ゼロから始めるゲノム解析 (Python編) Finding the Hamming Distance: Counting Point Mutations @onouyek
本勉強会の概要・目的 書籍名 対象者/目的 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
本日のお題 http://rosalind.info/problems/hamm/ DNA配列sとtのハミング距離を求めよ
前提知識編
ハミング距離 文字列sからtに変換するのに必要な編集(置換)の数 AAACCCGGGTTT || | CGACGATATGTC AAACCCGGGTTT ||||||||||| AACCCGGGTTTA 例1:sとtで共通のものは12塩基のうち3塩基なのでハミング距離は
9 例2:sとtの配列はほぼ共通( 92%)だが開始位置から比べるのでハミング距離は 4 AAACCCGGGTTT || || || || AACCCGGGTTTA s t s t s t
位置引数の複数指定 位置引数は順番があるのでargparseで指定した順番にして渡す $ ./hamm.py -h usage: hamm.py [-h] str str
Hamming distance positional arguments: str Sequence 1 str Sequence 2 optional arguments: -h, --help show this help message and exit
abs():絶対値を求める >>> seq1, seq2 = 'AC', 'ACGT' >>> len(seq1) -
len(seq2) -2 2つの配列が異なる場合、差をとると負になる場合がある >>> distance = abs(len(seq1) - len(seq2)) 2 abs()で絶対値を求めることができる
min():最小値を求める range()の引数に求めた最小値を渡せばインデックスを使って文字を比較できる >>> seq1, seq2 = 'AC', 'ACGT' >>> min(len(seq1),
len(seq2)) 2 2つの配列を比べる場合、短い方の長さだけ調べれば良い min()で最小値が求められる >>> for i in range(min(len(seq1), len(seq2))): ... print(seq1[i], seq2[i]) ... A A C C
解法編
Solution 1: min() ①seq1とseq2の長さの差の絶対値をdistanceに代入 ②seq1とseq2の短い方の長さでイテレーション ③seq1とseq2の同じ位置の文字を比較して異なればdistanceをインクリメント ① ② ③
Solution 2: zip() min()で短い方の長さを求めなくても、zip()で短い方に合わせて2つの リストからそれぞれの要素を取り出せる >>> list(zip('AC', 'ACGT')) [('A', 'A'),
('C', 'C')]
Solution 3: zip_longest() zip_longest()で長い方に合わせて2つのリストからそれぞれの要素を取り出せる 短い方は要素がなくなるとNoneが返されるのでabs()で初期値を求める必要がなくなる >>> from itertools import zip_longest
>>> list(zip_longest('AC', 'ACGT')) [('A', 'A'), ('C', 'C'), (None, 'G'), (None, 'T')]
Solution 4: リスト内包表記 リスト内包表記で1行で書くことができる >>> seq1, seq2 = 'GAGCCTACTAACGGGAT', 'CATCGTAATGACGGCCT'
>>> [1 if c1 != c2 else 0 for c1, c2 in zip_longest(seq1, seq2)] [1, 0, 1, 0, 1, 0, 0, 1, 0, 1, 0, 0, 0, 0, 1, 1, 0] demo
Solution 5: filter() filter()とlambda関数を使って2つの配列の異なる要素のイテレーターが得られる >>> seq1, seq2 = 'AC', 'ACGT'
>>> list(filter(lambda t: t[0] != t[1], zip_longest(seq1, seq2))) [(None, 'G'), (None, 'T')]
Solution 6: map() map()とlambda関数を使って2つの配列の要素を比較したbool値のイテレーターが得られる map(lambda t: t[0] != t[1], zip_longest('AC',
'ACGT'))) map(lambda t: t[0] != t[1], [('A', 'A'), ('C', 'C'), (None, 'G'), (None, 'T')]) lambda('A', 'A'): 'A' != 'A' -> False lambda('C', 'C'): 'C' != 'C' -> False lambda(None, 'G'): None != 'G' -> True lambda(None, 'T'): None != 'T' -> True
Solution 7: starmap() starmap()を使うことで、関数にタプルを展開して渡してくれる def not_same(t): return t[0] != t[1]
starmap(not_same, zip_longest('AC', 'ACGT')) starmap(lambda a, b: a != b, [('A', 'A'), ('C', 'C'), (None, 'G'), (None, 'T')]) *('A', 'A') lambda 'A', 'A': 'A' != 'A' -> False demo
operatorモジュール:関数形式の標準演算子 >>> import operator >>> operator.ne('A', 'A') False >>> operator.ne('A',
'T') True >>> 1 + 2 3 >>> operator.add(1, 2) 3 >>> 'AC' + 'GT' 'ACGT' >>> operator.concat('AC', 'GT') 'ACGT'
追加課題 プログラムを拡張して、2つ以上の入力配列を扱えるようにしてください。各配列のペア 間のハミング距離を表示するようにしてください。
本日学んだこと • abs() • min() • zip() • zip_longest() •
filter() • map() • starmap() • operatorモジュール