【问题标题】:find overlaps/range between two datasets using pandas or enumerate使用 pandas 或枚举查找两个数据集之间的重叠/范围
【发布时间】:2021-02-16 03:06:36
【问题描述】:

我正在尝试对具有以下条件的两个文件进行一些区间范围操作 检查 chrom 是否相等,然后检查我的 co0rdinatefile 的开始和结束是否在或等于gene_annotation 文件的开始和结束(如果链为“+”,则开始和结束将是例如 10-20,如果它"-" 它将是 20-10) ,如果匹配从坐标和gene_id,geneannotation 文件中的gene_name 打印起始端链。 (为了表示目的,我有头部注释文件)

注释文件中的行数 ~50000 协调文件中的行数 ~200,000

基因注释文件

chrom     start       end             gene_id    gene_name strand
17  71223692  71274336  ENSMUSG00000085299      Gm16627      -
17  18186448  18211184  ENSMUSG00000067978  Vmn2r-ps113      +
11  84645863  84684319  ENSMUSG00000020530       Ggnbp2      -
 7  51097639  51106551  ENSMUSG00000074155         Klk5      +
13  31711037  31712238  ENSMUSG00000087276      Gm11378      +

坐标文件

  chrom start   end strand
  1 4247322 4247912 -
  1 4427449 4432604 +
  1 4763414 4764404 -
  1 4764597 4767606 -
  1 4764597 4766491 -
  1 4766882 4767606 -
  1 4767729 4772649 -
  1 4767729 4768829 -
  1 4767729 4775654 -
  1 4772382 4772649 -
  1 4772814 4774032 -
  1 4772814 4774159 -
  1 4772814 4775654 -
  1 4772814 4774032 +
  1 4774186 4775654 -
  1 4774186 4775654 
  1 4774186 4775699 -

想要的输出

 chrom, start, end,strand, gene_id, gene_name
 1      4427432 4432686 + ENSMUSG0001 abcd

另一个问题是在某些情况下,如果有匹配项,它可能会映射到gene_id,在这种情况下我想写

 chrom, start, end,strand, gene_id, gene_name
 1      4427432 4432686 + ENSMUSG0001,ENSMUSG0002 abcd,efgh

到目前为止我的代码:

 import csv 

 with open('coordinates.txt', 'r') as source:
      coordinates = list(csv.reader(source, delimiter="\t"))

 with open('/gene_annotations.txt', 'rU') as source:
      #if i do not use 'rU' i get this error Error: new-line character seen in unquoted field - do you need to open the file in universal-newline mode?
      annotations = list(csv.reader(source, delimiter="\t"))

for index,line in enumerate(coordinates):

    for index2, line2 in enumerate(annotations):


        if coordinates[line][0] == annotations[line2][0] and coordinates[line][1] <= annotations[line2][1] and annotations[line2][2] >= coordinates[line][2] :
         print "%s\t%s\t%s\t%s\t%s" % (coordinates[line][0],coordinates[line][1],coordinates[line][2], annotations[line2][3], annotations[line2][4])
         break

我得到的错误

---> 15         if coordinates[line][0] == annotations[line2][0] and coordinates[line][1] <= annotations[line2][1] and annotations[line2][2] >= coordinates[line][2] :
16              print "%s\t%s\t%s\t%s\t%s" % (coordinates[line][0],coordinates[line][1],coordinates[line][2], annotations[line2][3], annotations[line2][4])
17              break

TypeError: list indices must be integers, not list

pandas 会是解决这个问题的好方法吗?

【问题讨论】:

    标签: python intervals


    【解决方案1】:

    我假设坐标是一个列表列表,例如 [[1,2],[3,4]] 。 线

    for index,line in enumerate(coordinates):
    

    遍历坐标,将坐标的每一行作为行返回,将索引作为索引返回。

    if coordinates[line][0] == annotations[line2][0] and coordinates[line][1] <= annotations[line2][1] and annotations[line2][2] >= coordinates[line][2] :
    

    错误消息表示您在此处使用列表(行)作为索引。您可能想使用索引而不是行:

    if coordinates[index][0] == annotations[index2][0] and coordinates[index][1] <= annotations[index2][1] and annotations[index2][2] >= coordinates[index][2] :
    

    最好只使用 line:

    if line[0] == line2[0] and line[1] <= line2[1] and line2[2] >= line[2] :
    

    https://docs.python.org/2.7/reference/compound_stmts.html?highlight=for_stmt#grammar-token-for_stmt

    【讨论】:

      【解决方案2】:
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2021-06-19
      • 1970-01-01
      • 1970-01-01
      • 2015-02-18
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多