def test_embed_data_two_widgets(self): feature_track = pileup.Track( viz="features", label="myFeatures", source=pileup.sources.GA4GHFeatureJson('{}')) variant_track = pileup.Track( viz="variants", label="myVariants", source=pileup.sources.GA4GHVariantJson('{}')) w1 = pileup.PileupViewer(chrom="chr17", start=1, stop=250, reference="hg19", tracks=[feature_track]) w2 = pileup.PileupViewer(chrom="chr17", start=1, stop=250, reference="hg19", tracks=[variant_track]) jslink((w1, 'reference'), (w2, 'reference')) state = dependency_state([w1, w2], drop_defaults=True) data = embed_data(views=[w1, w2], drop_defaults=True, state=state) state = data['manager_state']['state'] views = data['view_specs'] assert len(views) == 2 model_names = [s['model_name'] for s in state.values()] widget_names = list( filter(lambda x: x == 'PileupViewerModel', model_names)) assert len(widget_names) == 2
def test_track_invalid_viz_features(self): pileup.Track(viz="features", label="myFeatures", source=pileup.sources.BigBedDataSource("fakeFile.bb")) with self.assertRaises(RuntimeError) as context: # viz features with incompatible GA4GHVariantJson source pileup.Track(viz="features", label="myFeatures", source=pileup.sources.GA4GHVariantJson("{}")) self.assertTrue('Invalid data source variantJson for track features' in context.exception)
def test_track_invalid_viz_coverage(self): pileup.Track(viz="coverage", label="myCoverage", source=pileup.sources.BamDataSource("fakeFile.bam")) with self.assertRaises(RuntimeError) as context: # viz coverage with incompatible TwoBit source pileup.Track(viz="coverage", label="myReads", source=pileup.sources.TwoBitDataSource("fakeFile.2bit")) self.assertTrue('Invalid data source twoBit for track coverage' in context.exception)
def test_track_invalid_viz_location(self): pileup.Track(viz="location", label="location") with self.assertRaises(RuntimeError) as context: # viz location with incompatible any source pileup.Track(viz="location", label="location", source=pileup.sources.BamDataSource("fakeFile.bam")) self.assertTrue('Invalid data source bam for track location' in context.exception)
def test_track_invalid_viz_genes(self): pileup.Track(viz="genes", label="myGenes", source=pileup.sources.BigBedDataSource("fakeFile.bb")) with self.assertRaises(RuntimeError) as context: # viz genes with incompatible bam data source pileup.Track(viz="genes", label="myGenes", source=pileup.sources.BamDataSource("fakeFile.bam")) self.assertTrue('Invalid data source bam for track genes' in context.exception)
def test_track_invalid_viz_variants(self): pileup.Track(viz="variants", label="myVariants", source=pileup.sources.GA4GHVariantSource("www.fakeEndpoint.com", "readGroup")) with self.assertRaises(RuntimeError) as context: # viz variants with incompatible GA4GHAlignmentSource source pileup.Track(viz="variants", label="myVariants", source=pileup.sources.GA4GHAlignmentSource("www.fakeEndpoint.com", "readGroup")) self.assertTrue('Invalid data source GAReadAlignment for track variants' in context.exception)
def test_track_invalid_viz_genome(self): pileup.Track(viz="genome", label="myReference", source=pileup.sources.TwoBitDataSource("fakeFile.2bit")) with self.assertRaises(RuntimeError) as context: # viz genome with incompatible GA4GHFeatureJson source pileup.Track(viz="genome", label="myReference", source=pileup.sources.GA4GHFeatureJson("{}")) self.assertTrue('Invalid data source featureJson for track genome' in context.exception)
def test_track_invalid_viz_location(self): pileup.Track(viz="location", label="location") with self.assertRaises(ValueError) as context: # viz location with incompatible any source pileup.Track(viz="location", label="location", source=pileup.sources.BamDataSource("fakeFile.bam")) self.assertTrue("Invalid viz 'location'" in str(context.exception)) self.assertTrue("Valid viz tracks include: coverage, pileup." in str( context.exception))
def test_track_invalid_viz_genome(self): pileup.Track(viz="genome", label="myReference", source=pileup.sources.TwoBitDataSource("fakeFile.2bit")) with self.assertRaises(ValueError) as context: # viz genome with incompatible GA4GHFeatureJson source pileup.Track(viz="genome", label="myReference", source=pileup.sources.GA4GHFeatureJson("{}")) self.assertTrue("Invalid viz 'genome'" in str(context.exception)) self.assertTrue("Valid viz tracks include: coverage, features." in str( context.exception))
def test_track_invalid_viz_features(self): pileup.Track(viz="features", label="myFeatures", source=pileup.sources.BigBedDataSource("fakeFile.bb")) with self.assertRaises(ValueError) as context: # viz features with incompatible GA4GHVariantJson source pileup.Track(viz="features", label="myFeatures", source=pileup.sources.GA4GHVariantJson("{}")) self.assertTrue("Invalid viz 'features'" in str(context.exception)) self.assertTrue("Valid viz tracks include: variants, genotypes." in str(context.exception))
def test_track_invalid_viz_genes(self): # should work pileup.Track(viz="genes", label="myGenes", source=pileup.sources.BigBedDataSource("fakeFile.bb")) with self.assertRaises(ValueError) as context: # viz genes with incompatible bam data source pileup.Track(viz="genes", label="myGenes", source=pileup.sources.BamDataSource("fakeFile.bam")) self.assertTrue("Invalid viz 'genes'" in str(context.exception)) self.assertTrue("Valid viz tracks include: coverage, pileup." in str( context.exception))
def test_track_invalid_viz_coverage(self): pileup.Track(viz="coverage", label="myCoverage", source=pileup.sources.BamDataSource("fakeFile.bam")) with self.assertRaises(ValueError) as context: # viz coverage with incompatible TwoBit source pileup.Track( viz="coverage", label="myReads", source=pileup.sources.TwoBitDataSource("fakeFile.2bit")) self.assertTrue("Invalid viz 'coverage'" in str(context.exception)) self.assertTrue( "Valid viz tracks include: genome." in str(context.exception))
def test_track_invalid_viz_variants(self): pileup.Track(viz="variants", label="myVariants", source=pileup.sources.GA4GHVariantSource( "www.fakeEndpoint.com", "readGroup")) with self.assertRaises(ValueError) as context: # viz variants with incompatible GA4GHAlignmentSource source pileup.Track(viz="variants", label="myVariants", source=pileup.sources.GA4GHAlignmentSource( "www.fakeEndpoint.com", "readGroup")) self.assertTrue("Invalid viz 'variants'" in str(context.exception)) self.assertTrue( "Valid viz tracks include: pileup." in str(context.exception))
def test_tracks_serialization(self): sourceOptions = { 'url': 'http://www.biodalliance.org/datasets/ensGene.bb' } gene_track=pileup.Track(viz="genes", label="myGenes", source=pileup.sources.BigBedDataSource(sourceOptions)) pileup_track = pileup.Track(viz="pileup", label="myReads", source=pileup.sources.GA4GHAlignmentJson(str)) tracks = [pileup_track, gene_track] serialized_tracks = tracks_to_json(tracks, any) assert(len(serialized_tracks) == 2) deserialized_tracks = tracks_from_json(serialized_tracks, any) assert(len(deserialized_tracks) == 2) assert(deserialized_tracks[0].viz == 'pileup') assert(deserialized_tracks[1].viz == 'genes')
def test_pileup(self): track = pileup.Track(viz="pileup", label="myReads", source=pileup.sources.GA4GHAlignmentJson('{}')) x = pileup.PileupViewer(locus="chr17:1-250", reference="hg19", tracks=[track]) assert (x.reference == 'hg19') assert (x.tracks[0] == track)
def test_variants(self): track = pileup.Track(viz="variants", label="myVariants", source=pileup.sources.GA4GHVariantJson('{}')) x = pileup.PileupViewer(locus="chr17:1-250", reference="hg19", tracks=[track]) assert (x.reference == 'hg19') assert (x.tracks[0] == track)
def test_genotypes(self): track = pileup.Track(viz="genotypes", label="myGenotypes", source=pileup.sources.VcfDataSource('{}')) x = pileup.PileupViewer(locus="chr22:21340030-21340150", reference="hg19", tracks=[track]) assert (x.reference == 'hg19') assert (x.tracks[0] == track)
def test_toSVG(self): track = pileup.Track( viz="genes", label="myGenes", source=pileup.sources.BigBedDataSource('fakeGenes.bb')) x = pileup.PileupViewer(chrom="chr17", start=1, stop=250, reference="hg19", tracks=[track]) x.getSVG()
def test_genes(self): track = pileup.Track( viz="genes", label="myGenes", source=pileup.sources.BigBedDataSource('fakeGenes.bb')) x = pileup.PileupViewer(locus="chr17:1-250", reference="hg19", tracks=[track]) assert (x.reference == 'hg19') assert (x.tracks[0] == track)
def test_features(self): track = pileup.Track(viz="features", label="myFeatures", source=pileup.sources.GA4GHFeatureJson('{}')) x = pileup.PileupViewer(chrom="chr17", start=1, stop=250, reference="hg19", tracks=[track]) assert (x.reference == 'hg19') assert (x.tracks[0] == track)
def test_minimal_pileup_html(self): track = pileup.Track(viz="pileup", label="myReads", source=pileup.sources.GA4GHAlignmentJson('{}')) w = pileup.PileupViewer(locus="chr17:1-250", reference="hg19", tracks=[track]) output = StringIO() state = dependency_state(w, drop_defaults=True) embed_minimal_html(output, views=w, drop_defaults=True, state=state) content = output.getvalue() assert content.splitlines()[0] == '<!DOCTYPE html>'
def test_snippet(self): class Parser(HTMLParser): state = 'initial' states = [] def handle_starttag(self, tag, attrs): attrs = dict(attrs) if tag == 'script' and attrs.get( 'type', '') == "application/vnd.jupyter.widget-state+json": self.state = 'widget-state' self.states.append(self.state) elif tag == 'script' and attrs.get( 'type', '') == "application/vnd.jupyter.widget-view+json": self.state = 'widget-view' self.states.append(self.state) def handle_endtag(self, tag): self.state = 'initial' def handle_data(self, data): if self.state == 'widget-state': manager_state = json.loads(data)['state'] assert len(manager_state) == 2 self.states.append('check-widget-state') elif self.state == 'widget-view': view = json.loads(data) assert isinstance(view, dict) self.states.append('check-widget-view') track = pileup.Track(viz="variants", label="myVariants", source=pileup.sources.GA4GHVariantJson('{}')) w = pileup.PileupViewer(chrom="chr17", start=1, stop=250, reference="hg19", tracks=[track]) state = dependency_state(w, drop_defaults=True) snippet = embed_snippet(views=w, drop_defaults=True, state=state) parser = Parser() parser.feed(snippet) assert parser.states == [ 'widget-state', 'check-widget-state', 'widget-view', 'check-widget-view' ]
def test_track_serialization(self): pileup_track = pileup.Track(viz="pileup", label="myReads", source=pileup.sources.GA4GHAlignmentJson(str)) # serialize track serialized_track = track_to_json(pileup_track, any) assert(serialized_track['viz'] == pileup_track.viz) assert(serialized_track['source'] == pileup_track.source) assert(serialized_track['sourceOptions'] == pileup_track.sourceOptions) assert(serialized_track['label'] == pileup_track.label) # deserialize_track deserialized_track = track_from_json(serialized_track, any) assert(deserialized_track.viz == pileup_track.viz) assert(deserialized_track.source == pileup_track.source) assert(deserialized_track.sourceOptions == pileup_track.sourceOptions) assert(deserialized_track.label == pileup_track.label)
def test_embed_pileup(self): track = pileup.Track(viz="features", label="myFeatures", source=pileup.sources.GA4GHFeatureJson('{}')) w = pileup.PileupViewer(locus="chr17:1-250", reference="hg19", tracks=[track]) state = dependency_state(w, drop_defaults=True) data = embed_data(views=w, drop_defaults=True, state=state) state = data['manager_state']['state'] views = data['view_specs'] assert len(views) == 1 model_names = [s['model_name'] for s in state.values()] assert 'PileupViewerModel' in model_names