2013-06-04 80 views
2

我試圖計算Rosalind問題的DNA序列的GC含量(%)。我有以下代碼,但它返回0,或者僅返回G的數量或C的數量(無百分比)。用於計算DNA序列中GC含量的初學Python腳本

x = raw_input("Sequence?:").upper() 
total = len(x) 
c = x.count("C") 
g = x.count("G") 

gc_total = g+c 

gc_content = gc_total/total 

print gc_content 

我也試過這個,只是爲了讓G公司和C公司,而不是百分比的計數,但它只是返回整個字符串的計數:

x = raw_input("Sequence?:").upper() 
def gc(n): 
    count = 0 
    for i in n: 
     if i == "C" or "G": 
      count = count + 1 
     else: 
      count = count 
    return count 
gc(x) 

編輯:我定了在第一個代碼示例的print語句中輸入錯字。這不是問題,我只是粘貼了錯誤的代碼片段(有很多嘗試...)

+0

第一個可能是一個錯字,但你說的不是 'gc_content' cg_content「。第二個例子中沒有必要使用else語句。 – squiguy

+0

我修改了它。這不是問題的根源,我只是從我嘗試不同事情的許多嘗試中粘貼了錯誤的代碼塊。 – jstewartmitchel

回答

1

不應:

打印cg_content

閱讀

打印gc_​​content?

至於代碼的其他片段中,你的循環說

,如果我== 「C」 或 「G」:

這是評價 「G」 爲真每時間,從而運行if語句爲真。

相反,它應該讀

,如果我== 「C」 或者我== 「G」:

而且,你不需要那麼else語句。

希望這會有所幫助。讓我們知道怎麼回事。

阿卜杜勒·薩塔爾

+0

是的,工作!我的if語句已關閉。與打印語句中的錯字一樣快,這是我滾動瀏覽上面代碼的所有各種迭代的結果,以粘貼示例來向您展示。非常感謝! – jstewartmitchel

4

你的問題是你正在執行整數除法,而不是浮點除法。

嘗試

gc_content = gc_total/float(total) 
+0

修復它。謝謝!有沒有真正有理由使用整數除法?非常感謝你的幫助。 – jstewartmitchel

0

您還需要通過100乘以答案將其轉換爲百分比。

0
#This works for me. 

import sys 

filename=sys.argv[1] 

fh=open(filename,'r') 

file=fh.read() 
x=file 
c=0 
a=0 
g=0 
t=0 

for x in file: 
    if "C" in x: 
     c+=1  
    elif "G" in x: 
     g+=1 
    elif "A" in x: 
     a+=1  
    elif "T" in x: 
     t+=1 

print "C=%d, G=%d, A=%d, T=%d" %(c,g,a,t) 

gc_content=(g+c)*100/(a+t+g+c) 

print "gc_content= %f" %(gc_content) 
+1

對於源代碼塊和幾個解釋詞來說,這是最佳實踐。 –

0
import sys 
orignfile = sys.argv[1] 
outfile = sys.argv[2] 

sequence = "" 
with open(orignfile, 'r') as f: 
    for line in f: 
     if line.startswith('>'): 
      seq_id = line.rstrip()[0:] 
     else: 
      sequence += line.rstrip() 
GC_content = float((sequence.count('G') + sequence.count('C')))/len(sequence) * 100 
with open(outfile, 'a') as file_out: 
    file_out.write("The GC content of '%s' is\t %.2f%%" % (seq_id, GC_content)) 
+0

請避免使用純代碼的答案。檢查[旅遊]並閱讀[回答] – chtz