我撰寫了一個函式來遍歷多個 DNA 序列串列并將它們的同源性繪制為相同堿基含量的一部分,這樣人們就可以很容易地看到跨多個物種的高同源性區域與低同源性區域。
為此,我從對齊檔案(在本例中為 clustal)傳遞了一個由 m 序列串列組成的 numpy 陣列:
[['-' '-' '-' ... 'C' 'C' 'T']
['-' '-' '-' ... '-' '-' '-']
['-' '-' '-' ... '-' '-' '-']
['G' 'C' 'T' ... '-' '-' '-']]
然后我遍歷每個串列并跟蹤每列的值。如果它們是 ATC 或 G,我將每列的值添加到字典中,并忽略所有其他情況(N 或 -)。不關心哪個堿基對最常見,然后我通過以下函式獲取字典的最大值并將 max/m-species 存盤在單獨的串列中:
def createHists(SequenceArray):
Conserved = []
Ind = 0
while Ind < len(SequenceArray[0]):
NucCounts = {"A":0, "T":0, "C":0, "G":0}
ColumnConservation = []
for Seqs in SequenceArray:
ColumnConservation.append(Seqs[Ind])
for Nucs in ColumnConservation:
if Nucs in NucCounts:
NucCounts[Nucs] = 1
if NucCounts[max(NucCounts, key=NucCounts.get)] > 1:
ConservedN = NucCounts[max(NucCounts, key=NucCounts.get)]/len(ColumnConservation)
Conserved.append(ConservedN)
else:
ConservedN = 0
Conserved.append(ConservedN)
Ind = 1
return(Conserved)
輸出很簡單:
[0.75, 0.5, 0.75, 0.5, 0.5, 0.5, 0.75, 0.75, 0.5, 0.75, 0.5, 0.5, 0.75, 0.75]
我的問題是,鑒于我正在迭代每一行,有沒有辦法讓它更快?并不是說它已經不快了(我的對齊檔案的當前大小是 45000bp),但我想知道是否有內置庫可以更有效地并行化多個串列遍歷,例如 itertools。這里的主要警告是序列串列的數量是未知的,在這個例子中可能是 4,但可能更多。
uj5u.com熱心網友回復:
我想你可能有點想多了這個問題。
from collections import Counter
import numpy as np
VALID_BASES = ['A', 'T', 'G', 'C']
# Array where rows are samples and columns are split from a string
arr = np.array([['-', '-', '-', 'C', 'C', 'T'],
['-', '-', '-', '-', '-', '-'],
['-', '-', '-', '-', '-', '-'],
['G', 'C', 'T', '-', '-', '-']])
使用 python 計數器:
counts = [Counter(x) for x in arr.T]
然后取你的有效字符的最大比例:
max_proportion = [max(count[v] / arr.shape[0] for v in VALID_BASES) for count in counts]
>>> max_proportion
[0.25, 0.25, 0.25, 0.25, 0.25, 0.25]
轉載請註明出處,本文鏈接:https://www.uj5u.com/net/410469.html
標籤:
