@@ -77,16 +77,13 @@ class PrepTemplate(MetadataTemplate):
7777 _filepath_table = 'prep_template_filepath'
7878
7979 @classmethod
80- def create (cls , md_template , raw_data , study , data_type ,
81- investigation_type = None ):
80+ def create (cls , md_template , study , data_type , investigation_type = None ):
8281 r"""Creates the metadata template in the database
8382
8483 Parameters
8584 ----------
8685 md_template : DataFrame
8786 The metadata template file contents indexed by samples Ids
88- raw_data : RawData
89- The raw_data to which the prep template belongs to.
9087 study : Study
9188 The study to which the prep template belongs to.
9289 data_type : str or int
@@ -111,7 +108,7 @@ def create(cls, md_template, raw_data, study, data_type,
111108
112109 # Get a connection handler
113110 conn_handler = SQLConnectionHandler ()
114- queue_name = "CREATE_PREP_TEMPLATE_%d" % raw_data .id
111+ queue_name = "CREATE_PREP_TEMPLATE_%d" % study .id
115112 conn_handler .create_queue (queue_name )
116113
117114 # Check if the data_type is the id or the string
@@ -134,14 +131,19 @@ def create(cls, md_template, raw_data, study, data_type,
134131 # We need the prep_id for multiple calls below, which currently is not
135132 # supported by the queue system. Thus, executing this outside the queue
136133 prep_id = conn_handler .execute_fetchone (
137- "INSERT INTO qiita.prep_template (data_type_id, raw_data_id, "
138- "investigation_type) VALUES (%s, %s, %s) RETURNING "
139- "prep_template_id" , ( data_type_id , raw_data . id ,
140- investigation_type ))[0 ]
134+ "INSERT INTO qiita.prep_template "
135+ "(data_type_id, investigation_type) "
136+ "VALUES (%s, %s) RETURNING prep_template_id" ,
137+ ( data_type_id , investigation_type ))[0 ]
141138
142139 cls ._add_common_creation_steps_to_queue (md_template , prep_id ,
143140 conn_handler , queue_name )
144141
142+ # Link the prep template with the study
143+ sql = ("INSERT INTO qiita.study_prep_template "
144+ "(study_id, prep_template_id) VALUES (%s, %s)" )
145+ conn_handler .add_to_queue (queue_name , sql , (study .id , prep_id ))
146+
145147 try :
146148 conn_handler .execute_queue (queue_name )
147149 except Exception :
@@ -244,6 +246,11 @@ def delete(cls, id_):
244246 cls ._id_column ),
245247 (id_ ,))
246248
249+ # Remove the row from study_prep_template
250+ conn_handler .execute (
251+ "DELETE FROM qiita.study_prep_template "
252+ "WHERE {0} = %s" .format (cls ._id_column ), (id_ ,))
253+
247254 # Remove the row from prep_template
248255 conn_handler .execute (
249256 "DELETE FROM qiita.prep_template where "
@@ -365,10 +372,9 @@ def study_id(self):
365372 The ID of the study with which this prep template is associated
366373 """
367374 conn = SQLConnectionHandler ()
368- sql = ("SELECT srd.study_id FROM qiita.prep_template pt JOIN "
369- "qiita.study_raw_data srd ON pt.raw_data_id = srd.raw_data_id "
370- "WHERE prep_template_id = %d" % self .id )
371- study_id = conn .execute_fetchone (sql )
375+ sql = ("SELECT study_id FROM qiita.study_prep_template "
376+ "WHERE prep_template_id=%s" )
377+ study_id = conn .execute_fetchone (sql , (self .id ,))
372378 if study_id :
373379 return study_id [0 ]
374380 else :
0 commit comments