【发布时间】:2019-02-25 09:38:22
【问题描述】:
我正在尝试使用ensembl genome browser api 来获取一些基因组信息。挑战在于每个网络请求可能需要几秒钟,所以我一直在尝试使用asyncio 来等待这些网络请求,同时处理我拥有的数据。
这是我正在使用的示例输入 DataFrame:
import pandas as pd
df = pd.DataFrame({'Gene Name': {0: 'A1CF', 1: 'A1CF', 2: 'A1CF'},
'Sample Name': {0: 'ATL045', 1: 'QC2-20-T2', 2: 'GHE0624'},
'CDS Mutation': {0: 'c.234A>C', 1: 'c.492C>T', 2: 'c.490G>A'},
'AA Mutation': {0: 'p.K78N', 1: 'p.V164V', 2: 'p.V164I'}})
目标是使用上述df 中的Gene Name 和CDS Mutation 信息来获取其他一些基因组信息。
第一个方法旨在调用ensembl_calls 方法,该方法将发出网络请求并返回一些解析的输出。理想情况下,解析后的输出会组合成类似熊猫主数据框的东西。
async def concurrent_location_info(df):
import pandas as pd
import asyncio
full_df = pd.DataFrame()
# iterate through DataFrame
dfs = [asyncio.ensure_future(ensembl_calls(row)) for index, row in df.iterrows()]
print(dfs)
在这种方法中,我试图发出我的网络请求并解析我返回的信息。
# this makes the network ensembl call asynchronously
async def ensembl_calls(row):
new_df = {}
try: # sometimes ensembl can't find what i'm looking for
# this can take a while
await info = Ensembl(row['Gene Name'], row['CDS Mutation']).info().split(',')
# parse the output
new_df['Gene'] = row['Gene Name']
new_df['Chrom'] = info[0]
new_df['Start'] = info[1]
new_df['End'] = info[2]
new_df['WT'] = info[3]
new_df['Var'] = info[4]
new_df['Sift_Index'] = info[5]
except:
pass
return new_df # ideally somehow gets added to a master pd dataframe
我在正确的轨道上吗?有没有办法让它工作?
【问题讨论】:
-
你有什么问题?
-
@user2357112 代码不能按原样工作,因为我没有正确实现 asyncio。我正在寻求帮助以纠正此问题。
标签: python python-asyncio