Exemplo n.º 1
0
    def test_is_localhost(self):
        self.assertTrue(is_localhost('127.0.0.1'))
        self.assertTrue(is_localhost('localhost'))
        self.assertTrue(is_localhost('127.0.0.1:21174'))

        self.assertFalse(is_localhost('10.0.0.1'))
        self.assertFalse(is_localhost('10.0.0.1:21174'))
Exemplo n.º 2
0
    def test_is_localhost(self):
        self.assertTrue(is_localhost('127.0.0.1'))
        self.assertTrue(is_localhost('localhost'))
        self.assertTrue(is_localhost('127.0.0.1:21174'))

        self.assertFalse(is_localhost('10.0.0.1'))
        self.assertFalse(is_localhost('10.0.0.1:21174'))
Exemplo n.º 3
0
    def get(self):
        message = self.get_argument('message', '')
        level = self.get_argument('level', '')
        user = self.current_user

        analyses = user.shared_analyses | user.private_analyses

        is_local_request = is_localhost(self.request.headers['host'])
        gfi = partial(get_filepath_id, 'analysis')
        dlop = partial(download_link_or_path, is_local_request)
        mappings = {}
        bioms = {}
        for analysis in analyses:
            _id = analysis.id
            mapping = analysis.mapping_file
            if mapping is not None:
                mappings[_id] = dlop(mapping, gfi(mapping), 'mapping file')
            else:
                mappings[_id] = ''
            links = [
                dlop(f, gfi(f), l) for l, f in viewitems(analysis.biom_tables)
            ]
            bioms[_id] = '\n'.join(links)

        self.render("show_analyses.html",
                    analyses=analyses,
                    message=message,
                    level=level,
                    is_local_request=is_local_request,
                    mappings=mappings,
                    bioms=bioms)
Exemplo n.º 4
0
    def get(self):
        message = self.get_argument('message', '')
        level = self.get_argument('level', '')
        user = self.current_user

        analyses = user.shared_analyses | user.private_analyses

        is_local_request = is_localhost(self.request.headers['host'])
        gfi = partial(get_filepath_id, 'analysis')
        dlop = partial(download_link_or_path, is_local_request)
        mappings = {}
        bioms = {}
        tgzs = {}
        for analysis in analyses:
            _id = analysis.id
            # getting mapping file
            mapping = analysis.mapping_file
            if mapping is not None:
                mappings[_id] = dlop(mapping, gfi(mapping), 'mapping file')
            else:
                mappings[_id] = ''
            # getting biom tables
            links = [dlop(f, gfi(f), l)
                     for l, f in viewitems(analysis.biom_tables)]
            bioms[_id] = '\n'.join(links)
            # getting tgz file
            tgz = analysis.tgz
            if tgz is not None:
                tgzs[_id] = dlop(tgz, gfi(tgz), 'tgz file')
            else:
                tgzs[_id] = ''

        self.render("show_analyses.html", analyses=analyses, message=message,
                    level=level, is_local_request=is_local_request,
                    mappings=mappings, bioms=bioms, tgzs=tgzs)
Exemplo n.º 5
0
    def get(self):
        message = self.get_argument('message', '')
        level = self.get_argument('level', '')
        user = self.current_user

        analyses = user.shared_analyses | user.private_analyses

        is_local_request = is_localhost(self.request.headers['host'])
        dlop = partial(download_link_or_path, is_local_request)
        mappings = {}
        bioms = {}
        tgzs = {}
        for analysis in analyses:
            _id = analysis.id
            mappings[_id], bioms[_id], tgzs[_id] = '', '', ''
            for fid, fp, fpt in retrieve_filepaths('analysis_filepath',
                                                   'analysis_id', _id):
                if fpt == 'plain_text':
                    mappings[_id] = dlop(fp, fid, 'mapping file')
                if fpt == 'biom':
                    bioms[_id] = dlop(fp, fid, 'biom file')
                if fpt == 'tgz':
                    tgzs[_id] = dlop(fp, fid, 'tgz file')

        self.render("list_analyses.html",
                    analyses=analyses,
                    message=message,
                    level=level,
                    is_local_request=is_local_request,
                    mappings=mappings,
                    bioms=bioms,
                    tgzs=tgzs)
Exemplo n.º 6
0
    def render(self, study):
        study_info = study.info
        id = study.id
        abstract = study_info['study_abstract']
        description = study_info['study_description']
        pmids = ", ".join([pubmed_linkifier([pmid]) for pmid in study.pmids])
        princ_inv = StudyPerson(study_info['principal_investigator_id'])
        pi_link = study_person_linkifier((princ_inv.email, princ_inv.name))
        number_samples_promised = study_info['number_samples_promised']
        number_samples_collected = study_info['number_samples_collected']
        metadata_complete = study_info['metadata_complete']
        data_types = sorted(viewitems(get_data_types()), key=itemgetter(1))

        # Retrieve the files from the uploads folder, so the user can choose
        # the sample template of the study. Filter them to only include the
        # ones that ends with 'txt' or 'tsv'.
        files = [f for _, f in get_files_from_uploads_folders(str(study.id))
                 if f.endswith(('txt', 'tsv'))]

        # If the sample template exists, retrieve all its filepaths
        if SampleTemplate.exists(study.id):
            sample_templates = SampleTemplate(study.id).get_filepaths()
        else:
            # If the sample template does not exist, just pass an empty list
            sample_templates = []

        # Check if the request came from a local source
        is_local_request = is_localhost(self.request.headers['host'])

        # The user can choose the sample template only if the study is
        # sandboxed or the current user is an admin
        show_select_sample = (
            study.status == 'sandbox' or self.current_user.level == 'admin')

        # Ebi information
        ebi_status = study.ebi_submission_status
        ebi_accession = study.ebi_study_accession
        if ebi_accession:
            ebi_accession = (EBI_LINKIFIER.format(ebi_accession))

        return self.render_string(
            "study_description_templates/study_information_tab.html",
            abstract=abstract,
            description=description,
            id=id,
            pmids=pmids,
            principal_investigator=pi_link,
            number_samples_promised=number_samples_promised,
            number_samples_collected=number_samples_collected,
            metadata_complete=metadata_complete,
            show_select_sample=show_select_sample,
            files=files,
            study_id=study.id,
            sample_templates=sample_templates,
            is_local_request=is_local_request,
            data_types=data_types,
            ebi_status=ebi_status,
            ebi_accession=ebi_accession)
Exemplo n.º 7
0
    def render(self, study_id, processed_data, allow_approval,
               approval_deny_msg):
        user = self.current_user
        # The request approval, approve processed data and make public buttons
        # are mutually exclusive. Only one of them will be shown, depending on
        # the current status of the processed data
        status = processed_data.visibility
        btn_to_show = None
        if status == 'sandbox' and qiita_config.require_approval:
            # The request approval button only appears if the processed data is
            # sandboxed and the qiita_config specifies that the approval should
            # be requested
            btn_to_show = 'request_approval'
        elif (user.level == 'admin' and status == 'awaiting_approval'
              and qiita_config.require_approval):
            # The approve processed data button only appears if the user is an
            # admin, the processed data is waiting to be approved and the qiita
            # config requires processed data approval
            btn_to_show = 'approve'
        elif status == 'private':
            # The make public button only appears if the status is private
            btn_to_show = 'make_public'

        # The revert to sandbox button only appears if the processed data is
        # not sandboxed or public
        show_revert_btn = status not in {'sandbox', 'public'}

        # process data can only have one preprocess_data
        preprocessed_data_id = processed_data.parents[0].id
        process_date = str(processed_data.timestamp)
        filepaths = processed_data.filepaths
        is_local_request = is_localhost(self.request.headers['host'])

        return self.render_string(
            "study_description_templates/processed_data_info_tab.html",
            pd_id=processed_data.id,
            preprocessed_data_id=preprocessed_data_id,
            process_date=process_date,
            filepaths=filepaths,
            is_local_request=is_local_request,
            btn_to_show=btn_to_show,
            show_revert_btn=show_revert_btn,
            allow_approval=allow_approval,
            approval_deny_msg=approval_deny_msg)
Exemplo n.º 8
0
    def render(self, study_id, processed_data, allow_approval,
               approval_deny_msg):
        user = self.current_user
        # The request approval, approve processed data and make public buttons
        # are mutually exclusive. Only one of them will be shown, depending on
        # the current status of the processed data
        status = processed_data.status
        btn_to_show = None
        if status == 'sandbox' and qiita_config.require_approval:
            # The request approval button only appears if the processed data is
            # sandboxed and the qiita_config specifies that the approval should
            # be requested
            btn_to_show = 'request_approval'
        elif (user.level == 'admin' and status == 'awaiting_approval' and
                qiita_config.require_approval):
            # The approve processed data button only appears if the user is an
            # admin, the processed data is waiting to be approved and the qiita
            # config requires processed data approval
            btn_to_show = 'approve'
        elif status == 'private':
            # The make public button only appears if the status is private
            btn_to_show = 'make_public'

        # The revert to sandbox button only appears if the processed data is
        # not sandboxed or public
        show_revert_btn = status not in {'sandbox', 'public'}

        pd_id = processed_data.id
        preprocessed_data_id = processed_data.preprocessed_data
        process_date = processed_data.processing_info['processed_date']
        filepaths = processed_data.get_filepaths()
        is_local_request = is_localhost(self.request.headers['host'])

        return self.render_string(
            "study_description_templates/processed_data_info_tab.html",
            pd_id=pd_id,
            preprocessed_data_id=preprocessed_data_id,
            process_date=process_date,
            filepaths=filepaths,
            is_local_request=is_local_request,
            btn_to_show=btn_to_show,
            show_revert_btn=show_revert_btn,
            allow_approval=allow_approval,
            approval_deny_msg=approval_deny_msg)
Exemplo n.º 9
0
    def get(self):
        user = self.current_user
        is_local_request = is_localhost(self.request.headers['host'])

        uanalyses = user.shared_analyses | user.private_analyses
        user_analysis_ids = set([a.id for a in uanalyses])

        panalyses = Analysis.get_by_status('public')
        public_analysis_ids = set([a.id for a in panalyses])
        public_analysis_ids = public_analysis_ids - user_analysis_ids

        user_analyses = generate_analysis_list(user_analysis_ids)
        public_analyses = generate_analysis_list(public_analysis_ids, True)

        dlop = partial(download_link_or_path, is_local_request)

        messages = {'info': '', 'danger': ''}
        for analysis_id in user_analysis_ids:
            job_info = r_client.get('analysis_delete_%d' % analysis_id)
            if job_info:
                job_info = defaultdict(lambda: '', loads(job_info))
                job_id = job_info['job_id']
                job = ProcessingJob(job_id)
                job_status = job.status
                processing = job_status not in ('success', 'error')
                if processing:
                    messages['info'] += ('Analysis %s is being deleted<br/>' %
                                         analysis_id)
                elif job_status == 'error':
                    messages['danger'] += (job.log.msg.replace('\n', '<br/>') +
                                           '<br/>')
                else:
                    if job_info['alert_type'] not in messages:
                        messages[job_info['alert_type']] = []
                    messages[job_info['alert_type']] += (
                        job.log.msg.replace('\n', '<br/>') + '<br/>')

        self.render("list_analyses.html",
                    user_analyses=user_analyses,
                    public_analyses=public_analyses,
                    messages=messages,
                    dlop=dlop)
Exemplo n.º 10
0
    def get(self):
        user = self.current_user
        is_local_request = is_localhost(self.request.headers['host'])

        uanalyses = user.shared_analyses | user.private_analyses
        user_analysis_ids = set([a.id for a in uanalyses])

        panalyses = Analysis.get_by_status('public')
        public_analysis_ids = set([a.id for a in panalyses])
        public_analysis_ids = public_analysis_ids - user_analysis_ids

        user_analyses = generate_analysis_list(user_analysis_ids)
        public_analyses = generate_analysis_list(public_analysis_ids, True)

        dlop = partial(download_link_or_path, is_local_request)

        messages = {'info': '', 'danger': ''}
        for analysis_id in user_analysis_ids:
            job_info = r_client.get('analysis_delete_%d' % analysis_id)
            if job_info:
                job_info = defaultdict(lambda: '', loads(job_info))
                job_id = job_info['job_id']
                job = ProcessingJob(job_id)
                job_status = job.status
                processing = job_status not in ('success', 'error')
                if processing:
                    messages['info'] += (
                        'Analysis %s is being deleted<br/>' % analysis_id)
                elif job_status == 'error':
                    messages['danger'] += (
                        job.log.msg.replace('\n', '<br/>') + '<br/>')
                else:
                    if job_info['alert_type'] not in messages:
                        messages[job_info['alert_type']] = []
                    messages[job_info['alert_type']] += (
                        job.log.msg.replace('\n', '<br/>') + '<br/>')

        self.render("list_analyses.html", user_analyses=user_analyses,
                    public_analyses=public_analyses, messages=messages,
                    dlop=dlop)
Exemplo n.º 11
0
    def render(self, study_id, preprocessed_data):
        user = self.current_user
        ppd_id = preprocessed_data.id
        ebi_status = preprocessed_data.submitted_to_insdc_status()
        ebi_study_accession = preprocessed_data.ebi_study_accession
        ebi_submission_accession = preprocessed_data.ebi_submission_accession
        vamps_status = preprocessed_data.submitted_to_vamps_status()
        filepaths = preprocessed_data.get_filepaths()
        is_local_request = is_localhost(self.request.headers['host'])
        show_ebi_btn = user.level == "admin"
        processing_status = convert_text_html(
            preprocessed_data.processing_status)
        processed_data = sorted(preprocessed_data.processed_data)

        # 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']

        if PrepTemplate.exists(preprocessed_data.prep_template):
            prep_template_id = preprocessed_data.prep_template
            prep_template = PrepTemplate(prep_template_id)
            raw_data_id = prep_template.raw_data
            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 ProcessedSortmernaParams.iter()}
        # We just need to provide an ID for the default parameters,
        # so we can initialize the interface
        default_params = 1

        return self.render_string(
            "study_description_templates/preprocessed_data_info_tab.html",
            ppd_id=ppd_id,
            show_ebi_btn=show_ebi_btn,
            ebi_status=ebi_status,
            ebi_study_accession=ebi_study_accession,
            ebi_submission_accession=ebi_submission_accession,
            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,
            processing_status=processing_status,
            processed_data=processed_data)
Exemplo n.º 12
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_filetypes())),
            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_id = prep_template.raw_data
        preprocess_options = []
        preprocessed_data = None
        show_preprocess_btn = True
        no_preprocess_msg = None
        if raw_data_id:
            rd = RawData(raw_data_id)
            rd_ft = rd.filetype

            # If the prep template has a raw data associated, it can be
            # preprocessed. Retrieve the pre-processing parameters
            if rd_ft in ('SFF', 'FASTA'):
                param_iter = Preprocessed454Params.iter()
            elif rd_ft == 'FASTQ':
                param_iter = [pip for pip in PreprocessedIlluminaParams.iter()
                              if pip.values['barcode_type'] != 'not-barcoded']
            elif rd_ft == 'per_sample_FASTQ':
                param_iter = [pip for pip in PreprocessedIlluminaParams.iter()
                              if pip.values['barcode_type'] == 'not-barcoded']
            else:
                raise NotImplementedError(
                    "Pre-processing of %s files currently not supported."
                    % rd_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 = prep_template.preprocessed_data

            # Check if the template have all the required columns for
            # preprocessing
            raw_data_files = rd.get_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 rd_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))

        preprocessing_status = prep_template.preprocessing_status

        return self.render_string(
            "study_description_templates/prep_template_info_tab.html",
            pt_id=prep_template.id,
            study_id=study.id,
            raw_data=raw_data_id,
            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,
            show_preprocess_btn=show_preprocess_btn,
            no_preprocess_msg=no_preprocess_msg)
Exemplo n.º 13
0
    def render(self, study):
        study_info = study.info
        id = study.id
        abstract = study_info['study_abstract']
        description = study_info['study_description']
        publications = []
        for doi, pmid in study.publications:
            if doi is not None:
                publications.append(doi_linkifier([doi]))
            if pmid is not None:
                publications.append(pubmed_linkifier([pmid]))
        publications = ", ".join(publications)
        princ_inv = StudyPerson(study_info['principal_investigator_id'])
        pi_link = study_person_linkifier((princ_inv.email, princ_inv.name))
        number_samples_promised = study_info['number_samples_promised']
        number_samples_collected = study_info['number_samples_collected']
        metadata_complete = study_info['metadata_complete']

        data_types = sorted(viewitems(get_data_types()), key=itemgetter(1))

        # Retrieve the files from the uploads folder, so the user can choose
        # the sample template of the study. Filter them to only include the
        # ones that ends with 'txt' or 'tsv'.
        files = [
            f for _, f in get_files_from_uploads_folders(str(study.id))
            if f.endswith(('txt', 'tsv'))
        ]

        # If the sample template exists, retrieve all its filepaths
        if SampleTemplate.exists(study.id):
            sample_templates = SampleTemplate(study.id).get_filepaths()
        else:
            # If the sample template does not exist, just pass an empty list
            sample_templates = []

        # Check if the request came from a local source
        is_local_request = is_localhost(self.request.headers['host'])

        # The user can choose the sample template only if the study is
        # sandboxed or the current user is an admin
        show_select_sample = (study.status == 'sandbox'
                              or self.current_user.level == 'admin')

        # EBI information
        ebi_status = study.ebi_submission_status
        ebi_accession = study.ebi_study_accession
        if ebi_accession:
            ebi_accession = (EBI_LINKIFIER.format(ebi_accession))

        return self.render_string(
            "study_description_templates/study_information_tab.html",
            abstract=abstract,
            description=description,
            id=id,
            publications=publications,
            principal_investigator=pi_link,
            number_samples_promised=number_samples_promised,
            number_samples_collected=number_samples_collected,
            metadata_complete=metadata_complete,
            show_select_sample=show_select_sample,
            files=files,
            study_id=study.id,
            sample_templates=sample_templates,
            is_local_request=is_local_request,
            data_types=data_types,
            ebi_status=ebi_status,
            ebi_accession=ebi_accession)
Exemplo n.º 14
0
    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)
Exemplo n.º 15
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)
Exemplo n.º 16
0
    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)