def render(self, study_id, preprocessed_data):
        user = self.current_user
        ppd_id = preprocessed_data.id
        vamps_status = preprocessed_data.is_submitted_to_vamps
        filepaths = preprocessed_data.filepaths
        is_local_request = is_localhost(self.request.headers['host'])
        show_ebi_btn = user.level == "admin"
        processing_status, processing_status_msg = \
            get_artifact_processing_status(preprocessed_data)
        processed_data = sorted([pd.id for pd in preprocessed_data.children])

        # Get all the ENA terms for the investigation type
        ontology = Ontology(convert_to_id('ENA', 'ontology'))
        # make "Other" show at the bottom of the drop down menu
        ena_terms = []
        for v in sorted(ontology.terms):
            if v != 'Other':
                ena_terms.append('<option value="%s">%s</option>' % (v, v))
        ena_terms.append('<option value="Other">Other</option>')

        # New Type is for users to add a new user-defined investigation type
        user_defined_terms = ontology.user_defined_terms + ['New Type']

        # ppd can only have 1 prep template
        prep_template = preprocessed_data.prep_templates[0]
        # this block might seem wrong but is here due to a possible
        # pathological case that we used to have in the system: preprocessed
        # data without valid prep_templates
        prep_templates = preprocessed_data.prep_templates
        if len(prep_templates) == 1:
            prep_template_id = prep_template.id
            raw_data_id = prep_template.artifact.id
            inv_type = prep_template.investigation_type or "None selected"
        else:
            prep_template_id = None
            raw_data_id = None
            inv_type = "None Selected"

        process_params = {
            param.id: (generate_param_str(param), param.name)
            for param in Command(3).default_parameter_sets
        }
        # We just need to provide an ID for the default parameters,
        # so we can initialize the interface
        default_params = min(process_params.keys())

        ebi_link = None
        if preprocessed_data.is_submitted_to_ebi:
            ebi_link = EBI_LINKIFIER.format(
                Study(study_id).ebi_study_accession)

        return self.render_string(
            "study_description_templates/preprocessed_data_info_tab.html",
            ppd_id=ppd_id,
            show_ebi_btn=show_ebi_btn,
            filepaths=filepaths,
            is_local_request=is_local_request,
            prep_template_id=prep_template_id,
            raw_data_id=raw_data_id,
            inv_type=inv_type,
            ena_terms=ena_terms,
            vamps_status=vamps_status,
            user_defined_terms=user_defined_terms,
            process_params=process_params,
            default_params=default_params,
            study_id=preprocessed_data.study.id,
            processing_status=processing_status,
            processing_status_msg=processing_status_msg,
            processed_data=processed_data,
            ebi_link=ebi_link)
Example #2
0
    def render(self, study, prep_template, full_access, ena_terms,
               user_defined_terms):
        user = self.current_user
        is_local_request = is_localhost(self.request.headers['host'])

        template_fps = []
        qiime_fps = []
        # Unfortunately, both the prep template and the qiime mapping files
        # have the sample type. The way to differentiate them is if we have
        # the substring 'qiime' in the basename
        for id_, fp in prep_template.get_filepaths():
            if 'qiime' in basename(fp):
                qiime_fps.append(
                    download_link_or_path(is_local_request, fp, id_,
                                          'Qiime mapping'))
            else:
                template_fps.append(
                    download_link_or_path(is_local_request, fp, id_,
                                          'Prep template'))

        # Since get_filepaths returns the paths sorted from newest to oldest,
        # the first in both list is the latest one
        current_template_fp = template_fps[0]
        current_qiime_fp = qiime_fps[0]

        if len(template_fps) > 1:
            show_old_templates = True
            old_templates = template_fps[1:]
        else:
            show_old_templates = False
            old_templates = None

        if len(qiime_fps) > 1:
            show_old_qiime_fps = True
            old_qiime_fps = qiime_fps[1:]
        else:
            show_old_qiime_fps = False
            old_qiime_fps = None

        filetypes = sorted(((ft, ft_id, fp_type_by_ft[ft])
                            for ft, ft_id in viewitems(get_artifact_types())),
                           key=itemgetter(1))
        files = [f for _, f in get_files_from_uploads_folders(str(study.id))]

        other_studies_rd = sorted(viewitems(_get_accessible_raw_data(user)))

        # A prep template can be modified if its status is sandbox
        is_editable = prep_template.status == 'sandbox'

        raw_data = prep_template.artifact
        preprocess_options = []
        preprocessed_data = None
        show_preprocess_btn = True
        no_preprocess_msg = None
        preprocessing_status = 'Not processed'
        preprocessing_status_msg = ""
        if raw_data:
            raw_data_ft = raw_data.artifact_type
            # If the prep template has a raw data associated, it can be
            # preprocessed. Retrieve the pre-processing parameters
            # Hardcoding the command ids until the interface is refactored
            if raw_data_ft in ('SFF', 'FASTA'):
                param_iter = Command(2).default_parameter_sets
            elif raw_data_ft == 'FASTQ':
                param_iter = [
                    p for p in Command(1).default_parameter_sets
                    if p.values['barcode_type'] != 'not-barcoded'
                ]
            elif raw_data_ft == 'per_sample_FASTQ':
                param_iter = [
                    p for p in Command(1).default_parameter_sets
                    if p.values['barcode_type'] == 'not-barcoded'
                ]
            else:
                raise NotImplementedError(
                    "Pre-processing of %s files currently not supported." %
                    raw_data_ft)

            preprocess_options = []
            for param in param_iter:
                text = ("<b>%s:</b> %s" % (k, v)
                        for k, v in viewitems(param.values))
                preprocess_options.append(
                    (param.id, param.name, '<br>'.join(text)))
            preprocessed_data = raw_data.children

            # Check if the template have all the required columns for
            # preprocessing
            raw_data_files = raw_data.filepaths
            if len(raw_data_files) == 0:
                show_preprocess_btn = False
                no_preprocess_msg = (
                    "Preprocessing disabled because there are no files "
                    "linked with the Raw Data")
            else:
                if prep_template.data_type() in TARGET_GENE_DATA_TYPES:
                    raw_forward_fps = [
                        fp for _, fp, ftype in raw_data_files
                        if ftype == 'raw_forward_seqs'
                    ]
                    key = ('demultiplex_multiple'
                           if len(raw_forward_fps) > 1 else 'demultiplex')
                    missing_cols = prep_template.check_restrictions(
                        [PREP_TEMPLATE_COLUMNS_TARGET_GENE[key]])

                    if raw_data_ft == 'per_sample_FASTQ':
                        show_preprocess_btn = 'run_prefix' not in missing_cols
                    else:
                        show_preprocess_btn = len(missing_cols) == 0

                    no_preprocess_msg = None
                    if not show_preprocess_btn:
                        no_preprocess_msg = (
                            "Preprocessing disabled due to missing columns in "
                            "the prep template: %s" % ', '.join(missing_cols))

            # Check the processing status
            preprocessing_status, preprocessing_status_msg = \
                get_artifact_processing_status(raw_data)

        ebi_link = None
        if prep_template.is_submitted_to_ebi:
            ebi_link = EBI_LINKIFIER.format(study.ebi_study_accession)

        return self.render_string(
            "study_description_templates/prep_template_info_tab.html",
            raw_data=raw_data,
            current_template_fp=current_template_fp,
            current_qiime_fp=current_qiime_fp,
            show_old_templates=show_old_templates,
            old_templates=old_templates,
            show_old_qiime_fps=show_old_qiime_fps,
            old_qiime_fps=old_qiime_fps,
            filetypes=filetypes,
            files=files,
            other_studies_rd=other_studies_rd,
            prep_template=prep_template,
            study=study,
            ena_terms=ena_terms,
            user_defined_terms=user_defined_terms,
            investigation_type=prep_template.investigation_type,
            is_editable=is_editable,
            preprocess_options=preprocess_options,
            preprocessed_data=preprocessed_data,
            preprocessing_status=preprocessing_status,
            preprocessing_status_message=preprocessing_status_msg,
            show_preprocess_btn=show_preprocess_btn,
            no_preprocess_msg=no_preprocess_msg,
            ebi_link=ebi_link)
    def render(self, study, prep_template, full_access, ena_terms,
               user_defined_terms):
        user = self.current_user
        is_local_request = is_localhost(self.request.headers['host'])

        template_fps = []
        qiime_fps = []
        # Unfortunately, both the prep template and the qiime mapping files
        # have the sample type. The way to differentiate them is if we have
        # the substring 'qiime' in the basename
        for id_, fp in prep_template.get_filepaths():
            if 'qiime' in basename(fp):
                qiime_fps.append(
                    download_link_or_path(
                        is_local_request, fp, id_, 'Qiime mapping'))
            else:
                template_fps.append(
                    download_link_or_path(
                        is_local_request, fp, id_, 'Prep template'))

        # Since get_filepaths returns the paths sorted from newest to oldest,
        # the first in both list is the latest one
        current_template_fp = template_fps[0]
        current_qiime_fp = qiime_fps[0]

        if len(template_fps) > 1:
            show_old_templates = True
            old_templates = template_fps[1:]
        else:
            show_old_templates = False
            old_templates = None

        if len(qiime_fps) > 1:
            show_old_qiime_fps = True
            old_qiime_fps = qiime_fps[1:]
        else:
            show_old_qiime_fps = False
            old_qiime_fps = None

        filetypes = sorted(
            ((ft, ft_id, fp_type_by_ft[ft])
             for ft, ft_id in viewitems(get_artifact_types())),
            key=itemgetter(1))
        files = [f for _, f in get_files_from_uploads_folders(str(study.id))]

        other_studies_rd = sorted(viewitems(
            _get_accessible_raw_data(user)))

        # A prep template can be modified if its status is sandbox
        is_editable = prep_template.status == 'sandbox'

        raw_data = prep_template.artifact
        preprocess_options = []
        preprocessed_data = None
        show_preprocess_btn = True
        no_preprocess_msg = None
        preprocessing_status = 'Not processed'
        preprocessing_status_msg = ""
        if raw_data:
            raw_data_ft = raw_data.artifact_type
            # If the prep template has a raw data associated, it can be
            # preprocessed. Retrieve the pre-processing parameters
            # Hardcoding the command ids until the interface is refactored
            if raw_data_ft in ('SFF', 'FASTA'):
                param_iter = Command(2).default_parameter_sets
            elif raw_data_ft == 'FASTQ':
                param_iter = [p for p in Command(1).default_parameter_sets
                              if p.values['barcode_type'] != 'not-barcoded']
            elif raw_data_ft == 'per_sample_FASTQ':
                param_iter = [p for p in Command(1).default_parameter_sets
                              if p.values['barcode_type'] == 'not-barcoded']
            else:
                raise NotImplementedError(
                    "Pre-processing of %s files currently not supported."
                    % raw_data_ft)

            preprocess_options = []
            for param in param_iter:
                text = ("<b>%s:</b> %s" % (k, v)
                        for k, v in viewitems(param.values))
                preprocess_options.append((param.id,
                                           param.name,
                                           '<br>'.join(text)))
            preprocessed_data = raw_data.children

            # Check if the template have all the required columns for
            # preprocessing
            raw_data_files = raw_data.filepaths
            if len(raw_data_files) == 0:
                show_preprocess_btn = False
                no_preprocess_msg = (
                    "Preprocessing disabled because there are no files "
                    "linked with the Raw Data")
            else:
                if prep_template.data_type() in TARGET_GENE_DATA_TYPES:
                    raw_forward_fps = [fp for _, fp, ftype in raw_data_files
                                       if ftype == 'raw_forward_seqs']
                    key = ('demultiplex_multiple' if len(raw_forward_fps) > 1
                           else 'demultiplex')
                    missing_cols = prep_template.check_restrictions(
                        [PREP_TEMPLATE_COLUMNS_TARGET_GENE[key]])

                    if raw_data_ft == 'per_sample_FASTQ':
                        show_preprocess_btn = 'run_prefix' not in missing_cols
                    else:
                        show_preprocess_btn = len(missing_cols) == 0

                    no_preprocess_msg = None
                    if not show_preprocess_btn:
                        no_preprocess_msg = (
                            "Preprocessing disabled due to missing columns in "
                            "the prep template: %s" % ', '.join(missing_cols))

            # Check the processing status
            preprocessing_status, preprocessing_status_msg = \
                get_artifact_processing_status(raw_data)

        ebi_link = None
        if prep_template.is_submitted_to_ebi:
            ebi_link = EBI_LINKIFIER.format(study.ebi_study_accession)

        return self.render_string(
            "study_description_templates/prep_template_info_tab.html",
            raw_data=raw_data,
            current_template_fp=current_template_fp,
            current_qiime_fp=current_qiime_fp,
            show_old_templates=show_old_templates,
            old_templates=old_templates,
            show_old_qiime_fps=show_old_qiime_fps,
            old_qiime_fps=old_qiime_fps,
            filetypes=filetypes,
            files=files,
            other_studies_rd=other_studies_rd,
            prep_template=prep_template,
            study=study,
            ena_terms=ena_terms,
            user_defined_terms=user_defined_terms,
            investigation_type=prep_template.investigation_type,
            is_editable=is_editable,
            preprocess_options=preprocess_options,
            preprocessed_data=preprocessed_data,
            preprocessing_status=preprocessing_status,
            preprocessing_status_message=preprocessing_status_msg,
            show_preprocess_btn=show_preprocess_btn,
            no_preprocess_msg=no_preprocess_msg,
            ebi_link=ebi_link)
    def render(self, study_id, preprocessed_data):
        user = self.current_user
        ppd_id = preprocessed_data.id
        vamps_status = preprocessed_data.is_submitted_to_vamps
        filepaths = preprocessed_data.filepaths
        is_local_request = is_localhost(self.request.headers['host'])
        show_ebi_btn = user.level == "admin"
        processing_status, processing_status_msg = \
            get_artifact_processing_status(preprocessed_data)
        processed_data = sorted([pd.id for pd in preprocessed_data.children])

        # Get all the ENA terms for the investigation type
        ontology = Ontology(convert_to_id('ENA', 'ontology'))
        # make "Other" show at the bottom of the drop down menu
        ena_terms = []
        for v in sorted(ontology.terms):
            if v != 'Other':
                ena_terms.append('<option value="%s">%s</option>' % (v, v))
        ena_terms.append('<option value="Other">Other</option>')

        # New Type is for users to add a new user-defined investigation type
        user_defined_terms = ontology.user_defined_terms + ['New Type']

        # ppd can only have 1 prep template
        prep_template = preprocessed_data.prep_templates[0]
        # this block might seem wrong but is here due to a possible
        # pathological case that we used to have in the system: preprocessed
        # data without valid prep_templates
        prep_templates = preprocessed_data.prep_templates
        if len(prep_templates) == 1:
            prep_template_id = prep_template.id
            raw_data_id = prep_template.artifact.id
            inv_type = prep_template.investigation_type or "None selected"
        else:
            prep_template_id = None
            raw_data_id = None
            inv_type = "None Selected"

        process_params = {param.id: (generate_param_str(param), param.name)
                          for param in Command(3).default_parameter_sets}
        # We just need to provide an ID for the default parameters,
        # so we can initialize the interface
        default_params = min(process_params.keys())

        ebi_link = None
        if preprocessed_data.is_submitted_to_ebi:
            ebi_link = EBI_LINKIFIER.format(
                Study(study_id).ebi_study_accession)

        return self.render_string(
            "study_description_templates/preprocessed_data_info_tab.html",
            ppd_id=ppd_id,
            show_ebi_btn=show_ebi_btn,
            filepaths=filepaths,
            is_local_request=is_local_request,
            prep_template_id=prep_template_id,
            raw_data_id=raw_data_id,
            inv_type=inv_type,
            ena_terms=ena_terms,
            vamps_status=vamps_status,
            user_defined_terms=user_defined_terms,
            process_params=process_params,
            default_params=default_params,
            study_id=preprocessed_data.study.id,
            processing_status=processing_status,
            processing_status_msg=processing_status_msg,
            processed_data=processed_data,
            ebi_link=ebi_link)