我正在尝试比较一个文件中的一行,并将每条匹配的行放入输出文件中的另一文件中。例如,这是第一个文件。
.fc-today.fc-day-number {
background-color: #fff2c5;
}
然后是另一个文件:
chr8 18 . T T * *
chr8 29 . C T . .
chr9 21 . TA T . .
chr18 22 . C T . .
chr18 23 . A G . .
我想要的输出是:
chr8 ensembl CDS 1 1042 . - 0 gene_id "ENSCAFG00000031632"; gene_version "1"; transcript_id "ENSCAFT00000048171"; transcript_version "1"; exon_number "1"; gene_source "ensembl"; gene_biotype "protein_coding"; transcript_source "ensembl"; transcript_biotype "protein_coding"; protein_id "ENSCAFP00000042624"; protein_version "1";
chr8 ensembl CDS 27 1227 . + 0 gene_id "ENSCAFG00000032228"; gene_version "1"; transcript_id "ENSCAFT00000037896"; transcript_version "2"; exon_number "1"; gene_source "ensembl"; gene_biotype "protein_coding"; transcript_source "ensembl"; transcript_biotype "protein_coding"; protein_id "ENSCAFP00000033535"; protein_version "2";
chr8 ensembl CDS 41 1006 . - 0 gene_id "ENSCAFG00000029302"; gene_version "1"; transcript_id "ENSCAFT00000048043"; transcript_version "1"; exon_number "1"; gene_source "ensembl"; gene_biotype "protein_coding"; transcript_source "ensembl"; transcript_biotype "protein_coding"; protein_id "ENSCAFP00000036901"; protein_version "1";
因此,我想获取第一个文件的每一行并查找每一行,然后搜索第一列是否匹配,如果第1列匹配,则文件1中的第二个数字在第4列和第5列的范围内。然后,如果它们匹配,则在第一个文件中的各行下写一个新文件,文件2下的所有匹配行都在其下。这是我尝试过的:
chr8 18 . T T * *
chr8 ensembl CDS 1 1042 . - 0 gene_id "ENSCAFG00000031632"; gene_version "1"; transcript_id "ENSCAFT00000048171"; transcript_version "1"; exon_number "1"; gene_source "ensembl"; gene_biotype "protein_coding"; transcript_source "ensembl"; transcript_biotype "protein_coding"; protein_id "ENSCAFP00000042624"; protein_version "1";
chr8 29 . C T . .
chr8 ensembl CDS 1 1042 . - 0 gene_id "ENSCAFG00000031632"; gene_version "1"; transcript_id "ENSCAFT00000048171"; transcript_version "1"; exon_number "1"; gene_source "ensembl"; gene_biotype "protein_coding"; transcript_source "ensembl"; transcript_biotype "protein_coding"; protein_id "ENSCAFP00000042624"; protein_version "1";
chr8 ensembl CDS 27 1227 . + 0 gene_id "ENSCAFG00000032228"; gene_version "1"; transcript_id "ENSCAFT00000037896"; transcript_version "2"; exon_number "1"; gene_source "ensembl"; gene_biotype "protein_coding"; transcript_source "ensembl"; transcript_biotype "protein_coding"; protein_id "ENSCAFP00000033535"; protein_version "2";
但是我从来没有得到想要的输出。
答案 0 :(得分:0)
我相信您的索引有误。
if (int(vll[1]) > int(gll[3])) and (int(vll[1]) < int(gll[4])):
“ vll [1]”为18 “ gll [3]”为1042,因为“ ensembl CDS”似乎由“”分隔,而不是“ \ t” 请尝试逐步调试器并验证索引。
答案 1 :(得分:0)
发现了这个问题,只需要用公开声明来移动我的问题即可。另外,我在原始文件中添加了一些内容来处理一些注释:
with open('a1.vcf') as vfh:
for line in vfh:
if '#' not in line[0]:
ct=0
vll=line.split('\t')
with open('cds.gtf') as gfh:
for gline in gfh:
gll=gline.split('\t')
if vll[0] == gll[0]:
if (int(vll[1]) > int(gll[3])) and (int(vll[1]) < int(gll[4])):
while ct < 1:
opt+=line
ct+=1
opt+=gline