我有一个组.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)
不确定这是否正是你想要的,但这是一个开始:)
产生:
只是一个警告:如果输入的数量足够大,那么您要做的事情将开始花费很长时间,因为它的复杂性是2^N。您无法绕过它(这就是the problem demands),但它就在那里。你知道吗
一组N个项目的所有非空子集的列表(所有物种的集合)是2N-1
这是因为它就像一个由N位组成的二进制数,每个位可以是1(取子集中的物种)或0(从子集中排除该物种)。-1排除空集(所有位0)
因此,您可以使用一个简单的整数循环来枚举所有种类的子集:
结果:
如果还需要代码来测试组是否包含子集,则需要指定组在程序中的存储方式。你知道吗
如果此帖子回答了您的问题,您应该单击绿色复选标记✔ 在左上角。
使用while循环来控制range()参数怎么样?你知道吗
相关问题 更多 >
编程相关推荐