def diff_home(request): """ View function for diff query page """ # Render the HTML template index.html with data in the context variable diff_result = {} diff_release = False if request.method == 'POST': form = DiffForm(request.POST) if form.is_valid(): form_data_dict = form.get_cleaned_data() transcript_diff_url = ApiUtils.get_feature_diff_url(request, "transcript", form_data_dict) response = requests.get(transcript_diff_url) if response.status_code == 200: diff_result = response.json() # print(diff_result) return render(request, 'diff_compare_result.html', context={'form': form, 'diff_result': diff_result, 'diff_release': diff_release, 'form_data_dict': form_data_dict}) else: logger.error(form.errors) else: form = DiffForm() return render(request, 'tark_diff.html', context={'form': form, 'diff_release': diff_release, })
def search_home(request): """ View function for search query page """ host_url = ApiUtils.get_host_url(request) search_identifier = "" query_url = "/api/transcript/search/?expand=transcript_release_set,genes,translations&identifier_field=" # Render the HTML template index.html with data in the context variable search_result = {} if request.method == 'GET' and "identifier" in request.GET: search_identifier = request.GET['identifier'] if search_identifier is not None: # replace white space search_identifier = search_identifier.replace(" ", "") search_form = SearchForm(request.GET) query_url = query_url + search_identifier response = requests.get(host_url + query_url) if response.status_code == 200: search_result = response.json() return render(request, 'search_result.html', context={ 'form': search_form, 'search_result': search_result, 'search_identifier': search_identifier }) else: logger.error("Error from search") else: search_form = SearchForm() elif request.method == 'POST': search_form = SearchForm(request.POST) if search_form.is_valid(): search_identifier = search_form.cleaned_data['search_identifier'] # replace white space search_identifier = search_identifier.replace(" ", "") query_url = query_url + search_identifier response = requests.get(host_url + query_url) if response.status_code == 200: search_result = response.json() return render(request, 'search_result.html', context={ 'form': search_form, 'search_result': search_result, 'search_identifier': search_identifier }) else: logger.error("Error from search") else: logger.error(search_form.errors) else: search_form = SearchForm() return render(request, 'tark_search.html', context={'form': search_form})
def search_link(request, search_identifier): search_form = SearchForm(request.POST) host_url = ApiUtils.get_host_url(request) query_url = "/api/transcript/search/?identifier_field=" + search_identifier + \ "&expand=transcript_release_set,genes,translations" response = requests.get(host_url + query_url) if response.status_code == 200: search_result = response.json() return render(request, 'search_result.html', context={'form': search_form, 'search_result': search_result, 'search_identifier': search_identifier})
def transcript_details(request, stable_id_with_version, search_identifier): host_url = ApiUtils.get_host_url(request) # get transcript details query_url_details = "/api/transcript/stable_id_with_version/?stable_id_with_version=" + stable_id_with_version + \ "&expand_all=true" response = requests.get(host_url + query_url_details) transcript_details = {} if response.status_code == 200: search_result = response.json() if "results" in search_result and len(search_result["results"]) > 0: transcript_details = search_result["results"][0] if "genes" in transcript_details: gene = transcript_details["genes"][0] gene_name = gene["name"] lrg_id = SearchUtils.get_lrg_id_from_hgnc_name(gene_name) if lrg_id is not None: gene["lrg_id"] = lrg_id transcript_details["gene"] = gene else: logger.error("Error") # get transcript history transcript_history = {} if '.' in stable_id_with_version: (identifier, identifier_version) = stable_id_with_version.split( '.') # @UnusedVariable query_url_history = "/api/transcript/?stable_id=" + identifier + \ "&expand=transcript_release_set" response = requests.get(host_url + query_url_history) if response.status_code == 200: search_result = response.json() if "results" in search_result: transcript_history = search_result["results"] return render(request, 'transcript_details.html', context={ 'transcript_details': transcript_details, 'transcript_history': transcript_history, 'search_identifier': search_identifier, 'stable_id_with_version': stable_id_with_version })
def fetch_fasta_sequence(cls, request, feature_type, stable_id, stable_id_version, release_short_name=None, assembly_name=None, source_name=None, output_format="fasta"): if source_name is not None: source_name = source_name.lower() host_url = ApiUtils.get_host_url(request) query_url = "" if release_short_name is not None and assembly_name is not None and source_name is not None: query_url = "/api/" + feature_type.lower() + "/?stable_id=" + stable_id + \ "&stable_id_version=" + stable_id_version + \ "&release_short_name=" + release_short_name + \ "&assembly_name=" + assembly_name + \ "&source_name=" + source_name + \ "&expand=sequence" else: query_url = "/api/" + feature_type.lower() + "/?stable_id=" + stable_id + \ "&stable_id_version=" + stable_id_version + \ "&expand=sequence" sequence = None response = requests.get(host_url + query_url) if response.status_code == 200: response_result = response.json() if "results" in response_result and len( response_result["results"]) > 0: sequence_result = response_result["results"][0] sequence = sequence_result["sequence"]["sequence"] if sequence and "fasta" in output_format: id_a = stable_id + '.' + stable_id_version formatted_seq = cls.format_fasta(sequence, id_=id_a) return formatted_seq elif sequence and "raw" in output_format: return sequence return sequence
def test_get_host_url(self): # Create an instance of a GET request. request = self.factory.get('/api') host_url = ApiUtils.get_host_url(request) self.assertEqual("http://testserver", host_url, "Got the testserver url")
def fetch_cds_sequence(cls, request, feature_type, stable_id, stable_id_version, release_short_name=None, assembly_name=None, source_name=None, seq_type="cds", output_format="raw"): if source_name is not None: source_name = source_name.lower() host_url = ApiUtils.get_host_url(request) query_url = "" if release_short_name is not None and assembly_name is not None and source_name is not None: query_url = "/api/" + feature_type.lower() + "/?stable_id=" + stable_id + \ "&stable_id_version=" + stable_id_version + \ "&release_short_name=" + release_short_name + \ "&assembly_name=" + assembly_name + \ "&source_name=" + source_name + "&expand_all=true" else: query_url = "/api/" + feature_type.lower() + "/?stable_id=" + stable_id + \ "&stable_id_version=" + stable_id_version + \ "&expand_all=true" sequence = None response = requests.get(host_url + query_url) if response.status_code == 200: response_result = response.json() if "results" in response_result and len( response_result["results"]) > 0: transcript_result = response_result["results"][0] if "translations" in transcript_result and len( transcript_result["translations"]) > 0: translation = transcript_result["translations"][0] if "translation_id" in translation: tl_translation_id = translation["translation_id"] tl_query_set = Translation.objects.filter( translation_id=tl_translation_id).select_related( 'sequence') if tl_query_set is not None and len(tl_query_set) == 1: tl_obj = tl_query_set[0] translation["sequence"] = tl_obj.sequence.sequence translation[ "seq_checksum"] = tl_obj.sequence.seq_checksum transcript_result["translations"] = translation if "exons" in transcript_result: all_exons = transcript_result["exons"] new_exons = [] for exon in all_exons: if "exon_id" in exon: current_exon_query_set = Exon.objects.filter( exon_id=exon["exon_id"] ).select_related( 'sequence') # @IgnorePep8 if current_exon_query_set is not None and len( current_exon_query_set) == 1: current_exon_with_sequence = current_exon_query_set[ 0] exon[ "sequence"] = current_exon_with_sequence.sequence.sequence exon[ "seq_checksum"] = current_exon_with_sequence.sequence.seq_checksum new_exons.append(exon) if len(new_exons) > 0: transcript_result["exons"] = new_exons cds_info = ExonUtils.fetch_cds_info( transcript_result) if cds_info: if seq_type == "cds": if "cds_seq" in cds_info: sequence = cds_info['cds_seq'] elif seq_type == "five_prime": if "five_prime_utr_seq" in cds_info: sequence = cds_info[ 'five_prime_utr_seq'] elif seq_type == "three_prime": if "three_prime_utr_seq" in cds_info: sequence = cds_info[ 'three_prime_utr_seq'] if output_format == "raw": return sequence elif output_format == "fasta": id_version = stable_id + '.' + stable_id_version formatted_seq = cls.format_fasta(sequence, id_=id_version) return formatted_seq return sequence
def get_search_results(self, request, diff_query_params, attach_translation_seq=True, attach_exon_seq=True, attach_gene=True, attach_mane=True, attach_cds=True): host_url = ApiUtils.get_host_url(request) query_url = "/api/transcript/?" query_param_string = RequestUtils.get_query_param_string(diff_query_params) query_url = query_url + query_param_string response = requests.get(host_url + query_url) if response.status_code == 200: search_result = response.json() if search_result is not None and "count" in search_result and search_result["count"] == 1 and "results" in search_result: search_result = search_result["results"][0] if "transcript_release_set" in search_result: for release_set in search_result["transcript_release_set"]: if "release_short_name" in diff_query_params and "shortname" in release_set: if diff_query_params["release_short_name"] == release_set["shortname"]: search_result["transcript_release_set"] = release_set # handle mane if attach_mane is True: pass if attach_translation_seq is True: if "translations" in search_result and len(search_result["translations"]) > 0: translation = search_result["translations"][0] if "translation_id" in translation: tl_translation_id = translation["translation_id"] tl_query_set = Translation.objects.filter(translation_id=tl_translation_id).select_related('sequence') if tl_query_set is not None and len(tl_query_set) == 1: tl_obj = tl_query_set[0] translation["sequence"] = tl_obj.sequence.sequence translation["seq_checksum"] = tl_obj.sequence.seq_checksum search_result["translations"] = translation if attach_exon_seq is True: if "exons" in search_result: all_exons = search_result["exons"] new_exons = [] for exon in all_exons: if "exon_id" in exon: current_exon_query_set = Exon.objects.filter(exon_id=exon["exon_id"]).select_related('sequence') # @IgnorePep8 if current_exon_query_set is not None and len(current_exon_query_set) == 1: current_exon_with_sequence = current_exon_query_set[0] exon["sequence"] = current_exon_with_sequence.sequence.sequence exon["seq_checksum"] = current_exon_with_sequence.sequence.seq_checksum new_exons.append(exon) if len(new_exons) > 0: search_result["exons"] = new_exons if attach_gene is True: if "genes" in search_result: gene = search_result["genes"][0] search_result["gene"] = gene if attach_cds is True: if "translations" in search_result and len(search_result['translations']) > 0: cds_info = ExonUtils.fetch_cds_info(search_result) if cds_info: search_result["cds_info"] = cds_info return search_result