[英]IndexError: index 261861 is out of bounds for axis 0 with size 169567
我是編程初學者,遇到以下問題:我想編寫一個定義,在其中創建一個空矩陣,該矩陣使用vcf文件(帶有pyvcf)中所選染色體的數據創建。正確創建了空矩陣。
IndexError:索引261861超出軸0的范圍,大小為169567
但是,如果我嘗試第二條染色體作為輸入,則會發生上述錯誤。 第一條染色體以索引號262860結尾,這就是為什么我認為它想以某種方式將行的數據放置在矩陣的此行中,但是我不明白為什么!
這是我的代碼:
def creatematrix(newfile):
'''Based on the choice of a chromosome, this function fetches the positions and the DP values out of the vcf and saves it in a matrix.'''
vcf_reader = vcf.Reader(open(newfile), 'r')
numpos=0
Chr=input("Chromosome: ")
with open(newfile,'r') as newerfile:
for line in newerfile:
if line.startswith(Chr):
numpos=numpos+1
matrix = np.zeros(shape=(numpos,6)) -> here i am creating the matrix with so much lines as lines are in the vcf for the chosen chromosome (numpos)
z=0
for rec in vcf_reader:
if rec.CHROM==Chr:
matrix[z,0]=rec.CHROM
matrix[z,1]=rec.POS
for samples in rec.samples:
matrix[z,2]=samples['GT']
matrix[z,3]=samples['DP']
z=z+1
我真的希望有人可以幫助我!
問候,Milena
您要為vcf_reader中的每一行加z,而僅在確定了所需染色體的情況下才這樣做。
只需將z = z + 1放入if語句中即可完成以下操作:
z=0
for rec in vcf_reader:
if rec.CHROM==Chr:
matrix[z,0]=rec.CHROM
matrix[z,1]=rec.POS
for samples in rec.samples: # You might want to check this for loop as well. Now you are overwriting matrix[z, 2] and matrix[z, 3] everytime so you only save the last samples?
matrix[z,2]=samples['GT']
matrix[z,3]=samples['DP']
z=z+1 # z now only increases when the matched chromosome is found.
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.