Python & 生物学:使用 python 交叉数据集
Python & biology: using python to intersect datasets
喜欢这个网站。我是一名生物学家,提出简单的问题并使它们在 python 中变得复杂,目前卡在一个相当简单的问题上。我有两个不相等的元组列表,包含染色体('chr#')和位置('start','end')信息:
list1: 长度=1499, 元组=('chr5', '12345', '12678')
list2: 长度=75220, 元组=('chr5', '44', '7777777')
如果有人能向我解释为什么这段代码失败了,我可能会自己解决这个问题:
list1 = [('chr1', '123', '345'), ('chr1', '567', '678'), ('chr2', '123', 234'),...)
list2 = [('chr1', '123', '567'), ('chr1', '777', '890'), ('chr2', '1', 288'),...)
newlist = []
for j,k,l in list1:
if j == x for x,y,z in list2:
if int(k) >= int(y) or int(l) <= int(z):
newlist.append(k)
else:
pass
else:
pass
推理:我希望比较两个字符串中所有元组中的整数,但仅当两个元组的 item[0] 匹配时。
我将不胜感激任何帮助和提示,谢谢!
if list1[0] == list2[0]:
print
这是您要找的吗? :)
如果您也需要比较数字:
if list1[0] == list2[0]:
for i in range(1, min([len(list1), len(list2)]):
if int(list1[i]) < int(list2[i]):
print('Aha!')
如果您更改代码中的第一个 if 语句,使其不在代码运行的第二个 for 循环之前:
list1 = [('chr1', '123', '345'), ('chr1', '567', '678'), ('chr2', '123', '234')]
list2 = [('chr1', '123', '567'), ('chr1', '777', '890'), ('chr2', '1', '288')]
newlist = []
for j,k,l in list1:
for x,y,z in list2:
if j == x:
if int(k) >= int(y) or int(l) <= int(z):
newlist.append(k)
else:
pass
else:
pass
我的 post 没有回答你的问题,但我认为你的代码可以通过使用更多 pythonic 结构来改进。朝着这个方向迈出的第一步是使用 命名元组 以便可以通过属性 name, 访问染色体字段开始 和结束:
from collections import namedtuple
Chromosome = namedtuple('Chromosome', ['name', 'start', 'end'])
chromo = Chromosome('chr1', 123, 345)
# You can access the fields as chromo.name, chromo.start and chromo.end
第二步,您可以使用列表理解来减少嵌套循环的数量:
from collections import namedtuple
Chromosome = namedtuple('Chromosome', ['name', 'start', 'end'])
list1 = [Chromosome('chr1', 123, 345), Chromosome('chr1', 567, 678), ]
list2 = [Chromosome('chr1', 123, 567), Chromosome('chr2', 1, 288), ]
newlist = []
for chromo1 in list1:
# Build a list of chromosomes from list2 that
# * have the same name as chromo1
# * start before chromo1
# * end after chromo1
matches = [chromo2 for chromo2 in list2
if chromo2.name == chromo1.name and
(chromo2.start < chromo1.start or chromo2.end > chromo1.end)]
newlist.extend(matches)
喜欢这个网站。我是一名生物学家,提出简单的问题并使它们在 python 中变得复杂,目前卡在一个相当简单的问题上。我有两个不相等的元组列表,包含染色体('chr#')和位置('start','end')信息:
list1: 长度=1499, 元组=('chr5', '12345', '12678')
list2: 长度=75220, 元组=('chr5', '44', '7777777')
如果有人能向我解释为什么这段代码失败了,我可能会自己解决这个问题:
list1 = [('chr1', '123', '345'), ('chr1', '567', '678'), ('chr2', '123', 234'),...)
list2 = [('chr1', '123', '567'), ('chr1', '777', '890'), ('chr2', '1', 288'),...)
newlist = []
for j,k,l in list1:
if j == x for x,y,z in list2:
if int(k) >= int(y) or int(l) <= int(z):
newlist.append(k)
else:
pass
else:
pass
推理:我希望比较两个字符串中所有元组中的整数,但仅当两个元组的 item[0] 匹配时。 我将不胜感激任何帮助和提示,谢谢!
if list1[0] == list2[0]:
print
这是您要找的吗? :)
如果您也需要比较数字:
if list1[0] == list2[0]:
for i in range(1, min([len(list1), len(list2)]):
if int(list1[i]) < int(list2[i]):
print('Aha!')
如果您更改代码中的第一个 if 语句,使其不在代码运行的第二个 for 循环之前:
list1 = [('chr1', '123', '345'), ('chr1', '567', '678'), ('chr2', '123', '234')]
list2 = [('chr1', '123', '567'), ('chr1', '777', '890'), ('chr2', '1', '288')]
newlist = []
for j,k,l in list1:
for x,y,z in list2:
if j == x:
if int(k) >= int(y) or int(l) <= int(z):
newlist.append(k)
else:
pass
else:
pass
我的 post 没有回答你的问题,但我认为你的代码可以通过使用更多 pythonic 结构来改进。朝着这个方向迈出的第一步是使用 命名元组 以便可以通过属性 name, 访问染色体字段开始 和结束:
from collections import namedtuple
Chromosome = namedtuple('Chromosome', ['name', 'start', 'end'])
chromo = Chromosome('chr1', 123, 345)
# You can access the fields as chromo.name, chromo.start and chromo.end
第二步,您可以使用列表理解来减少嵌套循环的数量:
from collections import namedtuple
Chromosome = namedtuple('Chromosome', ['name', 'start', 'end'])
list1 = [Chromosome('chr1', 123, 345), Chromosome('chr1', 567, 678), ]
list2 = [Chromosome('chr1', 123, 567), Chromosome('chr2', 1, 288), ]
newlist = []
for chromo1 in list1:
# Build a list of chromosomes from list2 that
# * have the same name as chromo1
# * start before chromo1
# * end after chromo1
matches = [chromo2 for chromo2 in list2
if chromo2.name == chromo1.name and
(chromo2.start < chromo1.start or chromo2.end > chromo1.end)]
newlist.extend(matches)