Exemplo n.º 1
0
    def _collapse_region_docs(cls, docs):
        ''' If the document is a hit then find parent region; pad all regions for build_info.'''
        hits = [doc for doc in docs if doc.type() == 'hits']
        regions = [doc for doc in docs if doc.type() == 'region']

        if len(hits) > 0:
            regions = Region.hits_to_regions(hits)
            return regions
Exemplo n.º 2
0
def _collapse_region_docs(docs):
    ''' If the document is a hit then find parent region; pad all regions for build_info.'''
    hits = [doc for doc in docs if doc.type() == 'hits']
    regions = [doc for doc in docs if doc.type() == 'region']

    if len(hits) > 0:
        regions = Region.hits_to_regions(hits)
        for doc in hits:
            docs.remove(doc)

    regions = [Region.pad_region_doc(doc) for doc in regions]

    for doc in regions:
        if doc in docs:
            docs.remove(doc)
        docs.append(doc)

    return len(docs)
Exemplo n.º 3
0
    def get_overlapping_features(self, build, seqid, start, end):

        features = []
        study_hits = self.get_overlapping_hits(build, seqid, start, end)
        if len(study_hits) == 0:
            return features

        regions = Region.hits_to_regions(study_hits)
        for doc in regions:
            doc = Region.pad_region_doc(doc)
            loc = doc.get_position(build=build).split(':')
            pos = loc[1].replace(',', '').split('-')
            feature = {
                'name': doc.get_name(),
                'id': doc.doc_id(),
                'chr': loc[0],
                'start': int(pos[0]),
                'end': int(pos[1]) if len(pos) > 1 else int(pos[0]),
                'strand': doc.get_strand_as_int(),
                'attributes': {}
            }
            loci = doc.get_disease_loci()
            sub_features = []
            for locus in loci:
                attributes = {}
                loc = locus.get_position(build=build).split(':')
                pos = loc[1].replace(',', '').split('-')
                sub_feature = {
                    'name': locus.get_name(),
                    'id': locus.doc_id(),
                    'chr': loc[0],
                    'start': int(pos[0]),
                    'end': int(pos[1]) if len(pos) > 1 else int(pos[0]),
                    'strand': locus.get_strand_as_int()
                }
                if hasattr(locus, "disease") and getattr(locus, "disease") is not None:
                    attributes['disease'] = getattr(locus, "disease")
                sub_feature['attributes'] = attributes
                sub_features.append(sub_feature)
            feature['sub_features'] = sub_features
            features.append(ElasticObject(feature))

        return features
Exemplo n.º 4
0
    def filter_queryset(self, request, queryset, view):
        ''' Override this method to request feature locations. '''
        try:
            filterable = getattr(view, 'filter_fields', [])
            filters = dict([(k, v) for k, v in request.GET.items() if k in filterable])
            query_str = filters.get('feature', 'PTPN22')
            build = self._get_build(filters.get('build', settings.DEFAULT_BUILD))
            if query_str is None or query_str == '':
                return [ElasticObject(initial={'error': 'No feature name provided.'})]

            search_fields = ['id',
                             'symbol', 'dbxrefs.ensembl',
                             'region_name']
            sources = ['start', 'stop', 'seqid', 'chromosome',
                       'disease_loci']
            idxs = ElasticSettings.getattr('IDX')
            MARKER_IDX = ''

            if build == ElasticSettings.get_label('MARKER', label='build'):
                MARKER_IDX = 'MARKER'
            if MARKER_IDX == '':
                for idx in idxs:
                    if 'MARKER' in idx:
                        if build == ElasticSettings.get_label(idx, label='build'):
                            MARKER_IDX = idx

            (idx, idx_type) = ElasticSettings.idx_names(MARKER_IDX, 'MARKER')
            (idx_r, idx_type_r) = ElasticSettings.idx_names('REGION', 'REGION')
            (idx_g, idx_type_g) = ElasticSettings.idx_names('GENE', 'GENE')
            idx += ',' + idx_r + ',' + idx_g
            idx_type += ',' + idx_type_r + ',' + idx_type_g

            equery = BoolQuery(must_arr=Query.query_string(query_str, fields=search_fields))
            elastic = Search(search_query=ElasticQuery(equery, sources), size=10, idx=idx, idx_type=idx_type)
            docs = elastic.search().docs
            locs = []
            for doc in docs:
                if isinstance(doc, RegionDocument):
                    doc = Region.pad_region_doc(doc)

                loc = doc.get_position(build=build).split(':')
                pos = loc[1].replace(',', '').split('-')
                locs.append(ElasticObject(
                    {'feature': query_str,
                     'chr': loc[0],
                     'start': int(pos[0]),
                     'end': int(pos[1]) if len(pos) > 1 else int(pos[0]),
                     'locusString': query_str+" ("+str(loc[1])+")"}))
            return locs
        except (TypeError, ValueError, IndexError, ConnectionError):
            raise Http404
Exemplo n.º 5
0
def _collapse_region_docs(docs):
    ''' If the document is a hit then find parent region; pad all regions for build_info.'''
    hits = [doc for doc in docs if doc.type() == 'hits']
    regions = [doc for doc in docs if doc.type() == 'region']

    if len(hits) > 0:
        regions = Region.hits_to_regions(hits)
        for doc in hits:
            disease_locus = getattr(doc, "disease_locus")
            for region in regions:
                disease_loci = getattr(region, "disease_loci")
                if disease_locus in disease_loci:
                    region.__dict__['_meta']['highlight'] = doc.highlight()
            docs.remove(doc)

    regions = [Region.pad_region_doc(doc) for doc in regions]

    for doc in regions:
        if doc in docs:
            docs.remove(doc)
        docs.append(doc)

    return len(docs)
Exemplo n.º 6
0
    def get_context_data(self, **kwargs):
        context = super(RegionView, self).get_context_data(**kwargs)
        region = kwargs['region'] if 'region' in kwargs else self.request.GET.get('r')
        if region is None:
            messages.error(self.request, 'No region given.')
            raise Http404()

        if re.search("[p|q]\d+", region.lower()) is None:
            regionDocs = Region.loci_to_regions([region.lower()])
            if len(regionDocs) == 0:
                messages.error(self.request, 'No region found.')
                raise Http404()

            region = regionDocs[0].doc_id()
        return RegionView.get_region(self.request, region, context)
Exemplo n.º 7
0
def region_page(request, region):
    ''' Renders a region page. '''
    if region is None:
        messages.error(request, 'No region given.')
        raise Http404()
    query = ElasticQuery(Query.ids(region.split(',')))
    elastic = Search(query, idx=ElasticSettings.idx('REGION', 'REGION'), size=5)
    res = elastic.search()
    if res.hits_total == 0:
        messages.error(request, 'Region(s) '+region+' not found.')
    elif res.hits_total < 9:
        names = ', '.join([getattr(doc, 'region_name') for doc in res.docs])
        REGIONS = [Region.pad_region_doc(doc) for doc in res.docs]
        context = {'features': REGIONS, 'title': names}
        return render(request, 'region/index.html', context, content_type='text/html')
    raise Http404()
Exemplo n.º 8
0
    def get_region(cls, request, region, context):
        if region is None:
            messages.error(request, 'No region given.')
            raise Http404()
        query = ElasticQuery(Query.ids(region.split(',')))
        elastic = Search(query, idx=ElasticSettings.idx('REGION', 'REGION'), size=5)
        res = elastic.search()
        if res.hits_total == 0:
            messages.error(request, 'Region(s) '+region+' not found.')
        elif res.hits_total < 9:
            context['features'] = [Region.pad_region_doc(doc) for doc in res.docs]

            fids = [doc.doc_id() for doc in res.docs]
            criteria_disease_tags = RegionView.criteria_disease_tags(request, fids)
            context['criteria'] = criteria_disease_tags

            context['title'] = ', '.join([getattr(doc, 'region_name') for doc in res.docs])
            return context
        raise Http404()
Exemplo n.º 9
0
    def filter_queryset(self, request, queryset, view):
        ''' Override this method to request feature locations. '''
        try:
            filterable = getattr(view, 'filter_fields', [])
            filters = dict([(k, v) for k, v in request.GET.items() if k in filterable])
            ftype = filters.get('ftype').upper()
            build = self._get_build(filters.get('build', settings.DEFAULT_BUILD))
            if ftype is None or ftype == '':
                return [ElasticObject(initial={'error': 'No feature type provided.'})]

            idx = 'REGION' if ftype == 'ASSOC_SNP' else ftype
            idx_type = 'STUDY_HITS' if idx == 'REGION' else ftype
            print(idx_type)

            doc_class_str = ElasticSettings.get_label(idx, idx_type, label='class')
            doc_class = import_string(doc_class_str) if doc_class_str is not None else None

            if ftype == 'REGION':
                features = doc_class.get_overlapping_features(build, filters.get('chr').replace('chr', ''),
                                                              filters.get('start'), filters.get('end'))
                return features

            if ftype == 'ASSOC_SNP':
                hits = doc_class.get_overlapping_hits(build, filters.get('chr').replace('chr', ''),
                                                      filters.get('start'), filters.get('end'))
                features = Region.get_immune_snps(hits)
                return features

            sources = ['start', 'stop', 'seqid', 'chromosome', 'strand',
                       'biotype', 'giestain', 'name', 'symbol', 'id', 'ref', 'alt']

            seqid_param = 'seqid'
            end_param = 'stop'
            start_param = 'start'

            if ftype == 'GENE':
                seqid_param = 'chromosome'

            elastic = Search.range_overlap_query(filters.get('chr').replace('chr', ''), filters.get('start'),
                                                 filters.get('end'), idx=ElasticSettings.idx(idx, idx_type), size=10000,
                                                 seqid_param=seqid_param, end_param=end_param, start_param=start_param,
                                                 field_list=sources)
            docs = elastic.search().docs
            features = []
            for doc in docs:
                if isinstance(doc, RegionDocument):
                    doc = Region.pad_region_doc(doc)

                loc = doc.get_position(build=build).split(':')
                pos = loc[1].replace(',', '').split('-')
                attributes = {}
                feature = {
                    'name': doc.get_name(),
                    'id': doc.doc_id(),
                    'chr': loc[0],
                    'start': int(pos[0]),
                    'end': int(pos[1]) if len(pos) > 1 else int(pos[0]),
                    'strand': doc.get_strand_as_int()
                }

                if hasattr(doc, "biotype") and getattr(doc, "biotype") is not None:
                    attributes["biotype"] = getattr(doc, "biotype")
                if hasattr(doc, "giestain") and getattr(doc, "giestain") is not None:
                    attributes["stain"] = getattr(doc, "giestain")
                if hasattr(doc, "ref") and hasattr(doc, "alt") and getattr(doc, "ref") is not None and getattr(doc, "alt") is not None:
                    attributes["alleles"] = getattr(doc, "ref")+"/"+getattr(doc, "alt")
                feature['attributes'] = attributes
                features.append(ElasticObject(feature))
            return features
        except (ConnectionError) as err:
            logger.error(err)
            raise Http404