Sqlalchemy: select specific columns from multiple joins using aliases

It's been over a day now, and the examples I could find didn't work. I am new to SQLALCHEMY and I find the documentation not very interesting.

Request (for now):

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 genes interacting. One of them is called "bait" and the other is called "victim". Prey and Bait are aliases for the same table that contains additional information about these genes. The goal is to select all interactions with a given ENSG00000100360 gene as bait or prey.

Problem:

This query returns about 20 or so columns, but I only need six specific ones, two from each original table (I would like to rename them too). From the examples found on interwebz, I thought I should add:

  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 (faucet) .load_only ('TAP_UNIQUE_PEPTIDE_COUNT', 'TAP_SEQUENCE' local_COVERAGE /Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/sqlalchemy/orm/strategy_options.py "line 82, at init     self.path = insp._path_registry AttributeError: Table object has no "_path_registry" attribute

I have not been able to find anything on google on what to do about this. Sqlalchemy table objects are created from database table metadata.

What I'm trying to emulate with orm sqlalchemy statements looks like this:

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 request? Thank you in advance! John

+3


source to share


1 answer


Just change this part db.session.query(tap,prey,bait).\

like this:

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 want.

+3


source







All Articles