class PhenotypeResourceTestCase(AuthenticatedBaseTestCase): def setUp(self): # Create a new phenotype objects and store it self.phenotype = Phenotype(term="Hyterazygoze") self.phenotype.save() super(PhenotypeResourceTestCase, self).setUp() def test_get_empty(self): response = self.client.get('/api/phenotypes/999/', HTTP_ACCEPT='application/json') self.assertEqual(response.status_code, codes.not_found) def test_get(self): response = self.client.get('/api/phenotypes/{0}/'.format( self.phenotype.id), HTTP_ACCEPT='application/json') self.assertEqual(response.status_code, codes.ok) self.assertTrue(response.content) # Testing phenotype query response = self.client.get('/api/phenotypes/', HTTP_ACCEPT='application/json') self.assertEqual(response.status_code, codes.ok) response_obj = json.loads(response.content) # Query should return 1 phenotype and code should be ok self.assertEqual(response_obj['result_count'], 1)
def setUp(self): # Creating objects needed for variant self.pubmed = PubMed(pmid=1) self.pubmed.save() self.phenotype = Phenotype(term='Hymocompos', hpo_id=1) self.phenotype.save() # Retrive a Chromosome and varianttype self.chromo = Chromosome.objects.get(pk=1) self.chromo.save() self.varianttype = VariantType.objects.get(pk=1) self.varianttype.save() super(VariantsResourcesTestCase, self).setUp()
def load_hgmd_snp(self, cursor, using=None): cursor.execute(''' select distinct cl.acc_num as hgmd_id, c.value as chr, c.id as chr_id, v.id as variant_id, trim(both from cl.disease) as phenotype, ph.id as phenotype_id, cl.gene as gene, g.id as gene_id, cl.pmid as pubmed, pm.pmid as pubmed_id from ( select m.acc_num, m.disease, m.gene, m.pmid, c.chromosome, c."coordSTART" as pos from raw.hgmd_mutation m inner join raw.hgmd_hg19_coords c on (m.acc_num = c.acc_num) ) cl left outer join chromosome c on (cl.chromosome = c.value) left outer join variant v on (c.id = v.chr_id and cl.pos = v.pos) left outer join variant_type vt on (v.type_id = vt.id) left outer join phenotype ph on (lower(trim(both from regexp_replace(cl.disease, '\s*\?$', ''))) = lower(ph.term)) left outer join pubmed pm on (cl.pmid::varchar = pm.pmid::varchar) left outer join gene g on (cl.gene::text = g.symbol::text) left outer join variant_phenotype vp on (vp.variant_id = v.id) where vt.value = 'SNP' and cl.disease not like '%%?' and v.id is not null and g.id is not null order by c.id ''') keys = ['hgmd_id', 'chr', 'chr_id', 'variant_id', 'phenotype', 'phenotype_id', 'gene', 'gene_id', 'pubmed', 'pubmed_id'] count = 0 new_pubmed_map = {} new_phenotype_map = {} chrs = dict(Chromosome.objects.values_list('value', 'id')) while True: rows = cursor.fetchmany(100) if not rows: break for row in rows: record = dict(zip(keys, row)) # Get or create a pubmed record if record['pubmed_id']: pubmed = PubMed(pmid=record['pubmed_id']) pubmed._state.db = using # Some records have a bogus PMID. Only proces the valid ones. elif type(record['pubmed']) is int or record['pubmed'].isdigit(): pmid = int(record['pubmed']) if pmid in new_pubmed_map: pubmed = new_pubmed_map[pmid] else: pubmed = PubMed(pmid=pmid) pubmed.save() new_pubmed_map[pmid] = pubmed else: pubmed = None # Get or create a the phenotype, associate the HGMD id with if record['phenotype_id']: phenotype = Phenotype(pk=record['phenotype_id']) phenotype._state.db = using else: term = record['phenotype'] # Check newly added objects if term in new_phenotype_map: phenotype = new_phenotype_map[term] else: phenotype = Phenotype(term=record['phenotype']) phenotype.save() new_phenotype_map[term] = phenotype _chr = Chromosome(pk=chrs[record['chr']]) _chr._state.db = using if record['gene_id']: gene = Gene(pk=record['gene_id']) gene._state.db = using try: gp = GenePhenotype.objects.get(gene=gene, phenotype=phenotype) except GenePhenotype.DoesNotExist: gp = GenePhenotype(gene=gene, phenotype=phenotype) gp.hgmd_id = record['hgmd_id'] gp.save() else: gene = None if record['variant_id']: variant = Variant(pk=record['variant_id']) variant._state.db = using try: vp = VariantPhenotype.objects.get(variant=variant, phenotype=phenotype) except VariantPhenotype.DoesNotExist: vp = VariantPhenotype(variant=variant, phenotype=phenotype) vp.hgmd_id = record['hgmd_id'] vp.save() else: variant = None if pubmed: phenotype.articles.add(pubmed) if variant: variant.articles.add(pubmed) if gene: gene.articles.add(pubmed) count += 1 sys.stdout.write('{0}\r'.format(count)) sys.stdout.flush() return count
def load_hgmd_phenotypes(label, keys, cursor, using): count = 0 total = 0 # Local storage for new instances pmids = {} phenotypes = {} while True: rows = cursor.fetchmany(100) if not rows: break for row in rows: record = dict(zip(keys, row)) pubmed = gene = variant = None new_phenotype = saved = False # PubMed IDs if record["pubmed_id"]: pubmed = PubMed(pk=record["pubmed_id"]) pubmed._state.db = using # Some records have a bogus PMID. Only proces the valid ones. elif type(record["pubmed"]) is int or record["pubmed"].isdigit(): pmid = int(record["pubmed"]) if pmid in pmids: pubmed = PubMed(pk=pmids[pmid]) pubmed._state.db = using else: pubmed = PubMed(pmid=pmid) pubmed.save() pmids[pmid] = pubmed.pk # Phenotypes if record["phenotype_id"]: phenotype = Phenotype(pk=record["phenotype_id"]) phenotype._state.db = using else: new_phenotype = True term = record["phenotype"] if term in phenotypes: phenotype = Phenotype(pk=phenotypes[term]) phenotype._state.db = using else: phenotype = Phenotype(term=term) phenotype.save() phenotypes[term] = phenotype.pk # Variants variant = Variant(pk=record["variant_id"]) variant._state.db = using if ( new_phenotype or not VariantPhenotype.objects.filter( variant=variant, phenotype=phenotype, hgmd_id=record["hgmd_id"] ).exists() ): vp = VariantPhenotype(variant=variant, phenotype=phenotype, hgmd_id=record["hgmd_id"]) vp.save() saved = True # Genes if record["gene_id"]: gene = Gene(pk=record["gene_id"]) gene._state.db = using if ( new_phenotype or not GenePhenotype.objects.filter( gene=gene, phenotype=phenotype, hgmd_id=record["hgmd_id"] ).exists() ): gp = GenePhenotype(gene=gene, phenotype=phenotype, hgmd_id=record["hgmd_id"]) gp.save() saved = True # Associate articles with other objects if pubmed: phenotype.articles.add(pubmed) if variant: variant.articles.add(pubmed) if gene: gene.articles.add(pubmed) total += 1 if saved: count += 1 if count % BATCH_SIZE == 0: transaction.commit() log.debug("Loading {0}...{1}/{2}".format(label, count, total)) # Print a newline for the terminal prompt print
def setUp(self): # Create a new phenotype objects and store it self.phenotype = Phenotype(term="Hyterazygoze") self.phenotype.save() super(PhenotypeResourceTestCase, self).setUp()
class VariantsResourcesTestCase(AuthenticatedBaseTestCase): fixtures = ['initial_data.json'] def setUp(self): # Creating objects needed for variant self.pubmed = PubMed(pmid=1) self.pubmed.save() self.phenotype = Phenotype(term='Hymocompos', hpo_id=1) self.phenotype.save() # Retrive a Chromosome and varianttype self.chromo = Chromosome.objects.get(pk=1) self.chromo.save() self.varianttype = VariantType.objects.get(pk=1) self.varianttype.save() super(VariantsResourcesTestCase, self).setUp() def test_get_empty(self): response = self.client.get('/api/variants/999/', HTTP_ACCEPT='application/json') self.assertEqual(response.status_code, codes.not_found) def test_get_all(self): count_before = Variant.objects.count() new_variant = Variant(chr=self.chromo, pos=1, ref="jon", alt="ba", md5="kid", type=self.varianttype,) new_variant.save() count_after = Variant.objects.count() # Now try making a get request with the objects id response = self.client.get('/api/variants/{0}/'.format(new_variant.id), HTTP_ACCEPT='application/json') # Request should be good and the count should have changed self.assertEqual(response.status_code, codes.ok) self.assertEqual(count_after, count_before+1) def test_get_empty_metrics(self): Variant.objects.all().delete() # Bad request, should return 404 response = self.client.get('/api/variants/1/assessment-metrics/', HTTP_ACCEPT='application/json') self.assertEqual(response.status_code, codes.not_found) def test_get_metrics(self): # Create a variant object new_variant = Variant(chr=self.chromo, pos=1, ref="jon", alt="ba", md5="kid", type=self.varianttype,) new_variant.save() response = self.client.get('/api/variants/{0}/assessment-metrics/'. format(new_variant.id), HTTP_ACCEPT='application/json') # Should be ok self.assertEqual(response.status_code, codes.ok)