2323from qiita_db .ontology import Ontology
2424from qiita_db .metadata_template import (PrepTemplate , SampleTemplate ,
2525 load_template_to_dataframe ,
26- SAMPLE_TEMPLATE_COLUMNS )
26+ SAMPLE_TEMPLATE_COLUMNS ,
27+ looks_like_qiime_mapping_file )
2728from qiita_db .util import convert_to_id , get_mountpoint
2829from qiita_db .exceptions import (QiitaDBUnknownIDError , QiitaDBColumnError ,
2930 QiitaDBExecutionError , QiitaDBDuplicateError ,
3031 QiitaDBDuplicateHeaderError , QiitaDBError )
32+ from qiita_ware .metadata_pipeline import (
33+ create_templates_from_qiime_mapping_file )
3134from qiita_pet .handlers .base_handlers import BaseHandler
3235from qiita_pet .handlers .util import check_access
3336from qiita_pet .handlers .study_handlers .listing_handlers import (
@@ -161,13 +164,11 @@ def process_sample_template(self, study, user, callback):
161164 HTTPError
162165 If the sample template file does not exists
163166 """
164- # If we are on this function, the argument "sample_template" must
165- # defined. If not, let tornado raise its error
167+ # If we are on this function, the arguments "sample_template" and
168+ # "data_type" must be defined. If not, let tornado raise its error
166169 sample_template = self .get_argument ('sample_template' )
170+ data_type = self .get_argument ('data_type' )
167171
168- # Define here the message and message level in case of success
169- msg = "The sample template '%s' has been added" % sample_template
170- msg_level = "success"
171172 # Get the uploads folder
172173 _ , base_fp = get_mountpoint ("uploads" )[0 ]
173174 # Get the path of the sample template in the uploads folder
@@ -177,25 +178,33 @@ def process_sample_template(self, study, user, callback):
177178 # The file does not exist, fail nicely
178179 raise HTTPError (404 , "This file doesn't exist: %s" % fp_rsp )
179180
181+ # Define here the message and message level in case of success
182+ is_mapping_file = looks_like_qiime_mapping_file (fp_rsp )
183+
180184 try :
181185 with warnings .catch_warnings (record = True ) as warns :
182186 # deleting previous uploads and inserting new one
183187 self .remove_add_study_template (study .raw_data , study .id ,
184- fp_rsp )
188+ fp_rsp , data_type ,
189+ is_mapping_file )
185190
186- # join all the warning messages into one. Note that this info
187- # will be ignored if an exception is raised
191+ # join all the warning messages into one. Note that this
192+ # info will be ignored if an exception is raised
188193 if warns :
189194 msg = '; ' .join ([str (w .message ) for w in warns ])
190195 msg_level = 'warning'
191196
192197 except (TypeError , QiitaDBColumnError , QiitaDBExecutionError ,
193198 QiitaDBDuplicateError , IOError , ValueError , KeyError ,
194- CParserError , QiitaDBDuplicateHeaderError , QiitaDBError ) as e :
199+ CParserError , QiitaDBDuplicateHeaderError ,
200+ QiitaDBError ) as e :
195201 # Some error occurred while processing the sample template
196202 # Show the error to the user so they can fix the template
197- msg = html_error_message % ('parsing the sample template:' ,
198- basename (fp_rsp ), str (e ))
203+ error_msg = ('parsing the QIIME mapping file'
204+ if is_mapping_file
205+ else 'parsing the sample template' )
206+ msg = html_error_message % (error_msg , basename (fp_rsp ),
207+ str (e ))
199208 msg_level = "danger"
200209
201210 callback ((msg , msg_level , None , None , None ))
@@ -564,9 +573,14 @@ def unspecified_action(self, study, user, callback):
564573 msg_level = 'danger'
565574 callback ((msg , msg_level , 'study_information_tab' , None , None ))
566575
567- def remove_add_study_template (self , raw_data , study_id , fp_rsp ):
576+ def remove_add_study_template (self , raw_data , study_id , fp_rsp , data_type ,
577+ is_mapping_file ):
568578 """Replace prep templates, raw data, and sample template with a new one
569579 """
580+ if is_mapping_file and data_type == "" :
581+ raise ValueError ("Please, choose a data type if uploading a QIIME "
582+ "mapping file" )
583+
570584 for rd in raw_data ():
571585 rd = RawData (rd )
572586 for pt in rd .prep_templates :
@@ -575,8 +589,13 @@ def remove_add_study_template(self, raw_data, study_id, fp_rsp):
575589 if SampleTemplate .exists (study_id ):
576590 SampleTemplate .delete (study_id )
577591
578- SampleTemplate .create (load_template_to_dataframe (fp_rsp ),
579- Study (study_id ))
592+ if is_mapping_file :
593+ create_templates_from_qiime_mapping_file (fp_rsp , Study (study_id ),
594+ int (data_type ))
595+ else :
596+ SampleTemplate .create (load_template_to_dataframe (fp_rsp ),
597+ Study (study_id ))
598+
580599 remove (fp_rsp )
581600
582601 def remove_add_prep_template (self , fp_rpt , study , data_type_id ,
0 commit comments