cDNA_refName = cDNA_ref_split[0] cDNA_refVer = cDNA_ref_split[1] cDNA_var = cDNA_value[1] # genomic values should 2nd item(tab) genomic_value = line[1].split(':') genomic_ref = genomic_value[0] error = False try: genomic_ref_split = genomic_ref.split('.') genomic_refName = genomic_ref_split[0] genomic_refVer = genomic_ref_split[1] genomic_var = genomic_value[1] gp = parser.parse('', genomic_var) if gp.position != '': genomic_position = gp.position else: genomic_position = gp.range_lower except: error = True genomic_ref_split = '' genomic_refName = line[1] genomic_refVer = '' genomic_var = '' genomic_position = '' # print errors print 'Error: Could not convert ' + cDNA_refName + '.' + cDNA_refVer + ' ' + cDNA_var + ' | ' + genomic_refName + '.' + genomic_refVer + ' ' + genomic_var + ' ' + genomic_position # save results to hvp db
def gene_results_view(request): # send user to timeout page if not authenticated if not request.user.is_authenticated(): return render_to_response('home/timeout.html') # deny access if permission has not been granted if request.user.get_profile().AccessStatus.ID != 2: return render_to_response('home/permission.html', {'user': request.user}) # default if blank search all searched_gene = '' if 'searched_gene' in request.POST: searched_gene = request.POST['searched_gene'] #gene = searchedGene paginate_results = 5 error = False # if searched_gene is genomic variant starting with "chr."(chromosome) or 'nc_'(refseq) # then we can go directy to the gene and searched for the variant in the variant page. if ('chr' in searched_gene.lower() or 'nc_' in searched_gene.lower()) and ':' in searched_gene.lower(): # try and get the gene from the variant try: p = Parser() variant = p.parse('', searched_gene) variant.genomic_ref variant_list = [] if 'nc_' in searched_gene.lower(): split_string = searched_gene.lower().split(':')[0].split( '.')[0] variant_list = Variant.objects.filter( GenomicRefSeq=split_string) else: split_string = searched_gene.lower().split(':')[0].replace( 'chr', '') variant_list = Variant.objects.filter( Q(GenomicRefSeq__contains=split_string)) if variant_list >= 1: gene = variant_list[0].Gene return HttpResponseRedirect( '/search/gene/' + str(gene.ID) + '/searchvariant/' + urllib.quote(searched_gene) + '/variantType/results/path_all/path_all/') except: error = True # tries for a precise search for the GeneName, groups then by GeneName due to duplicate GeneNames # with different RefSeq Names and Versions. if searched_gene == '': query = """ select g1.*, max(hvp_variantinstance.DateSubmitted) as DateSubmitted, (select Count(hvp_variant.ID) as variants from hvp_variant left join hvp_gene g2 on hvp_variant.Gene_id = g2.ID where g2.GeneName = g1.GeneName) as Variants, (select Count(hvp_variantinstance.ID) as variantInstances from hvp_variantinstance left join hvp_variant on hvp_variantinstance.Variant_id = hvp_variant.ID left join hvp_gene on hvp_variant.Gene_id = hvp_gene.ID where hvp_gene.GeneName = g1.GeneName) as VariantInstances from hvp_gene g1 left join hvp_variant on g1.ID = hvp_variant.Gene_id left join hvp_variantinstance on hvp_variant.ID = hvp_variantinstance.Variant_id group by GeneName order by GeneName asc """ qs = Gene.objects.raw(query) gene_list = list(qs) else: query = """ select g1.*, max(hvp_variantinstance.DateSubmitted) as DateSubmitted, (select Count(hvp_variant.ID) as variants from hvp_variant left join hvp_gene g2 on hvp_variant.Gene_id = g2.ID where g2.GeneName = g1.GeneName) as Variants, (select Count(hvp_variantinstance.ID) as variantInstances from hvp_variantinstance left join hvp_variant on hvp_variantinstance.Variant_id = hvp_variant.ID left join hvp_gene on hvp_variant.Gene_id = hvp_gene.ID where hvp_gene.GeneName = g1.GeneName) as VariantInstances from hvp_gene g1 left join hvp_variant on g1.ID = hvp_variant.Gene_id left join hvp_variantinstance on hvp_variant.ID = hvp_variantinstance.Variant_id where GeneName like %s group by GeneName order by GeneName asc """ qs = Gene.objects.raw(query, [searched_gene + '%']) gene_list = list(qs) # if nothing is return then we need to search in the other areas if len(gene_list) == 0: query = """ select g1.*, max(hvp_variantinstance.DateSubmitted) as DateSubmitted, (select Count(hvp_variant.ID) as variants from hvp_variant left join hvp_gene g2 on hvp_variant.Gene_id = g2.ID where g2.GeneName = g1.GeneName) as Variants, (select Count(hvp_variantinstance.ID) as variantInstances from hvp_variantinstance left join hvp_variant on hvp_variantinstance.Variant_id = hvp_variant.ID left join hvp_gene on hvp_variant.Gene_id = hvp_gene.ID where hvp_gene.GeneName = g1.GeneName) as VariantInstances from hvp_gene g1 left join hvp_variant on g1.ID = hvp_variant.Gene_id left join hvp_variantinstance on hvp_variant.ID = hvp_variantinstance.Variant_id where (RefSeqName like %s or GeneDescription like %s or HGNC_ID like %s or AlternateSymbols like %s or AlternateNames like %s or Chromosome like %s or PreviousSymbols like %s or PreviousNames like %s) group by GeneName order by GeneName asc """ qs = Gene.objects.raw(query, [ searched_gene + '%', '%' + searched_gene + '%', searched_gene + '%', '%' + searched_gene + '%', '%' + searched_gene + '%', searched_gene + '%', '%' + searched_gene + '%', '%' + searched_gene + '%' ]) gene_list = list(qs) # if still nothing then look at the gene disease tags - using taggit if len(gene_list) == 0: # break down string into list searched_gene_list = searched_gene.split(' ') gene_list = Gene.objects.filter( DiseasesTags__name__in=searched_gene_list).distinct() # number genes returned from search gene_count = gene_list.count # sets the paginator with the list of objects and number of objects per page paginator = Paginator(gene_list, 10) # gets the current page page = request.GET.get('page') try: genes = paginator.page(page) except PageNotAnInteger: genes = paginator.page(1) except EmptyPage: genes = paginator.page(paginator.num_pages) return render_to_response('search/gene_result.html', { 'user': request.user, 'error': error, 'searched_gene': searched_gene, 'gene_count': gene_count, 'genes': genes, 'paginate_results': paginate_results, }, context_instance=RequestContext(request))
def variant_patient_view(request, geneID, variantID, instanceID, path_filter, path_filter_ratio): template = 'variant_patient' if not request.user.is_authenticated(): return render_to_response('home/timeout.html') if request.user.get_profile().AccessStatus.ID != 2: return render_to_response('home/permission.html', {'user': request.user}) gene = get_object_or_404(Gene, pk = geneID) instance = get_object_or_404(VariantInstance, pk = instanceID) searched_variant = '' searched_variant_safe = '' variant_query_string = '' params = [instance.Patient_id] page_error = False search_tooshort = False invalid_variant = False # apply path filters dict_path = Dict_path_filter(path_filter) # apply path filter path_id = Path_ID(path_filter) # apply path filters ratio (the total number of instance per path) dict_path_ratio = Dict_path_filter(path_filter_ratio) # apply path filter ratio path_ratio_id = Path_ID(path_filter_ratio) # query options to filter by community consensus path path_query_string = '' if path_id != '': if path_id == 'NULL': path_query_string = ' and v.Pathogenicity_id is NULL ' else: path_query_string = ' and v.Pathogenicity_id = %s ' params.append(path_id) # search results if 'search' in request.POST: search_type = 'variantType' #request.POST['search_type'] if request.POST['searched_variant'] != '': searched_variant = request.POST['searched_variant'] seached_variant_safe = searched_variant # url friendly searched_variant = urllib.unquote(searched_variant) # raw form validator = Validator() if validator.validate(searched_variant) == False: page_error = True invalid_variant = True # if there are no errors then proceed with the query if not page_error: parser = Parser() variant = parser.parse('', searched_variant) # query options filter by community consensus path path_query_string = '' variant_query_string = VariantQueryString(variant, params) # query options filter by path count count_query_string = '' order_query_string = ' order by Instances desc ' if path_ratio_id != '': count_query_string = """, (select count(hvp_variantinstance.Pathogenicity_id) as path from hvp_variantinstance where hvp_variantinstance.Variant_ID = v.ID and hvp_variantinstance.pathogenicity_id = """ + str(path_ratio_id) + ') as Path_Count ' order_query_string = ' order by Path_Count desc ' query = """select v.*, hvp_variantinstance.Patient_id, (select Count(hvp_variantinstance.Variant_id) as i from hvp_variantinstance left join hvp_variant on hvp_variantinstance.Variant_ID = hvp_variant.ID where hvp_variant.ID = v.ID) as instances """ + count_query_string + """ from hvp_variant v left join hvp_variantinstance on v.ID = hvp_variantinstance.Variant_id where hvp_variantinstance.Patient_id = %s """ + path_query_string + variant_query_string + ' group by ID ' + order_query_string qs = Variant.objects.raw(query, params) variant_list = list(qs) # sum of total results used to display on screen results = len(variant_list) # sets the paginator with the list of objects and number objects per page paginator = Paginator(variant_list, 10) # gets the current page page = request.GET.get('page') try: variants = paginator.page(page) except PageNotAnInteger: variants = paginator.page(1) except EmptyPage: variants = paginator.page(paginator.num_pages) return render_to_response('search/variant.html', { 'user': request.user, 'template': template, 'dict_path': dict_path, 'path_filter': path_filter, 'geneID': geneID, 'variantID': variantID, 'gene': gene, 'variants': variants, 'instance': instance, 'results': results, 'page_error': page_error, 'search_tooshort': search_tooshort, 'invalid_variant': invalid_variant, 'searched_variant': searched_variant, 'searched_variant_safe': searched_variant_safe, 'dict_path_ratio': dict_path_ratio, 'path_filter_ratio': path_filter_ratio, })
def variant_results_view(request, geneID, searched_variant, search_type, path_filter, path_filter_ratio): template = 'variant_result' if not request.user.is_authenticated(): return render_to_response('home/timeout.html') if request.user.get_profile().AccessStatus.ID != 2: return render_to_response('home/permission.html', {'user': request.user}) # searching/filtering for a specific variant search if 'search' in request.POST: if request.POST['searched_variant'] != '': #search_type = request.POST['search_type'] search_type = "variantType" searched_variant = request.POST['searched_variant'] return HttpResponseRedirect('/search/gene/' + geneID + '/searchvariant/' + urllib.quote(searched_variant) + '/' + search_type + '/results/' + path_filter + '/' + path_filter_ratio + '/') else: # go back to the default variant page that lists all variants return HttpResponseRedirect('/search/gene/' + geneID + '/searchvariant/path_all/path_all/') gene = get_object_or_404(Gene, pk = geneID) geneName = gene.GeneName page_error = False search_tooShort = False invalid_variant = False # apply path filters dict_path = Dict_path_filter(path_filter) # apply path filter path_id = Path_ID(path_filter) # apply path filters ratio (the total number of instance per path) dict_path_ratio = Dict_path_filter(path_filter_ratio) # apply path filter ratio path_ratio_id = Path_ID(path_filter_ratio) # keep url safe version for path filter links searched_variant_safe = searched_variant # converting url string back to a proper variant nomenclature searched_variant = urllib.unquote(searched_variant) # search_type = searchType paginate_results = 5 # check if the search query string is less than 2 characters, if so return error message to user if len(searched_variant) < 2: page_error = True search_tooShort = True # check if the entered variant is valid validator = Validator() if validator.validate(searched_variant) == False: page_error = True invalid_variant = True # if there are no errors then proceed with the query variant_list = [] variants = None results = None if not page_error: # Break down the searched_variant using variant_parser to use in search with solr # NB: I noticed that any values that have been broken down in the parser that # has a '+' or '-' will be converted to a long int. parser = Parser() variant = parser.parse('', searched_variant) # query options filter by community consensus path path_query_string = '' params = [geneName] variant_query_string = VariantQueryString(variant, params) if path_id != '': if path_id == 'NULL': path_query_string = ' and v.Pathogenicity_id is NULL ' else: path_query_string = ' and v.Pathogenicity_id = %s ' params.append(path_id) # query options filter by path count count_query_string = '' order_query_string = ' order by Instances desc ' if path_ratio_id != '': count_query_string = """, (select count(hvp_variantinstance.Pathogenicity_id) as path from hvp_variantinstance where hvp_variantinstance.Variant_ID = v.ID and hvp_variantinstance.pathogenicity_id = """ + str(path_ratio_id) + ') as Path_Count ' order_query_string = ' order by Path_Count desc ' query = 'select v.*, Count(hvp_variantinstance.Variant_id) as Instances' + count_query_string + """ from hvp_variant v left join hvp_variantinstance on v.ID = hvp_variantinstance.Variant_id left join hvp_gene on v.Gene_id = hvp_gene.ID where hvp_gene.GeneName = %s """ + path_query_string + variant_query_string + ' group by ID ' + order_query_string qs = Variant.objects.raw(query, params) variant_list = list(qs) # sum of total results used to display on screen results = variant_list.count #len(variant_list) # sets the paginator with the list of objects and number of objects per page paginator = Paginator(variant_list, 10) # gets the current page page = request.GET.get('page') try: variants = paginator.page(page) except PageNotAnInteger: variants = paginator.page(1) except EmptyPage: variants = paginator.page(paginator.num_pages) return render_to_response('search/variant.html', { 'user': request.user, 'template': template, 'searched_variant': searched_variant, 'searched_variant_safe': searched_variant_safe, 'page_error': page_error, 'invalid_variant': invalid_variant, 'paginate_results': paginate_results, 'geneName' : geneName, 'geneID' : geneID, 'search_type': search_type, 'variants': variants, 'results': results, 'search_tooShort': search_tooShort, 'path_filter': path_filter, 'dict_path': dict_path, 'dict_path_ratio': dict_path_ratio, 'path_filter_ratio': path_filter_ratio, }, context_instance=RequestContext(request))
from Portal import settings setup_environ(settings) from Portal.hvp.models.search.Gene import Gene from Portal.hvp.models.search.Variant import Variant from Portal.search.hgvs_parser.Parser import Parser # get all variants variant_list = Variant.objects.all() parser = Parser() for variant in variant_list: if variant.cDNA != 'None': try: v = parser.parse('', variant.cDNA) if v.position: variant.Position = v.position.replace('*','') if v.position_intron: variant.PositionIntron = v.position_intron.replace('*','') else: if v.range_lower: variant.LowerRange = v.range_lower.replace('*','') if v.range_lower_intron: variant.LowerRangeIntron = v.range_lower_intron.replace('*','') variant.UpperRange = v.range_upper.replace('*','') if v.range_upper_intron:
def gene_results_view(request): # send user to timeout page if not authenticated if not request.user.is_authenticated(): return render_to_response('home/timeout.html') # deny access if permission has not been granted if request.user.get_profile().AccessStatus.ID != 2: return render_to_response('home/permission.html', { 'user': request.user }) # default if blank search all searched_gene = '' if 'searched_gene' in request.POST: searched_gene = request.POST['searched_gene'] #gene = searchedGene paginate_results = 5 error = False # if searched_gene is genomic variant starting with "chr."(chromosome) or 'nc_'(refseq) # then we can go directy to the gene and searched for the variant in the variant page. if ('chr' in searched_gene.lower() or 'nc_' in searched_gene.lower()) and ':' in searched_gene.lower(): # try and get the gene from the variant try: p = Parser() variant = p.parse('', searched_gene) variant.genomic_ref variant_list = [] if 'nc_' in searched_gene.lower(): split_string = searched_gene.lower().split(':')[0].split('.')[0] variant_list = Variant.objects.filter(GenomicRefSeq = split_string) else: split_string = searched_gene.lower().split(':')[0].replace('chr','') variant_list = Variant.objects.filter(Q(GenomicRefSeq__contains=split_string)) if variant_list >= 1: gene = variant_list[0].Gene return HttpResponseRedirect('/search/gene/' + str(gene.ID) + '/searchvariant/' + urllib.quote(searched_gene) + '/variantType/results/path_all/path_all/') except: error = True # tries for a precise search for the GeneName, groups then by GeneName due to duplicate GeneNames # with different RefSeq Names and Versions. if searched_gene == '': query = """ select g1.*, max(hvp_variantinstance.DateSubmitted) as DateSubmitted, (select Count(hvp_variant.ID) as variants from hvp_variant left join hvp_gene g2 on hvp_variant.Gene_id = g2.ID where g2.GeneName = g1.GeneName) as Variants, (select Count(hvp_variantinstance.ID) as variantInstances from hvp_variantinstance left join hvp_variant on hvp_variantinstance.Variant_id = hvp_variant.ID left join hvp_gene on hvp_variant.Gene_id = hvp_gene.ID where hvp_gene.GeneName = g1.GeneName) as VariantInstances from hvp_gene g1 left join hvp_variant on g1.ID = hvp_variant.Gene_id left join hvp_variantinstance on hvp_variant.ID = hvp_variantinstance.Variant_id group by GeneName order by GeneName asc """ qs = Gene.objects.raw(query) gene_list = list(qs) else: query = """ select g1.*, max(hvp_variantinstance.DateSubmitted) as DateSubmitted, (select Count(hvp_variant.ID) as variants from hvp_variant left join hvp_gene g2 on hvp_variant.Gene_id = g2.ID where g2.GeneName = g1.GeneName) as Variants, (select Count(hvp_variantinstance.ID) as variantInstances from hvp_variantinstance left join hvp_variant on hvp_variantinstance.Variant_id = hvp_variant.ID left join hvp_gene on hvp_variant.Gene_id = hvp_gene.ID where hvp_gene.GeneName = g1.GeneName) as VariantInstances from hvp_gene g1 left join hvp_variant on g1.ID = hvp_variant.Gene_id left join hvp_variantinstance on hvp_variant.ID = hvp_variantinstance.Variant_id where GeneName like %s group by GeneName order by GeneName asc """ qs = Gene.objects.raw(query, [searched_gene+'%']) gene_list = list(qs) # if nothing is return then we need to search in the other areas if len(gene_list) == 0: query = """ select g1.*, max(hvp_variantinstance.DateSubmitted) as DateSubmitted, (select Count(hvp_variant.ID) as variants from hvp_variant left join hvp_gene g2 on hvp_variant.Gene_id = g2.ID where g2.GeneName = g1.GeneName) as Variants, (select Count(hvp_variantinstance.ID) as variantInstances from hvp_variantinstance left join hvp_variant on hvp_variantinstance.Variant_id = hvp_variant.ID left join hvp_gene on hvp_variant.Gene_id = hvp_gene.ID where hvp_gene.GeneName = g1.GeneName) as VariantInstances from hvp_gene g1 left join hvp_variant on g1.ID = hvp_variant.Gene_id left join hvp_variantinstance on hvp_variant.ID = hvp_variantinstance.Variant_id where (RefSeqName like %s or GeneDescription like %s or HGNC_ID like %s or AlternateSymbols like %s or AlternateNames like %s or Chromosome like %s or PreviousSymbols like %s or PreviousNames like %s) group by GeneName order by GeneName asc """ qs = Gene.objects.raw(query, [searched_gene+'%', '%'+searched_gene+'%', searched_gene+'%', '%'+searched_gene+'%', '%'+searched_gene+'%', searched_gene+'%', '%'+searched_gene+'%', '%'+searched_gene+'%']) gene_list = list(qs) # if still nothing then look at the gene disease tags - using taggit if len(gene_list) == 0: # break down string into list searched_gene_list = searched_gene.split(' ') gene_list = Gene.objects.filter(DiseasesTags__name__in=searched_gene_list).distinct() # number genes returned from search gene_count = gene_list.count # sets the paginator with the list of objects and number of objects per page paginator = Paginator(gene_list, 10) # gets the current page page = request.GET.get('page') try: genes = paginator.page(page) except PageNotAnInteger: genes = paginator.page(1) except EmptyPage: genes = paginator.page(paginator.num_pages) return render_to_response('search/gene_result.html', { 'user': request.user, 'error': error, 'searched_gene': searched_gene, 'gene_count': gene_count, 'genes': genes, 'paginate_results': paginate_results, }, context_instance=RequestContext(request))