@@ -99,7 +99,7 @@ def get_by_status(cls, status):
99
99
return {x [0 ] for x in conn_handler .execute_fetchall (sql , (status ,))}
100
100
101
101
@classmethod
102
- def create (cls , owner , name , description , parent = None ):
102
+ def create (cls , owner , name , description , parent = None , from_default = False ):
103
103
"""Creates a new analysis on the database
104
104
105
105
Parameters
@@ -112,23 +112,53 @@ def create(cls, owner, name, description, parent=None):
112
112
Description of the analysis
113
113
parent : Analysis object, optional
114
114
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.
115
118
"""
119
+ queue = "create_analysis"
116
120
conn_handler = SQLConnectionHandler ()
121
+ conn_handler .create_queue (queue )
117
122
# 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 ))
125
154
126
155
# add parent if necessary
127
156
if parent :
128
157
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}' ] )
131
160
161
+ a_id = conn_handler .execute_queue (queue )[0 ]
132
162
return cls (a_id )
133
163
134
164
# ---- Properties ----
0 commit comments