class Translation(models.Model): MANY2ONE_RELATED = { 'SEQUENCE': 'sequence', 'SESSION': 'session', 'ASSEMBLY': 'assembly' } ONE2MANY_RELATED = { 'RELEASE_SET': 'translation_release_set', 'TRANSCRIPT': "transcripts" } translation_id = models.AutoField(primary_key=True) stable_id = models.CharField(max_length=64) stable_id_version = models.PositiveIntegerField() assembly = models.ForeignKey(Assembly, models.DO_NOTHING, blank=True, null=True) loc_start = models.PositiveIntegerField(blank=True, null=True) loc_end = models.PositiveIntegerField(blank=True, null=True) loc_strand = models.IntegerField(blank=True, null=True) loc_region = models.CharField(max_length=42, blank=True, null=True) loc_checksum = ChecksumField(unique=True, max_length=20, blank=True, null=True) translation_checksum = ChecksumField(unique=True, max_length=20, blank=True, null=True) sequence = models.ForeignKey(Sequence, models.DO_NOTHING, db_column='seq_checksum', blank=True, null=True) session = models.ForeignKey(Session, models.DO_NOTHING, blank=True, null=True) translation_release_set = models.ManyToManyField( 'release.ReleaseSet', through='release.TranslationReleaseTag', related_name='translation_release_set') transcripts = models.ManyToManyField( 'transcript.Transcript', through='translation.TranslationTranscript') class Meta: managed = False db_table = 'translation'
class ReleaseSet(models.Model): MANY2ONE_RELATED = {'RELEASE_SOURCE': 'release_source'} ONE2MANY_RELATED = {'RELEASE_SET': 'release_set'} release_id = models.AutoField(primary_key=True) shortname = models.CharField(max_length=24, blank=True, null=True) description = models.CharField(max_length=256, blank=True, null=True) assembly = models.ForeignKey(Assembly, models.DO_NOTHING, blank=True, null=True) release_date = models.DateField(blank=True, null=True) session = models.ForeignKey(Session, models.DO_NOTHING, blank=True, null=True) release_checksum = ChecksumField(unique=True, max_length=20, blank=True, null=True) source = models.ForeignKey(ReleaseSource, models.DO_NOTHING, blank=True, null=True) class Meta: managed = False db_table = 'release_set' unique_together = (('shortname', 'assembly', 'source'), )
class Gene(models.Model): MANY2ONE_RELATED = { 'SESSION': 'session', 'ASSEMBLY': 'assembly', 'HGNC': 'name' } ONE2MANY_RELATED = {'RELEASE_SET': 'gene_release_set'} gene_id = models.AutoField(primary_key=True) stable_id = models.CharField(max_length=64) stable_id_version = models.PositiveIntegerField() assembly = models.ForeignKey(Assembly, models.DO_NOTHING, blank=True, null=True) loc_start = models.PositiveIntegerField(blank=True, null=True) loc_end = models.PositiveIntegerField(blank=True, null=True) loc_strand = models.IntegerField(blank=True, null=True) loc_region = models.CharField(max_length=42, blank=True, null=True) loc_checksum = ChecksumField(unique=True, max_length=20, blank=True, null=True) name = HGNCField(GeneNames, models.DO_NOTHING, to_field='external_id', blank=True, null=True) gene_checksum = ChecksumField(unique=True, max_length=20, blank=True, null=True) session = models.ForeignKey(Session, models.DO_NOTHING, blank=True, null=True) gene_release_set = models.ManyToManyField('release.ReleaseSet', through='release.GeneReleaseTag', related_name='gene_release_set') class Meta: managed = False db_table = 'gene'
class Sequence(models.Model): seq_checksum = ChecksumField(max_length=20, blank=False, null=False, primary_key=True) sequence = models.TextField(blank=True, null=True) session = models.ForeignKey(Session, models.DO_NOTHING, blank=True, null=True) class Meta: managed = False db_table = 'sequence'
class Transcript(models.Model): MANY2ONE_RELATED = { 'SEQUENCE': 'sequence', 'SESSION': 'session', 'ASSEMBLY': 'assembly' } ONE2MANY_RELATED = { 'RELEASE_SET': 'transcript_release_set', 'GENE': 'genes', 'TRANSLATION': "translations", "EXONTRANSCRIPT": "exons" } # You'll normally want to ensure that you've set an appropriate related_name argument on the relationship, # that you can use as the field name. transcript_id = models.AutoField(primary_key=True) stable_id = models.CharField(max_length=64) stable_id_version = models.PositiveIntegerField() assembly = models.ForeignKey(Assembly, models.DO_NOTHING, blank=True, null=True) loc_start = models.PositiveIntegerField(blank=True, null=True) loc_end = models.PositiveIntegerField(blank=True, null=True) loc_strand = models.IntegerField(blank=True, null=True) loc_region = models.CharField(max_length=42, blank=True, null=True) loc_checksum = ChecksumField(unique=True, max_length=20, blank=True, null=True) exon_set_checksum = ChecksumField(unique=True, max_length=20, blank=True, null=True) transcript_checksum = ChecksumField(unique=True, max_length=20, blank=True, null=True) sequence = models.ForeignKey(Sequence, models.DO_NOTHING, db_column='seq_checksum', blank=True, null=True) session = models.ForeignKey(Session, models.DO_NOTHING, blank=True, null=True) transcript_release_set = models.ManyToManyField( 'release.ReleaseSet', through='release.TranscriptReleaseTag', related_name='transcript_release_set') biotype = models.CharField(max_length=40, blank=True, null=True) genes = models.ManyToManyField('gene.Gene', through='transcript.TranscriptGene') exons = models.ManyToManyField('exon.Exon', through='exon.ExonTranscript') translations = models.ManyToManyField( 'translation.Translation', through='translation.TranslationTranscript') class Meta: managed = False db_table = 'transcript' @classmethod def fetch_mane_transcript_and_type(cls, transcript_id=None): transcript = None source = "Ensembl" if transcript_id is not None: transcript = Transcript.objects.get(pk=transcript_id) if transcript is not None: try: source = transcript.transcript_release_set.all()[:1].get( ).source.shortname except Exception as e: logger.error("Exception from get_mane_transcript " + str(e)) if "Ensembl" in source: raw_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 \ 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" else: raw_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 \ 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_subject_id \ JOIN transcript_release_tag trt2 ON \ transcript_release_tag_relationship.transcript_release_object_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" if transcript_id is not None: raw_sql = raw_sql + " WHERE t1.transcript_id=%s limit 1" mane_transcripts = Transcript.objects.raw(raw_sql, [transcript_id]) mane_transcript_dict = {} if mane_transcripts is not None and len( list(mane_transcripts)) > 0: mane_transcript = mane_transcripts[0] mane_transcript_dict = { "mane_transcript_stableid": "{}.{}".format(mane_transcript.refseq_stable_id, mane_transcript.refseq_stable_id_version), "mane_transcript_type": mane_transcript.mane_type } return mane_transcript_dict else: raw_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" mane_transcripts = Transcript.objects.raw(raw_sql) return mane_transcripts