Skip to content

Create analysis from cart #1041

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
50 changes: 40 additions & 10 deletions qiita_db/analysis.py
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,7 @@ def get_by_status(cls, status):
return {x[0] for x in conn_handler.execute_fetchall(sql, (status,))}

@classmethod
def create(cls, owner, name, description, parent=None):
def create(cls, owner, name, description, parent=None, from_default=False):
"""Creates a new analysis on the database

Parameters
Expand All @@ -112,23 +112,53 @@ def create(cls, owner, name, description, parent=None):
Description of the analysis
parent : Analysis object, optional
The analysis this one was forked from
from_default : bool, optional
If True, use the default analysis to populate selected samples.
Default False.
"""
queue = "create_analysis"
conn_handler = SQLConnectionHandler()
conn_handler.create_queue(queue)
# TODO after demo: if exists()

# insert analysis information into table with "in construction" status
sql = ("INSERT INTO qiita.{0} (email, name, description, "
"analysis_status_id) VALUES (%s, %s, %s, 1) "
"RETURNING analysis_id".format(cls._table))
a_id = conn_handler.execute_fetchone(
sql, (owner.id, name, description))[0]
# Needed since issue #292 exists
status_id = conn_handler.execute_fetchone(
"SELECT analysis_status_id from qiita.analysis_status WHERE "
"status = 'in_construction'")[0]
if from_default:
# insert analysis and move samples into that new analysis
dflt_id = owner.default_analysis
sql = """INSERT INTO qiita.{0}
(email, name, description, analysis_status_id)
VALUES (%s, %s, %s, %s)
RETURNING analysis_id""".format(cls._table)
conn_handler.add_to_queue(queue, sql, (owner.id, name,
description, status_id))
# MAGIC NUMBER 3: command selection step
# needed so we skip the sample selection step
sql = """INSERT INTO qiita.analysis_workflow
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I just noticed this. Why is the analysis_workflow table needed? Is it only handling a column called step, and there is no further control on it, so it looks like that column can be included in the analysis object???

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The table was supposed to be a housekeeping table to track what step the analysis is on and any other things that needed tracking during analysis creation. I guess it turns out we didn't need as much housekeeping as originally thought.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah, I think we are keeping track of everything else in the multiple tables that build up the analysis and the step just limits what can be done on the analysis. I've created a new issue #1052 and I assigned to beta. Later we can decide to exactly which alpha release it belongs, but I don't think it is either 0.1 or 0.2. Thanks for the feedback!

(analysis_id, step) VALUES (%s, %s)
RETURNING %s"""
conn_handler.add_to_queue(queue, sql, ['{0}', 3, '{0}'])
sql = """UPDATE qiita.analysis_sample
SET analysis_id = %s
WHERE analysis_id = %s RETURNING %s"""
conn_handler.add_to_queue(queue, sql, ['{0}', dflt_id, '{0}'])
else:
# insert analysis information into table as "in construction"
sql = """INSERT INTO qiita.{0}
(email, name, description, analysis_status_id)
VALUES (%s, %s, %s, %s)
RETURNING analysis_id""".format(cls._table)
conn_handler.add_to_queue(
queue, sql, (owner.id, name, description, status_id))

# add parent if necessary
if parent:
sql = ("INSERT INTO qiita.analysis_chain (parent_id, child_id) "
"VALUES (%s, %s)")
conn_handler.execute(sql, (parent.id, a_id))
"VALUES (%s, %s) RETURNING child_id")
conn_handler.add_to_queue(queue, sql, [parent.id, '{0}'])

a_id = conn_handler.execute_queue(queue)[0]
return cls(a_id)

# ---- Properties ----
Expand Down
21 changes: 21 additions & 0 deletions qiita_db/test/test_analysis.py
Original file line number Diff line number Diff line change
Expand Up @@ -123,6 +123,27 @@ def test_create_parent(self):
obs = self.conn_handler.execute_fetchall(sql, [new_id])
self.assertEqual(obs, [[1, new_id]])

def test_create_from_default(self):
new_id = get_count("qiita.analysis") + 1
owner = User("test@foo.bar")
new = Analysis.create(owner, "newAnalysis",
"A New Analysis", from_default=True)
self.assertEqual(new.id, new_id)
self.assertEqual(new.step, 3)

# Make sure samples were transfered properly
sql = "SELECT * FROM qiita.analysis_sample WHERE analysis_id = %s"
obs = self.conn_handler.execute_fetchall(sql, [owner.default_analysis])
exp = []
self.assertEqual(obs, exp)
sql = "SELECT * FROM qiita.analysis_sample WHERE analysis_id = %s"
obs = self.conn_handler.execute_fetchall(sql, [new_id])
exp = [[new_id, 1, '1.SKD8.640184'],
[new_id, 1, '1.SKB7.640196'],
[new_id, 1, '1.SKM9.640192'],
[new_id, 1, '1.SKM4.640180']]
self.assertEqual(obs, exp)

def test_retrieve_owner(self):
self.assertEqual(self.analysis.owner, "test@foo.bar")

Expand Down
5 changes: 2 additions & 3 deletions qiita_db/util.py
Original file line number Diff line number Diff line change
Expand Up @@ -905,9 +905,8 @@ def convert_to_id(value, table, conn_handler=None):
The passed string has no associated id
"""
conn_handler = conn_handler if conn_handler else SQLConnectionHandler()
_id = conn_handler.execute_fetchone(
"SELECT {0}_id FROM qiita.{0} WHERE {0} = %s".format(table),
(value, ))
sql = "SELECT {0}_id FROM qiita.{0} WHERE {0} = %s".format(table)
_id = conn_handler.execute_fetchone(sql, (value, ))
if _id is None:
raise IncompetentQiitaDeveloperError("%s not valid for table %s"
% (value, table))
Expand Down