比较数组:根据坐标对TSV文件中的行进行分组

2024-05-14 02:34:49 发布

您现在位置:Python中文网/ 问答频道 /正文

我正在尝试使用python2.7.2对包含制表符分隔值列表的文件执行一些操作。有关更多信息,文件格式称为BED,表示一个基因列表,其中每个基因用一行表示。每行的前三个字段表示坐标。另一个字段包含的描述可能与多行相同

在具有相同描述的行中,我需要将具有重叠坐标的所有行组合在一起,并以某种方式明确地命名这些子组。问题是我实际上需要将所有具有重叠坐标的线分组为一组,例如:

chr1    1    3    geneA    1000    +
chr1    3    5    geneA    1000    +
chr1    4    6    geneA    1000    +
chr1    8    9    geneA    1000    +

应按以下方式对基因进行分组:

chr1    1    3    geneA    1000    +
chr1    3    5    geneA    1000    +
chr1    4    6    geneA    1000    +

以及

chr1    8    9    geneA    1000    +

最终目标是为每个子组输出一行(新行),例如:

chr1    1    6    geneA    1000    +
chr1    8    9    geneA    1000    +

第一个字段(chr)的值是可变的,应在具有相同chr值的行中构建子组

到目前为止,我一直试图用这种(错误的)方法来解决问题:

#key = description
#values = list of lines (genes) with same description
#self.raw_groups_of_overlapping.items = attribute (dict) containing, given a description, all genes whose description matches the key
#self.picked_cherries = attribute (dict) in which I would like to store, for a given unique identifier, all genes in a specific subgroup (sub-grouping lines according to the aformentioned rule)
#self.__overlappingGenes__(j,k) method evaluating if lines (genes) j and k overlap
for key,values in self.raw_groups_of_overlapping.items():
    for j in values:
        #Remove gene j from list:
        is_not_j = lambda x: x is not j
        other_genes = filter(is_not_j, values) 
        for k in other_genes:
            if self.__overlappingGenes__(j,k):
                intersection = [x for x in j.overlaps_with if x in k.overlaps_with]
                identifier = ''
                for gene in intersection:
                    identifier += gene.chr.replace('chr', '') + str(gene.start) + str(gene.end) + gene.description + gene.strand.replace('\n', '')
                try:
                    self.picked_cherries[identifier].append(j)
                except:
                    self.picked_cherries[identifier] = []
                    self.picked_cherries[identifier].append(j)
                break

我知道我没有把所有的基因放在一起考虑,我很感激你的意见


Tags: inselffor基因descriptionvaluesidentifierchr1