def __init__(self, *args, **kwargs): super(CompareSetForm, self).__init__(*args, **kwargs) current_release = ReleaseUtils.get_latest_release() current_assembly = ReleaseUtils.get_latest_assembly() current_source = ReleaseUtils.get_default_source() self.fields['diff_current_assembly'] = forms.CharField( initial=current_assembly, widget=forms.Select(choices=FormUtils.get_all_assembly_name_tuples( ))) # @IgnorePep8 self.fields['diff_current_release'] = forms.CharField( initial=int(current_release), widget=forms.Select(choices=FormUtils.get_all_release_name_tuples( source_name="all"))) # @IgnorePep8 self.fields['diff_current_source'] = forms.CharField( initial=current_source, widget=forms.Select( choices=FormUtils.get_all_sources_tuples())) # @IgnorePep8 self.fields['diff_compare_assembly'] = forms.CharField( initial=current_assembly, widget=forms.Select(choices=FormUtils.get_all_assembly_name_tuples( ))) # @IgnorePep8 self.fields['diff_compare_release'] = forms.CharField( initial=int(current_release) - 1, widget=forms.Select(choices=FormUtils.get_all_release_name_tuples( source_name="all"))) # @IgnorePep8 self.fields['diff_compare_source'] = forms.CharField( initial=current_source, widget=forms.Select( choices=FormUtils.get_all_sources_tuples())) # @IgnorePep8
def __init__(self, *args, **kwargs): super(DiffFormRelease, self).__init__(*args, **kwargs) current_release = ReleaseUtils.get_latest_release() current_assembly = ReleaseUtils.get_latest_assembly() default_source = ReleaseUtils.get_default_source() self.fields['species'] = forms.CharField(widget=forms.Select( choices=FormUtils.get_all_species_name_tuples()), required=False) self.fields['diff_me_assembly'] = forms.CharField( initial=current_assembly, widget=forms.Select(choices=FormUtils.get_all_assembly_name_tuples( ))) # @IgnorePep8 self.fields['diff_me_release'] = forms.CharField( initial=current_release, widget=forms.Select(choices=FormUtils.get_all_release_name_tuples( ))) # @IgnorePep8 self.fields['diff_with_assembly'] = forms.CharField( initial=current_assembly, widget=forms.Select(choices=FormUtils.get_all_assembly_name_tuples( ))) # @IgnorePep8 self.fields['diff_with_release'] = forms.CharField( initial=int(current_release) - 1, widget=forms.Select(choices=FormUtils.get_all_release_name_tuples( ))) # @IgnorePep8 self.fields['diff_with_source'] = forms.CharField( initial=default_source, widget=forms.Select( choices=FormUtils.get_all_sources_tuples())) # @IgnorePep8 self.fields['diff_me_source'] = forms.CharField( initial=default_source, widget=forms.Select( choices=FormUtils.get_all_sources_tuples())) # @IgnorePep8
def test_get_all_releases(self): all_releases = ReleaseUtils.get_all_releases() self.assertEqual(len(all_releases), 3, "Release list match for current release") all_releases = ReleaseUtils.get_all_releases(assembly_name="GRCh37") self.assertEqual(len(all_releases), 1, "Release list match for GRCh37")
def test_get_all_release_short_names(self): all_release_short_names = ReleaseUtils.get_all_release_short_names() expected_releases = ['92', '91'] self.assertListEqual(all_release_short_names, expected_releases, "Release list match for current release") all_release_short_names = ReleaseUtils.get_all_release_short_names( assembly_name="GRCh37") expected_releases = ['92'] self.assertListEqual(all_release_short_names, expected_releases, "Release list match for GRCh37")
def get_all_assembly_release_name_tuples(cls, assembly_name=None, source_name=None): assembly_release_list = [] for assembly_name in ReleaseUtils.get_all_assembly_names(): for release in ReleaseUtils.get_all_release_short_names( assembly_name, source_name): assembly_release_list.append( (str(assembly_name) + ":" + str(release), str(assembly_name) + ":" + str(release))) return assembly_release_list
def test_get_all_assembly_releases(self): all_assembly_releases = ReleaseUtils.get_all_assembly_releases() expected_result = {'GRCh37': ['92'], 'GRCh38': ['92', '91']} self.assertDictEqual(all_assembly_releases, expected_result, "Got the right assembly_releases") all_assembly_releases_refseq = ReleaseUtils.get_all_assembly_releases( source_name="RefSeq") expected_result_refseq = {'GRCh38': ['92']} self.assertDictEqual(all_assembly_releases_refseq, expected_result_refseq, "Got the expected result for RefSeq")
def get_all_assembly_name_tuples(cls): assembly_list = [] for assembly in ReleaseUtils.get_all_assembly_names(): assembly_list.append((assembly, assembly)) return assembly_list
def get_all_sources_tuples(cls): source_list = [] source_list.append(("", "")) for source in ReleaseUtils.get_all_release_sources(): source_list.append((source, source)) return source_list
def statistics(request): reports = ReleaseUtils.get_release_loading_stats() return render(request, 'statistics_view.html', context={'reports': reports})
def get_all_release_name_tuples(cls, assembly_name=None, source_name=None): release_list = [] for release in ReleaseUtils.get_all_release_short_names( assembly_name, source_name): release_list.append((release, release)) return release_list
def diff_with_release_field(cls): diff_with_release_field = coreapi.Field( name='diff_with_release', location='query', required=False, type='string', description='release_short_name to diff with(eg: 86 default to highest release in the releaset set - ' + str(ReleaseUtils.get_latest_release()) + ' )') return diff_with_release_field
def mane_GRCh37_list(request): sql = """ SELECT DISTINCT t1.transcript_id, t1.stable_id as ens_stable_id, t1.stable_id_version as ens_stable_id_version, relationship_type.shortname as mane_type, t2.stable_id as refseq_stable_id, t2.stable_id_version as refseq_stable_id_version, gn1.name as ens_gene_name, t3.stable_id as grch37_stable_id, t3.stable_id_version as grch37_stable_id_version, IF(tl3.five_utr_checksum = tl1.five_utr_checksum,'True','False') as five_prime_utr, 'True' as cds, IF(tl3.three_utr_checksum = tl1.three_utr_checksum,'True','False') as three_prime_utr FROM transcript t1 JOIN transcript_release_tag trt1 ON t1.transcript_id=trt1.feature_id JOIN transcript_release_tag_relationship ON trt1.transcript_release_id=transcript_release_tag_relationship.transcript_release_object_id JOIN transcript_release_tag trt2 ON transcript_release_tag_relationship.transcript_release_subject_id=trt2.transcript_release_id JOIN transcript t2 ON trt2.feature_id=t2.transcript_id JOIN relationship_type ON transcript_release_tag_relationship.relationship_type_id=relationship_type.relationship_type_id JOIN transcript_gene tg1 ON t1.transcript_id=tg1.transcript_id JOIN gene gene1 ON tg1.gene_id=gene1.gene_id JOIN gene_names gn1 ON gene1.name_id=gn1.external_id and gn1.primary_id=1 JOIN transcript t3 ON t3.stable_id = t1.stable_id AND t3.assembly_id = 1 JOIN translation_transcript tt1 ON tt1.transcript_id = t1.transcript_id JOIN translation tl1 ON tl1.translation_id = tt1.translation_id JOIN translation_transcript tt3 ON tt3.transcript_id = t3.transcript_id JOIN translation tl3 ON tl3.translation_id = tt3.translation_id WHERE t1.assembly_id = 1001 and tl3.seq_checksum = tl1.seq_checksum ORDER BY gn1.name; """ with connections['tark'].cursor() as cursor: cursor.execute(sql) results = ReleaseUtils.dictfetchall(cursor) page = request.GET.get('page', 1) paginator = Paginator(results,25) try: results = paginator.page(page) except PageNotAnInteger: results = paginator.page(1) except EmptyPage: results = paginator.page(paginator.num_pages) return render( request, 'mane_GRCh37_list.html', context={ 'results': results } )
def index(request): """ View function for home page """ current_release = ReleaseUtils.get_latest_release() # Render the HTML template index.html with data in the context variable return render( request, 'index.html', context={'current_release': current_release}, )
def init_assembly_releases(request): # default is ensembl all_assembly_releases = json.dumps( ReleaseUtils.get_all_assembly_releases()) all_assembly_releases_refseq = json.dumps( ReleaseUtils.get_all_assembly_releases("RefSeq")) all_assembly_releases_ensembl = json.dumps( ReleaseUtils.get_all_assembly_releases("Ensembl")) current_release = ReleaseUtils.get_latest_release() current_assembly = ReleaseUtils.get_latest_assembly() source_name = ReleaseUtils.get_default_source() init_hash = { "all_assembly_releases": all_assembly_releases, "all_assembly_releases_ensembl": all_assembly_releases_ensembl, "all_assembly_releases_refseq": all_assembly_releases_refseq, "current_release": current_release, "current_assembly": current_assembly, "release_name": current_release, "assembly_name": current_assembly, 'release_name_compare': int(current_release) - 1, 'assembly_name_compare': current_assembly, 'source_name': source_name } return init_hash
def init_assembly_releases(request): # default is ensembl all_assembly_releases = json.dumps( ReleaseUtils.get_all_assembly_releases()) all_assembly_releases_refseq = json.dumps( ReleaseUtils.get_all_assembly_releases("RefSeq")) all_assembly_releases_ensembl = json.dumps( ReleaseUtils.get_all_assembly_releases("Ensembl")) current_release = ReleaseUtils.get_latest_release() current_assembly = ReleaseUtils.get_latest_assembly() source_name = ReleaseUtils.get_default_source() software_release_tag = "1.0.0" if settings.SOFTWARE_RELEASE_TAG is None else settings.SOFTWARE_RELEASE_TAG init_hash = { "all_assembly_releases": all_assembly_releases, "all_assembly_releases_ensembl": all_assembly_releases_ensembl, "all_assembly_releases_refseq": all_assembly_releases_refseq, "current_release": current_release, "current_assembly": current_assembly, "release_name": current_release, "assembly_name": current_assembly, 'release_name_compare': int(current_release) - 1, 'assembly_name_compare': current_assembly, 'source_name': source_name, 'software_release_tag': software_release_tag } return init_hash
def get_query_params(cls, request, diff_type=None): params_diff = {} stable_id_cur = request.query_params.get(diff_type + '_stable_id', None) if '.' in stable_id_cur: stable_id, stable_id_version = stable_id_cur.split('.') params_diff['stable_id'] = stable_id params_diff['stable_id_version'] = stable_id_version else: params_diff['stable_id'] = request.query_params.get( diff_type + '_stable_id', None) params_diff['release_short_name'] = request.query_params.get( diff_type + '_release', ReleaseUtils.get_latest_release()) params_diff['assembly_name'] = request.query_params.get( diff_type + '_assembly', ReleaseUtils.get_latest_assembly()) params_diff['source_name'] = request.query_params.get( diff_type + '_source', ReleaseUtils.get_default_source()) # params_diff['expand'] = request.query_params.get('expand', "transcript_release_set,translations, sequence") params_diff['expand_all'] = request.query_params.get( 'expand_all', "true") return params_diff
def diff_release_home(request): """ View function for diff query page """ # Render the HTML template index.html with data in the context variable current_url = resolve(request.path_info).url_name if "diff_home_release" in current_url: diff_release = True if request.method == 'POST': form = DiffFormRelease(request.POST) if form.is_valid(): form_data_dict = form.get_cleaned_data() diff_dict = {} diff_dict['release_set_1'] = { "source": form_data_dict["diff_me_source"].lower(), "assembly": form_data_dict["diff_me_assembly"].lower(), "version": form_data_dict["diff_me_release"] } diff_dict['release_set_2'] = { "source": form_data_dict["diff_with_source"].lower(), "assembly": form_data_dict["diff_with_assembly"].lower(), "version": form_data_dict["diff_with_release"] } diff_result_release = ReleaseUtils.get_release_diff(diff_dict) return render(request, 'diff_compare_release_set_result.html', context={ 'form': form, 'diff_release': diff_release, 'diff_result_release': json.dumps(diff_result_release), 'form_data_dict': json.dumps(form_data_dict) }) else: logger.error(form.errors) else: form = DiffForm() return render(request, 'release_diff.html', context={'form': form})
def manelist(request): sql = """ SELECT DISTINCT t1.transcript_id, t1.stable_id as ens_stable_id, t1.stable_id_version as ens_stable_id_version, relationship_type.shortname as mane_type, t2.stable_id as refseq_stable_id, t2.stable_id_version as refseq_stable_id_version, gn1.name as ens_gene_name FROM transcript t1 JOIN transcript_release_tag trt1 ON t1.transcript_id=trt1.feature_id JOIN transcript_release_tag_relationship ON trt1.transcript_release_id=transcript_release_tag_relationship.transcript_release_object_id JOIN transcript_release_tag trt2 ON transcript_release_tag_relationship.transcript_release_subject_id=trt2.transcript_release_id JOIN transcript t2 ON trt2.feature_id=t2.transcript_id JOIN relationship_type ON transcript_release_tag_relationship.relationship_type_id=relationship_type.relationship_type_id JOIN transcript_gene tg1 ON t1.transcript_id=tg1.transcript_id JOIN gene gene1 ON tg1.gene_id=gene1.gene_id JOIN gene_names gn1 ON gene1.name_id=gn1.external_id where gn1.primary_id=1 ORDER BY gn1.name; """ with connections['tark'].cursor() as cursor: cursor.execute(sql) results = ReleaseUtils.dictfetchall(cursor) return render( request, 'mane_list.html', context={ 'results': results } )
def feature_diff(request, feature, from_release, to_release, direction="changed", source="Ensembl"): """ Get the list of features that are different between two releases in the context (addition, deletion or change) Parameters ---------- feature : str version : int direction : str One of gained|removed|changed Returns ------- count : int """ # print("feature {} from_release {}, to_release {}, direction {}, source {} ".format(feature, from_release, to_release, direction, source)) # Change the original sql query to include biotypes from gene and transcript tables sql = """ SELECT v0.stable_id as from_stable_id, v0.stable_id_version as from_stable_id_version, v1.stable_id as to_stable_id, v1.stable_id_version as to_stable_id_version, v1.biotype as new_biotype, case when v1.biotype=v0.biotype then ' ' else v0.biotype end as previous_biotype FROM ( SELECT #FEATURE#.stable_id, #FEATURE#.stable_id_version, #FEATURE#.biotype, f_tag.feature_id, rs.shortname, rs.description, rs.assembly_id FROM #FEATURE# JOIN #FEATURE#_release_tag AS f_tag ON (#FEATURE#.#FEATURE#_id=f_tag.feature_id) JOIN release_set AS rs ON (f_tag.release_id=rs.release_id) JOIN release_source AS rst ON (rs.source_id=rst.source_id) WHERE rs.shortname=%s AND rst.shortname=%s ) AS v0 #DIRECTION# JOIN ( SELECT #FEATURE#.stable_id, #FEATURE#.stable_id_version, #FEATURE#.biotype, f_tag.feature_id, rs.shortname, rs.description, rs.assembly_id FROM #FEATURE# JOIN #FEATURE#_release_tag AS f_tag ON (#FEATURE#.#FEATURE#_id=f_tag.feature_id) JOIN release_set AS rs ON (f_tag.release_id=rs.release_id) JOIN release_source AS rst ON (rs.source_id=rst.source_id) WHERE rs.shortname=%s AND rst.shortname=%s ) AS v1 ON (v0.stable_id=v1.stable_id) WHERE #OUTER_WHERE#; """ # In Release stats gene count details page, display gene name along with stable_id, if gene name is present in database if feature == 'gene': sql = """ SELECT v0.gene_symbol as from_gene, v0.stable_id as from_stable_id, v0.stable_id_version as from_stable_id_version, v1.gene_symbol as to_gene, v1.stable_id as to_stable_id, v1.stable_id_version as to_stable_id_version,v1.biotype as new_biotype, case when v1.biotype=v0.biotype then ' ' else v0.biotype end as previous_biotype FROM ( SELECT IF(gn.name IS NULL, '', gn.name) as gene_symbol, #FEATURE#.stable_id, #FEATURE#.stable_id_version, #FEATURE#.biotype, f_tag.feature_id, rs.shortname, rs.description, rs.assembly_id FROM #FEATURE# JOIN #FEATURE#_release_tag AS f_tag ON (#FEATURE#.#FEATURE#_id=f_tag.feature_id) JOIN release_set AS rs ON (f_tag.release_id=rs.release_id) JOIN release_source AS rst ON (rs.source_id=rst.source_id) LEFT JOIN gene_names AS gn ON (#FEATURE#.name_id = gn.external_id) AND (gn.primary_id = 1) WHERE rs.shortname=%s AND rst.shortname=%s ) AS v0 #DIRECTION# JOIN ( SELECT IF(gn.name IS NULL, '', gn.name) as gene_symbol, #FEATURE#.stable_id, #FEATURE#.stable_id_version, #FEATURE#.biotype, f_tag.feature_id, rs.shortname, rs.description, rs.assembly_id FROM #FEATURE# JOIN #FEATURE#_release_tag AS f_tag ON (#FEATURE#.#FEATURE#_id=f_tag.feature_id) JOIN release_set AS rs ON (f_tag.release_id=rs.release_id) JOIN release_source AS rst ON (rs.source_id=rst.source_id) LEFT JOIN gene_names AS gn ON (#FEATURE#.name_id = gn.external_id) AND (gn.primary_id = 1) WHERE rs.shortname=%s AND rst.shortname=%s ) AS v1 ON (v0.stable_id=v1.stable_id) WHERE #OUTER_WHERE#; """ sql = sql.replace('#FEATURE#', feature) if direction == 'removed': sql = sql.replace('#DIRECTION#', 'LEFT') sql = sql.replace('#OUTER_WHERE#', 'v1.stable_id IS NULL') elif direction == 'gained': sql = sql.replace('#DIRECTION#', 'RIGHT') sql = sql.replace('#OUTER_WHERE#', 'v0.stable_id IS NULL') else: sql = sql.replace('#DIRECTION#', '') sql = sql.replace( '#OUTER_WHERE#', 'v0.stable_id_version!=v1.stable_id_version' ) # print(sql) with connections['tark'].cursor() as cursor: cursor.execute( sql, [ str(from_release), source, str(to_release), source, ] ) results = ReleaseUtils.dictfetchall(cursor) return render( request, 'feature_diff_list.html', context={ 'feature': feature, 'from_release': from_release, 'to_release': to_release, 'source': source, 'direction': direction, 'results': results } )
def feature_diff(request, feature, from_release, to_release, direction="changed", source="Ensembl"): """ Get the list of features that are different between two releases in the context (addition, deletion or change) Parameters ---------- feature : str version : int direction : str One of gained|removed|changed Returns ------- count : int """ # print("feature {} from_release {}, to_release {}, direction {}, source {} ".format(feature, from_release, to_release, direction, source)) sql = """ SELECT v0.stable_id as from_stable_id, v0.stable_id_version as from_stable_id_version, v1.stable_id as to_stable_id, v1.stable_id_version as to_stable_id_version FROM ( SELECT #FEATURE#.stable_id, #FEATURE#.stable_id_version, f_tag.feature_id, rs.shortname, rs.description, rs.assembly_id FROM #FEATURE# JOIN #FEATURE#_release_tag AS f_tag ON (#FEATURE#.#FEATURE#_id=f_tag.feature_id) JOIN release_set AS rs ON (f_tag.release_id=rs.release_id) JOIN release_source AS rst ON (rs.source_id=rst.source_id) WHERE rs.shortname=%s AND rst.shortname=%s ) AS v0 #DIRECTION# JOIN ( SELECT #FEATURE#.stable_id, #FEATURE#.stable_id_version, f_tag.feature_id, rs.shortname, rs.description, rs.assembly_id FROM #FEATURE# JOIN #FEATURE#_release_tag AS f_tag ON (#FEATURE#.#FEATURE#_id=f_tag.feature_id) JOIN release_set AS rs ON (f_tag.release_id=rs.release_id) JOIN release_source AS rst ON (rs.source_id=rst.source_id) WHERE rs.shortname=%s AND rst.shortname=%s ) AS v1 ON (v0.stable_id=v1.stable_id) WHERE #OUTER_WHERE#; """ sql = sql.replace('#FEATURE#', feature) if direction == 'removed': sql = sql.replace('#DIRECTION#', 'LEFT') sql = sql.replace('#OUTER_WHERE#', 'v1.stable_id IS NULL') elif direction == 'gained': sql = sql.replace('#DIRECTION#', 'RIGHT') sql = sql.replace('#OUTER_WHERE#', 'v0.stable_id IS NULL') else: sql = sql.replace('#DIRECTION#', '') sql = sql.replace('#OUTER_WHERE#', 'v0.stable_id_version!=v1.stable_id_version') # print(sql) with connections['tark'].cursor() as cursor: cursor.execute(sql, [ str(from_release), source, str(to_release), source, ]) results = ReleaseUtils.dictfetchall(cursor) return render(request, 'feature_diff_list.html', context={ 'feature': feature, 'from_release': from_release, 'to_release': to_release, 'source': source, 'direction': direction, 'results': results })
def test_get_latest_release(self): latest_release = ReleaseUtils.get_latest_release() current_release = getattr(settings, "CURRENT_RELEASE", "92") self.assertEquals(latest_release, current_release, "Got the correct latest release " + latest_release)
def test_get_latest_assembly(self): latest_assembly = ReleaseUtils.get_latest_assembly() current_assembly = getattr(settings, "CURRENT_ASSEMBLY", "GRCh38") self.assertEquals(latest_assembly, current_assembly, "Got the correct latest assembly " + latest_assembly)
def test_get_all_assemblies(self): all_assemblies = ReleaseUtils.get_all_assemblies() self.assertEqual(len(all_assemblies), 2, "Got right assemblies")
def test_get_all_assembly_names(self): all_assemblies = ReleaseUtils.get_all_assembly_names() expected_list = ['GRCh37', 'GRCh38'] self.assertListEqual(all_assemblies, expected_list, "Got the right assembly list")
def test_get_all_release_sources(self): all_release_sources = ReleaseUtils.get_all_release_sources() expected_result = ['Ensembl', 'RefSeq'] self.assertListEqual(expected_result, all_release_sources, "Got the right sources")
def get_queryset(self): latest_release = self.kwargs['release_name'] latest_assembly = self.kwargs['assembly_name'] latest_source = self.kwargs['source_name'] if latest_release is None: latest_release = ReleaseUtils.get_latest_release() if latest_assembly is None: latest_assembly = ReleaseUtils.get_latest_assembly() if latest_source is None: latest_source = ReleaseUtils.get_default_source() self.unfiltered_query_set = query_set = Transcript.objects.filter( Q(transcript_release_set__shortname__icontains=latest_release) & Q(assembly__assembly_name__icontains=latest_assembly) & Q(transcript_release_set__source__shortname__icontains=latest_source)) order_by_index = int(self.request.query_params.get('order[0][column]', 0)) orderable = bool(self.request.query_params.get('columns[{}][orderable]'.format(order_by_index), 'false')) if order_by_index == 0 or not orderable: order_by_index = 1 order_by = self.request.query_params.get('columns[{}][data]'.format(order_by_index), self.default_order_by).\ replace('.', '__') order_by_dir = self.request.query_params.get('order[0][dir]', 'asc') if order_by_dir == 'desc': order_by = '-{}'.format(order_by) search_queries = self.request.query_params.get('search[value]', '').strip().split(' ') q = Q() if len(search_queries) > 0 and search_queries[0] != u'': for params in self.search_parameters: if "genes" == params: for query in search_queries: temp = { '{}__hgnc__name__icontains'.format(params): query, } q |= Q(**temp) else: for query in search_queries: if not query.isdigit() and params in ["transcript_id", "stable_id_version", "loc_start", "loc_end"]: continue temp = { '{}__icontains'.format(params): query, } q |= Q(**temp) query_set = query_set.filter(q).distinct() if order_by == '': return query_set return query_set.order_by(order_by)