我正在嘗試爲序列中的每個核苷酸(A,G,C,T)創建一個列表,其中列表的索引對應於序列中的位置,值在所有序列是核苷酸的頻率,這裏有4個序列爲例:在特定索引處遞增列表的值python
>ignore this
GTAGGGCGA
>ignore this
GTATACAGC
>ignore this
GTTTCTCTT
>ignore this
GTAATCAAA
我寫的代碼:
def function(filename, length):
g,t,c,a = [],[],[],[]
with open(filename, "r") as f:
for line in f:
if line.startswith('GT'):
gcount, acount, tcount, ccount = 0, 0, 0, 0
g = [gcount + 1 if nuc == 'G' else gcount for nuc in line[:length]]
return g
眼下,這個代碼只是着眼於G核苷酸我得到了每個序列的列表,而不是1個列表,它們列出了每個索引處的值噸。
[1, 0, 0, 1, 1, 1, 0, 1, 0]
[1, 0, 0, 0, 0, 0, 0, 1, 0]
[1, 0, 0, 0, 0, 0, 0, 0, 0]
[1, 0, 0, 0, 0, 0, 0, 0, 0]
我想什麼是我的輸出爲單獨G:
[4, 0, 0, 1, 1, 1, 0, 2, 0]
我一直對numpy是什麼以及它如何適用於我的代碼感興趣。謝謝!然而,這個例子只顯示了4個序列,文件中可能有數千個序列。我可以修改這個代碼來解釋這個嗎? – Ouwan12
@Ouwan當然。實際上,我已經通過修改'function'函數爲您提供了這樣的實現。 –
當我從這些例子中測試的文件切換到不同長度的序列時,我得到ValueError:操作數無法與形狀(1,9)(5,)一起廣播? – Ouwan12