在范围的每个循环中修改范围

2024-09-30 02:35:40 发布

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

我有一个组.txt包含直系群的文件,每个群中都有物种和基因ID。它看起来像:

OG_117996: R_baltica_p|32476565 V_spinosum_v|497645257
OG_117997: R_baltica_p|32476942 S_pleomorpha_s|374317197
OG_117998: R_baltica_p|32477405 V_bacterium_v|198258541

我做了一个函数,创建了整个文件中所有物种的列表(总共66个),称为listOfAllSpecies。我需要创建一个函数,给我所有包含66中1个物种的群,然后所有包含66中2个物种的群,以此类推

为了简化它:

OG_1: A|1 A|3 B|1 C|2
OG_2: A|4 B|6
OG_3: C|8 B|9 A|10

我需要在这个例子中:

(species) A,B (are in groups) OG_1, OG_2, OG_3
(species) A,C (are in groups) OG_1, OG_3
(species) B,C (are in groups) OG_1, OG_2, OG_3
(species) A,B,C (are in groups) OG_1, OG_3
(species) B (is in groups) OG_1, OG_2, OG_3

我想试试

for species in range(start, end=None):         
    if end == None:           
        start = 0
        end = start + 1

获取所有物种列表中的第一个物种,然后告诉我它包含在哪些组OG_XXXX。然后得到第一个和第二个物种,以此类推,直到66个物种全部被捕获。如何在for循环中修改范围,或者是否有其他方法来执行此操作?你知道吗

这是我的实际代码,带有我需要的函数,没有我需要的部分:

import sys 

if len(sys.argv) != 2:
print("Error, file name to open is missing")
sys.exit([1])

def readGroupFile(groupFileName):
dict_gene_taxonomy = {}
fh = open(groupFileName,"r")

for line in fh:
    liste = line.split(": ")
    groupName = liste[0]
    genesAsString = liste[1]
    dict_taxon = {}
    liste_gene = genesAsString.split()

    for item in liste_gene:
        taxonomy_gene = item.split("|")
        taxonomy = taxonomy_gene[0]
        geneId   = taxonomy_gene[1]

        if not taxonomy in dict_taxon:
            dict_taxon[taxonomy] = []

        dict_taxon[taxonomy].append(geneId)

    dict_gene_taxonomy[groupName] = dict_taxon
fh.close()
return dict_gene_taxonomy


def showListOfAllSpecies(dictio):
listAllSpecies = []
for groupName in dictio:
    dictio_in_dictio = dictio[groupName]
    for speciesName in dictio_in_dictio:
        if not speciesName in listAllSpecies:
            listAllSpecies.append(speciesName)
return listAllSpecies

dico = readGroupFile(sys.argv[1])
listAllSpecies = showListOfAllSpecies(dico)

Tags: inforif物种aredictgroupstaxonomy
3条回答

不确定这是否正是你想要的,但这是一个开始:)

from itertools import combinations

# Assume input is a list of strings called input_list
input_list = ['OG_1: A|1 A|3 B|1 C|2','OG_2: A|4 B|6','OG_3: C|8 B|9 A|10']

# Create a dict to store relationships and a list to store OGs
rels = {}
species = set()

# Populate the dict
for item in input_list:
    params = item.split(': ')
    og = params[0]
    raw_species = params[1].split()
    s = [rs.split('|')[0] for rs in raw_species]
    rels[og] = s

    for item in s:
        species.add(item)

# Get the possible combinations of species:
combos = [c for limit in range(1, len(l)-1) for c in combinations(species,limit)]

def combo_in_og(combo, og):
    for item in combo:
        if item not in rels[og]:
            return False
    return True

# Loop over the combinations and print
for combo in combos:
    valid_ogs = []
    for og in ogs:
        if combo_in_og(combo, og):
            valid_ogs.append(og)
    print('(species) ' + ','.join(combo) + ' (are in groups) ' + ', '.join(valid_ogs))

产生:

(species) C (are in groups) OG_1, OG_3
(species) A (are in groups) OG_1, OG_2, OG_3
(species) B (are in groups) OG_1, OG_2, OG_3
(species) C,A (are in groups) OG_1, OG_3
(species) C,B (are in groups) OG_1, OG_3
(species) A,B (are in groups) OG_1, OG_2, OG_3
(species) C,A,B (are in groups) OG_1, OG_3

只是一个警告:如果输入的数量足够大,那么您要做的事情将开始花费很长时间,因为它的复杂性是2^N。您无法绕过它(这就是the problem demands),但它就在那里。你知道吗

一组N个项目的所有非空子集的列表(所有物种的集合)是2N-1

这是因为它就像一个由N位组成的二进制数,每个位可以是1(取子集中的物种)或0(从子集中排除该物种)。-1排除空集(所有位0)

因此,您可以使用一个简单的整数循环来枚举所有种类的子集:

# sample data
listOfAllSpecies = ['A', 'B', 'C']

# enumerate all subsets of listOfAllSpecies, 0 excluded (the empty set)
for bits in range(1, 2**len(listOfAllSpecies)):

    # build the subset
    subset = []
    for n in range(len(listOfAllSpecies)):
        # test if the current subset includes bit n
        if bits & 2**n:
            subset.append(listOfAllSpecies[n])

    # see which groups contain the given subset
    print "species", ",".join(subset), "are in groups TODO"

结果:

species A are in groups TODO
species B are in groups TODO
species A,B are in groups TODO
species C are in groups TODO
species A,C are in groups TODO
species B,C are in groups TODO
species A,B,C are in groups TODO

如果还需要代码来测试组是否包含子集,则需要指定组在程序中的存储方式。你知道吗

如果此帖子回答了您的问题,您应该单击绿色复选标记✔ 在左上角。

使用while循环来控制range()参数怎么样?你知道吗

end = 0
start = 0
while end < 1000:
    for species in range(start, end):         
        ...do something

    end += 1

相关问题 更多 >

    热门问题