2017-07-28 46 views
0

我是一個初學者在python,目前正在執行一段代碼,但它會引發以下錯誤。我盡我所能解決但無法做到這一點。請幫幫我。代碼如下。索引錯誤,而執行python

continue 
    num_snps_skipped += 1 
    samp_id = sline[id_index] 
    ref_allele = sline[ref_allele_index] 
    tum_allele = sline[tum_allele_index] 
    snp = ref_allele + tum_allele 
    if not snp in transitions: 
     snp = nucleotide_complement[ref_allele] + nucleotide_complement[tum_allele] 
    ref_trinuc = sline[ref_tri_index] 
    if ref_trinuc == "NA": 
     print "Warning: Reference allele not available on "+\ 
       "line %d; skipping line"%line_number 
     continue 
    if not ref_trinuc[1] == snp[0]: 
     print "Warning: Reference allele does not match reference "+\ 
       "trinucleotide; skipping line %d"%line_number** 
     continue 
    snp_with_ctx = ref_trinuc[0] + snp + ref_trinuc[2] 
    if not samp_id in signatures: 
     signatures[samp_id] = [0 for i in substitution_order] 
    if snp_with_ctx not in substitution_order: 
     print "Warning: substitution on line " + \ 
       "%d is %s, not "%(line_number,snp_with_ctx) + \ 
       "found among possible substitutions. Skipping line." 

我得到的錯誤如下

Traceback (most recent call last): 
    File "main.py", line 59, in <module> 
    signatures = signature.make(args.in_file, substitution_order=stratton['substitution_order'], out_path = args.spectrum_output) 
    File "/home/ateeqanees/Mutation/centos/mutation-signatures-master/signature.py", line 73, in make 
    if not ref_trinuc[1] == snp[0]: 
IndexError: string index out of range 

請幫助我。這將有很大的幫助。

非常感謝! Dav

+1

嘗試'打印'ref_trinuc'和'snp',其中一個沒有你期待的那麼長。 – AChampion

+0

這意味着你正試圖訪問一個不存在的列表元素。如上所述,打印該值或瞭解python調試器pdb。您可以通過將pdb.set_trace放在您的第73行和步驟來檢查值。 – user1427026

回答

0

很可能你的ref_trinuc字符串有一個或零個字符。這就是爲什麼if not ref_trinuc[1] == snp[0]:提供索引錯誤,因爲它試圖抓住第二個字符。

在該行之前,請嘗試打印ref_trinuc以查看它的含義。

如果是正常的ref_trinuc有時短於2個字符,嘗試這個if語句:

if (len(ref_trinuc) > 1) and (not ref_trinuc[1] == snp[0]):

由於如果長度是1或0,在這種情況下,第二半不會企圖並且不會引發索引錯誤。