Skip to content

Commit c1fc3c8

Browse files
committed
Merge pull request #1041 from squirrelo/create-analysis-from-cart
Create analysis from cart
2 parents dfc2538 + f06eb57 commit c1fc3c8

File tree

3 files changed

+63
-13
lines changed

3 files changed

+63
-13
lines changed

qiita_db/analysis.py

Lines changed: 40 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -99,7 +99,7 @@ def get_by_status(cls, status):
9999
return {x[0] for x in conn_handler.execute_fetchall(sql, (status,))}
100100

101101
@classmethod
102-
def create(cls, owner, name, description, parent=None):
102+
def create(cls, owner, name, description, parent=None, from_default=False):
103103
"""Creates a new analysis on the database
104104
105105
Parameters
@@ -112,23 +112,53 @@ def create(cls, owner, name, description, parent=None):
112112
Description of the analysis
113113
parent : Analysis object, optional
114114
The analysis this one was forked from
115+
from_default : bool, optional
116+
If True, use the default analysis to populate selected samples.
117+
Default False.
115118
"""
119+
queue = "create_analysis"
116120
conn_handler = SQLConnectionHandler()
121+
conn_handler.create_queue(queue)
117122
# TODO after demo: if exists()
118-
119-
# insert analysis information into table with "in construction" status
120-
sql = ("INSERT INTO qiita.{0} (email, name, description, "
121-
"analysis_status_id) VALUES (%s, %s, %s, 1) "
122-
"RETURNING analysis_id".format(cls._table))
123-
a_id = conn_handler.execute_fetchone(
124-
sql, (owner.id, name, description))[0]
123+
# Needed since issue #292 exists
124+
status_id = conn_handler.execute_fetchone(
125+
"SELECT analysis_status_id from qiita.analysis_status WHERE "
126+
"status = 'in_construction'")[0]
127+
if from_default:
128+
# insert analysis and move samples into that new analysis
129+
dflt_id = owner.default_analysis
130+
sql = """INSERT INTO qiita.{0}
131+
(email, name, description, analysis_status_id)
132+
VALUES (%s, %s, %s, %s)
133+
RETURNING analysis_id""".format(cls._table)
134+
conn_handler.add_to_queue(queue, sql, (owner.id, name,
135+
description, status_id))
136+
# MAGIC NUMBER 3: command selection step
137+
# needed so we skip the sample selection step
138+
sql = """INSERT INTO qiita.analysis_workflow
139+
(analysis_id, step) VALUES (%s, %s)
140+
RETURNING %s"""
141+
conn_handler.add_to_queue(queue, sql, ['{0}', 3, '{0}'])
142+
sql = """UPDATE qiita.analysis_sample
143+
SET analysis_id = %s
144+
WHERE analysis_id = %s RETURNING %s"""
145+
conn_handler.add_to_queue(queue, sql, ['{0}', dflt_id, '{0}'])
146+
else:
147+
# insert analysis information into table as "in construction"
148+
sql = """INSERT INTO qiita.{0}
149+
(email, name, description, analysis_status_id)
150+
VALUES (%s, %s, %s, %s)
151+
RETURNING analysis_id""".format(cls._table)
152+
conn_handler.add_to_queue(
153+
queue, sql, (owner.id, name, description, status_id))
125154

126155
# add parent if necessary
127156
if parent:
128157
sql = ("INSERT INTO qiita.analysis_chain (parent_id, child_id) "
129-
"VALUES (%s, %s)")
130-
conn_handler.execute(sql, (parent.id, a_id))
158+
"VALUES (%s, %s) RETURNING child_id")
159+
conn_handler.add_to_queue(queue, sql, [parent.id, '{0}'])
131160

161+
a_id = conn_handler.execute_queue(queue)[0]
132162
return cls(a_id)
133163

134164
# ---- Properties ----

qiita_db/test/test_analysis.py

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -123,6 +123,27 @@ def test_create_parent(self):
123123
obs = self.conn_handler.execute_fetchall(sql, [new_id])
124124
self.assertEqual(obs, [[1, new_id]])
125125

126+
def test_create_from_default(self):
127+
new_id = get_count("qiita.analysis") + 1
128+
owner = User("test@foo.bar")
129+
new = Analysis.create(owner, "newAnalysis",
130+
"A New Analysis", from_default=True)
131+
self.assertEqual(new.id, new_id)
132+
self.assertEqual(new.step, 3)
133+
134+
# Make sure samples were transfered properly
135+
sql = "SELECT * FROM qiita.analysis_sample WHERE analysis_id = %s"
136+
obs = self.conn_handler.execute_fetchall(sql, [owner.default_analysis])
137+
exp = []
138+
self.assertEqual(obs, exp)
139+
sql = "SELECT * FROM qiita.analysis_sample WHERE analysis_id = %s"
140+
obs = self.conn_handler.execute_fetchall(sql, [new_id])
141+
exp = [[new_id, 1, '1.SKD8.640184'],
142+
[new_id, 1, '1.SKB7.640196'],
143+
[new_id, 1, '1.SKM9.640192'],
144+
[new_id, 1, '1.SKM4.640180']]
145+
self.assertEqual(obs, exp)
146+
126147
def test_retrieve_owner(self):
127148
self.assertEqual(self.analysis.owner, "test@foo.bar")
128149

qiita_db/util.py

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -905,9 +905,8 @@ def convert_to_id(value, table, conn_handler=None):
905905
The passed string has no associated id
906906
"""
907907
conn_handler = conn_handler if conn_handler else SQLConnectionHandler()
908-
_id = conn_handler.execute_fetchone(
909-
"SELECT {0}_id FROM qiita.{0} WHERE {0} = %s".format(table),
910-
(value, ))
908+
sql = "SELECT {0}_id FROM qiita.{0} WHERE {0} = %s".format(table)
909+
_id = conn_handler.execute_fetchone(sql, (value, ))
911910
if _id is None:
912911
raise IncompetentQiitaDeveloperError("%s not valid for table %s"
913912
% (value, table))

0 commit comments

Comments
 (0)