コード例 #1
0
 def get(self, rs_id):
     """
     Get all SNPs by rs-ID short info
     """
     all_args = pagination_parser.parse_args()
     filters = self.release_service.get_filters_by_rs_id(rs_id) + \
         self.release_service.get_filters_by_fdr(default_fdr_tr(int(self.used_release.version)))
     result = self.paginate(all_args, extra_filters=filters)
     return {
         'results': result,
         'total': self.items_count(extra_filters=filters)
     }
コード例 #2
0
            def get(self, gene_id):
                """
                Get all SNPs by eqtl target gene encode id short info
                """
                all_args = pagination_parser.parse_args()
                gene = self.release_service.get_gene_by_id(gene_id)
                if gene is None:
                    return {'results': [], 'gene': None, 'total': 0}
                gene.locus_start, gene.locus_end = self.release_service.get_gene_locus(
                    gene)
                filters = self.release_service.get_filters_by_eqtl_gene(gene) + \
                    self.release_service.get_filters_by_fdr(default_fdr_tr(int(self.used_release.version)))
                result = self.paginate(all_args, extra_filters=filters)

                return {
                    'results': result,
                    'gene': gene,
                    'total': self.items_count(extra_filters=filters)
                }
コード例 #3
0
    def get_snps_by_advanced_filters_tsv_with_targets(self, filters_object):
        file = tempfile.NamedTemporaryFile('wt', suffix='.tsv')
        csv_writer = csv.writer(file, dialect=TsvDialect)

        headers = ['Chromosome', 'Position', 'Ref', 'Alt', 'rsID']
        names = dict(zip(headers, ['chromosome', 'position', 'ref', 'alt', 'rs_id']))

        join_tuples = []
        additional_columns = []
        query_args = [self.SNP]
        aliases = dict()

        if int(self.release.version) >= 3:
            if not filters_object['fdr']:
                filters_object['fdr'] = default_fdr_tr(int(self.release.version))
            if not filters_object['es']:
                filters_object['es'] = default_es_tr(int(self.release.version))

        for what_for in ('TF', 'CL'):
            aggregation_class = {'TF': self.TranscriptionFactor, 'CL': self.CellLine}[what_for]
            aggregated_snp_class = {'TF': self.TranscriptionFactorSNP, 'CL': self.CellLineSNP}[what_for]
            id_field = {'TF': 'tf_id', 'CL': 'cl_id'}[what_for]
            agr_snp_class_alias = aliased(aggregated_snp_class)
            agr_class_alias = aliased(aggregation_class)
            query_args.append(self.release.db.func.group_concat(agr_class_alias.name.distinct()))
            headers.append('{}-ASBs'.format({'TF': 'TF', 'CL': 'Cell type'}[what_for]))

            if int(self.release.version) >= 3:
                join_tuples += [
                    (
                        agr_snp_class_alias,
                        (agr_snp_class_alias.chromosome == self.SNP.chromosome) &
                        (agr_snp_class_alias.position == self.SNP.position) &
                        (agr_snp_class_alias.alt == self.SNP.alt) &
                        (agr_snp_class_alias.fdr_class.in_(get_corresponding_fdr_classes(filters_object['fdr']))) &
                        (agr_snp_class_alias.es_class.in_(get_corresponding_es_classes(filters_object['es']))),
                    ),
                    (
                        agr_class_alias,
                        getattr(agr_class_alias, id_field) == getattr(agr_snp_class_alias, id_field),
                    )
                ]
            else:
                join_tuples += [
                    (
                        agr_snp_class_alias,
                        (agr_snp_class_alias.chromosome == self.SNP.chromosome) &
                        (agr_snp_class_alias.position == self.SNP.position) &
                        (agr_snp_class_alias.alt == self.SNP.alt),
                    ),
                    (
                        agr_class_alias,
                        getattr(agr_class_alias, id_field) == getattr(agr_snp_class_alias, id_field),
                    )
                ]

        for what_for in ('TF', 'CL'):
            filter_object_key = {'TF': 'transcription_factors', 'CL': 'cell_types'}[what_for]
            aggregation_class = {'TF': self.TranscriptionFactor, 'CL': self.CellLine}[what_for]
            aggregated_snp_class = {'TF': self.TranscriptionFactorSNP, 'CL': self.CellLineSNP}[what_for]
            id_field = {'TF': 'tf_id', 'CL': 'cl_id'}[what_for]
            if filters_object[filter_object_key]:
                for name in filters_object[filter_object_key]:
                    aliases[name] = aliased(aggregated_snp_class)
                    aggregation_entity = aggregation_class.query.filter_by(name=name).one_or_none()
                    if not aggregation_entity:
                        continue
                    aggregation_id = getattr(aggregation_entity, id_field)
                    join_tuples.append(
                        (aliases[name],
                         (getattr(
                             aliases[name],
                             {'TF': 'tf_id', 'CL': 'cl_id'}[what_for]
                         ) == aggregation_id) &
                         (aliases[name].chromosome == self.SNP.chromosome) &
                         (aliases[name].position == self.SNP.position) &
                         (aliases[name].alt == self.SNP.alt))
                    )
                    for field, label in [
                        ('log_p_value_ref', '{}_FDR_Ref'.format(name)),
                        ('log_p_value_alt', '{}_FDR_Alt'.format(name)),
                        ('es_ref', '{}_Effect_Size_Ref'.format(name)),
                        ('es_alt', '{}_Effect_Size_Alt'.format(name)),
                    ]:
                        headers.append(label)
                        additional_columns.append(
                            self.release.db.func.coalesce(getattr(aliases[name], field)).label(label))

        Target = aliased(self.Gene)
        join_tuples.append((Target, self.SNP.target_genes))
        additional_columns.append(self.release.db.func.group_concat(Target.gene_name.distinct()).label('targets'))
        headers.append('Targets')

        found_snps = self.release.session.query(*query_args)
        found_snps = found_snps.filter(*self.construct_advanced_filters(filters_object))
        for cls, condition in join_tuples:
            found_snps = found_snps.join(cls, condition, isouter=True)
        found_snps = found_snps.add_columns(*additional_columns)
        found_snps = found_snps.group_by(self.SNP)

        csv_writer.writerow(headers)
        for tup in found_snps:
            snp = tup[0]
            columns = tup[1:]
            csv_writer.writerow(
                [getattr(snp, names[header]) if names[header] != 'rs_id' else 'rs' + str(getattr(snp, names[header]))
                 for header in headers[:len(names.keys())]] + list(columns))

        file.flush()
        return send_file(
            file.name,
            cache_timeout=0,
            mimetype="text/tsv",
            as_attachment=True
        )
コード例 #4
0
    def construct_advanced_filters(self, filters_object):
        filters = []
        if int(self.release.version) >= 3:
            if not filters_object['fdr']:
                filters_object['fdr'] = default_fdr_tr(int(self.release.version))
            if not filters_object['es']:
                filters_object['es'] = default_es_tr(int(self.release.version))
            if filters_object['transcription_factors']:
                filters += [self.SNP.tf_aggregated_snps.any(
                    (self.TranscriptionFactorSNP.tf_id == getattr(self.TranscriptionFactor.query.filter(
                        self.TranscriptionFactor.name == tf_name
                    ).one_or_none(), 'tf_id', None)) &
                    (self.TranscriptionFactorSNP.fdr_class.in_(get_corresponding_fdr_classes(filters_object['fdr']))) &
                    (self.TranscriptionFactorSNP.es_class.in_(get_corresponding_es_classes(filters_object['es']))))
                    for tf_name in filters_object['transcription_factors']]

            if filters_object['cell_types']:
                filters += [self.SNP.cl_aggregated_snps.any(
                    (self.CellLineSNP.cl_id == getattr(self.CellLine.query.filter(
                        self.CellLine.name == cl_name
                    ).one_or_none(), 'cl_id', None)) &
                    (self.CellLineSNP.fdr_class.in_(get_corresponding_fdr_classes(filters_object['fdr']))) &
                    (self.CellLineSNP.es_class.in_(get_corresponding_es_classes(filters_object['es']))))
                    for cl_name in filters_object['cell_types']]

            if not filters_object['transcription_factors'] and not filters_object['cell_types']:
                filters += self.get_filters_by_fdr(filters_object['fdr']) + \
                           self.get_filters_by_es(filters_object['es'])

            if filters_object['motif_concordance']:
                search_null = False
                if 'None' in filters_object['motif_concordance']:
                    search_null = True
                    filters_object['motif_concordance'] = [x for x in filters_object['motif_concordance'] if
                                                           x != 'None']
                if filters_object['transcription_factors']:
                    filters += [self.SNP.tf_aggregated_snps.any(
                        (self.TranscriptionFactorSNP.motif_concordance.in_(filters_object['motif_concordance']) |
                         (self.TranscriptionFactorSNP.motif_concordance.is_(None) if search_null else False)) &
                        self.TranscriptionFactorSNP.transcription_factor.has(
                            self.TranscriptionFactor.name.in_(filters_object['transcription_factors'])
                        ) & (self.TranscriptionFactorSNP.fdr_class.in_(
                            get_corresponding_fdr_classes(filters_object['fdr'])))
                        & (self.TranscriptionFactorSNP.es_class.in_(
                            get_corresponding_es_classes(filters_object['es'])))
                    )]
                else:
                    filters += [self.SNP.tf_aggregated_snps.any(
                        (self.TranscriptionFactorSNP.motif_concordance.in_(filters_object['motif_concordance']) |
                         (self.TranscriptionFactorSNP.motif_concordance.is_(None) if search_null else False)) &
                        (self.TranscriptionFactorSNP.fdr_class.in_(
                            get_corresponding_fdr_classes(filters_object['fdr']))) &
                        (self.TranscriptionFactorSNP.es_class.in_(
                            get_corresponding_es_classes(filters_object['es'])))
                    ) | (~self.SNP.tf_aggregated_snps.any() if search_null else False)]
        else:
            if filters_object['transcription_factors']:
                filters += [self.SNP.tf_aggregated_snps.any(
                    self.TranscriptionFactorSNP.tf_id == getattr(self.TranscriptionFactor.query.filter(
                        self.TranscriptionFactor.name == tf_name
                    ).one_or_none(), 'tf_id', None))
                    for tf_name in filters_object['transcription_factors']]

            if filters_object['cell_types']:
                filters += [self.SNP.cl_aggregated_snps.any(
                    self.CellLineSNP.cl_id == getattr(self.CellLine.query.filter(
                        self.CellLine.name == cl_name
                    ).one_or_none(), 'cl_id', None))
                    for cl_name in filters_object['cell_types']]

            if filters_object['motif_concordance']:
                search_null = False
                if 'None' in filters_object['motif_concordance']:
                    search_null = True
                    filters_object['motif_concordance'] = [x for x in filters_object['motif_concordance'] if
                                                           x != 'None']
                if filters_object['transcription_factors']:
                    filters += [self.SNP.tf_aggregated_snps.any(
                        (self.TranscriptionFactorSNP.motif_concordance.in_(filters_object['motif_concordance']) |
                         (self.TranscriptionFactorSNP.motif_concordance.is_(None) if search_null else False)) &
                        self.TranscriptionFactorSNP.transcription_factor.has(
                            self.TranscriptionFactor.name.in_(filters_object['transcription_factors'])
                        )
                    )]
                else:
                    filters += [self.SNP.tf_aggregated_snps.any(
                        self.TranscriptionFactorSNP.motif_concordance.in_(filters_object['motif_concordance']) |
                        (self.TranscriptionFactorSNP.motif_concordance.is_(None) if search_null else False)
                    ) | (~self.SNP.tf_aggregated_snps.any() if search_null else False)]

        if filters_object['chromosome']:
            if not filters_object['start'] or not filters_object['end']:
                filters += [self.SNP.chromosome == filters_object['chromosome']]
            else:
                filters += [self.SNP.chromosome == filters_object['chromosome'],
                            self.SNP.position.between(filters_object['start'], filters_object['end'])]

        if filters_object['phenotype_databases']:
            filters += [or_(*(getattr(self.SNP, db_name_property_dict[phenotype_db])
                              for phenotype_db in filters_object['phenotype_databases']))]

        return filters