也许我之前的问题太长而且没完没了地回答,对不起......我会尝试更具体地缩短我之前的问题
我可以从 API 查询(json 格式作为输出)中提取以下信息:
基因1
Experiment1
Experiment2
Experiment3
Experiment4
基因2
Experiment5
Experiment2
Experiment3
Experiment8
Experiment9
[...]
所以我获得了研究过它们的基因及其相关实验......一个基因可以有多个实验,1个实验可以有多个基因(多对多)
我在 SQL Alchemy 中有这个模式:
from sqlalchemy import create_engine, Column, Integer, String, Date, ForeignKey, Table, Float
from sqlalchemy.orm import sessionmaker, relationship, backref
from sqlalchemy.ext.declarative import declarative_base
import requests
Base = declarative_base()
Genes2experiments = Table('genes2experiments',Base.metadata,
Column('gene_id', String, ForeignKey('genes.id')),
Column('experiment_id', String, ForeignKey('experiments.id'))
)
class Genes(Base):
__tablename__ = 'genes'
id = Column(String(45), primary_key=True)
experiments = relationship("Experiments", secondary=Genes2experiments, backref="genes")
def __init__(self, id=""):
self.id= id
def __repr__(self):
return "<genes(id:'%s')>" % (self.id)
class Experiments(Base):
__tablename__ = 'experiments'
id = Column(String(45), primary_key=True)
def __init__(self, id=""):
self.id= id
def __repr__(self):
return "<experiments(id:'%s')>" % (self.id)
def setUp():
global Session
engine=create_engine('mysql://root:password@localhost/db_name?charset=utf8', pool_recycle=3600,echo=False)
Session=sessionmaker(bind=engine)
def add_data():
session=Session()
for i in range(0,1000,200):
request= requests.get('http://www.ebi.ac.uk/gxa/api/v1',params={"updownInOrganism_part":"brain","rows":200,"start":i})
result = request.json
for item in result['results']:
gene_to_add = item['gene']['ensemblGeneId']
session.commit()
session.close()
setUp()
add_data()
使用此代码,我只需将 API 查询到基因表中的所有基因添加到我的数据库中......
第一个问题:我应该如何以及何时添加实验信息以保持他们的关系???
第二个问题:我应该在 Experiments 类中添加一个新的辅助关系,就像在 Genes 类中一样,还是只放一个就足够了?
谢谢
(更多上下文/信息:我以前的问题)