IndexError: index 261861 is out of bounds for axis 0 with size 169567
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
我真的希望有人能帮我解决这个问题!
您好,
米莲娜
您正在为 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.
我是编程初学者,遇到以下问题: 我想写一个定义,在其中我使用 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
我真的希望有人能帮我解决这个问题!
您好, 米莲娜
您正在为 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.