我是编程初学者,但遇到以下问题:我想编写一个定义,在其中创建一个空矩阵,该矩阵使用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
我真的希望有人可以帮助我!
偶然的你
相关分类