我是编程初学者,但遇到以下问题:我想编写一个定义,在其中创建一个空矩阵,该矩阵使用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 我真的希望有人可以帮助我!
1 回答
偶然的你
TA贡献1841条经验 获得超3个赞
您正在为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.
添加回答
举报
0/150
提交
取消