简体   繁体   English

sqlalchemy:使用别名从多个联接中选择特定的列

[英]sqlalchemy: select specific columns from multiple join using aliases

This has be stumped for more than a day now and examples I could find have not worked. 现在,这个问题已经困扰了超过一天,而我发现的例子却没有奏效。 I am new to SQLALCHEMY and I find the documentation not very enlightening. 我是SQLALCHEMY的新手,但是我发现文档不是很有启发性。

The query (so far): 查询(到目前为止):

prey = alias(ensembl_genes, name='prey')
bait = alias(ensembl_genes, name='bait')
query = db.session.query(tap,prey,bait).\
    join(prey, tap.c.TAP_PREY_ENSEMBL_GENE_ID==prey.c.ENSEMBL_GENE_ID).\
    join(bait, tap.c.TAP_BAIT_ENSEMBL_GENE_ID==bait.c.ENSEMBL_GENE_ID).\
    filter(\
      or_(\
        tap.c.TAP_PREY_ENSEMBL_GENE_ID=='ENSG00000100360',\
        tap.c.TAP_BAIT_ENSEMBL_GENE_ID=='ENSG00000100360'\
      )\
    ).\
    order_by(desc(tap.c.TAP_UNIQUE_PEPTIDE_COUNT))

tap refers to a table of interacting genes. tap是指相互作用的基因表。 One interactor is designated the 'bait' and the other the 'prey'. 一个交互者被称为“诱饵”,另一个被称为“猎物”。 Prey and Bait are aliases for the same table that holds additional information on these genes. 猎物和诱饵是同一个表的别名,该表包含有关这些基因的其他信息。 The objective is to select all interactions with a given gene 'ENSG00000100360' as either bait or prey. 目的是选择与给定基因“ ENSG00000100360”作为诱饵或猎物的所有相互作用。

The problem: 问题:

This query returns about 20 or so columns, but I need only six specific ones, two from each original tables (I'd like to rename them as well). 该查询返回大约20列,但是我只需要六个特定的列,每个原始表中两个(我也想重命名它们)。 From examples found on the interwebz I thought I should add: 从interwebz上的示例中,我认为我应该添加:

  options(
      Load(tap).load_only('TAP_UNIQUE_PEPTIDE_COUNT','TAP_SEQUENCE_COVERAGE'),
      Load(prey).load_only('ENSEMBL_GENE_SYMBOL','ENSEMBL_GENE_ID'),
      Load(bait).load_only('ENSEMBL_GENE_SYMBOL','ENSEMBL_GENE_ID')
    )

But this gives me the following error: 但这给了我以下错误:

File "/Users/jvandam/Github/syscilia/tools/BDT/quest/blueprints/genereport.py", line 246, in createTAPMSView Load(tap).load_only('TAP_UNIQUE_PEPTIDE_COUNT','TAP_SEQUENCE_COVERAGE') File "/opt/local/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/sqlalchemy/orm/strategy_options.py", line 82, in init self.path = insp._path_registry AttributeError: 'Table' object has no attribute '_path_registry' 文件“ /Users/jvandam/Github/syscilia/tools/BDT/quest/blueprints/genereport.py”,行246,位于createTAPMSView Load(tap).load_only('TAP_UNIQUE_PEPTIDE_COUNT','TAP_SEQUENCE_COVERAGE')文件“ / opt / local /Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/sqlalchemy/orm/strategy_options.py“,第82行, init self.path = insp._path_registry AttributeError:“表”对象没有属性“ _path_registry”

I have not been able to find anything on google about what to do about this. 我无法在Google上找到有关此事的任何信息。 The sqlalchemy table objects are created from the database table metadata. sqlalchemy表对象是从数据库表元数据创建的。

What I am trying to emulate using the sqlalchemy orm statements is: 我正在尝试使用sqlalchemy orm语句模拟的是:

SELECT
prey.ENSEMBL_GENE_SYMBOL AS PREY_ENSEMBL_GENE_SYMBOL,
prey.ENSEMBL_GENE_ID AS PREY_ENSEMBL_GENE_ID,
bait.ENSEMBL_GENE_SYMBOL AS BAIT_ENSEMBL_GENE_SYMBOL,
bait.ENSEMBL_GENE_ID AS BAIT_ENSEMBL_GENE_ID,
t.TAP_UNIQUE_PEPTIDE_COUNT AS UNIQUE_PEPTIDE_COUNT,
t.TAP_SEQUENCE_COVERAGE AS SEQUENCE_COVERAGE
FROM TAP as t
INNER JOIN ENSEMBL_GENES AS prey
  ON tap.TAP_PREY_ENSEMBL_GENE_ID=prey.ENSEMBL_GENE_ID
INNER JOIN ENSEMBL_GENES AS bait
  ON t.TAP_BAIT_ENSEMBL_GENE_ID=bait.ENSEMBL_GENE_ID
WHERE
  t.TAP_PREY_ENSEMBL_GENE_ID='ENSG00000100360' 
  OR t.TAP_BAIT_ENSEMBL_GENE_ID='ENSG00000100360'
ORDER BY t.TAP_UNIQUE_PEPTIDE_COUNT DESC

Can anyone help me fix my query? 谁能帮我解决我的查询问题? Thanks in advance! 提前致谢! John 约翰

Just change this part db.session.query(tap,prey,bait).\\ with the below: 只需使用以下内容更改这部分db.session.query(tap,prey,bait).\\

db.session.query(\
    prey.ENSEMBL_GENE_SYMBOL.label("PREY_ENSEMBL_GENE_SYMBOL"),
    prey.ENSEMBL_GENE_ID.label("PREY_ENSEMBL_GENE_ID"),
    bait.ENSEMBL_GENE_SYMBOL.label("BAIT_ENSEMBL_GENE_SYMBOL"),
    bait.ENSEMBL_GENE_ID.label("BAIT_ENSEMBL_GENE_ID"),
    tap.TAP_UNIQUE_PEPTIDE_COUNT.label("UNIQUE_PEPTIDE_COUNT"),
    tap.TAP_SEQUENCE_COVERAGE.label("SEQUENCE_COVERAGE"),
).\
select_from(tap).\  # @note: need this in so that FROM and JOINs are in desired order

This will select only the columns you need. 这只会选择您需要的列。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM