def test_register_custom_factory(self): ## registering GA4GH variant factory GenericFactoryAvro.register_factory(Variant, GA4GHVariantFactory, version="4.0.0") factory = GenericFactoryAvro.get_factory_avro(Variant, "4.0.0") instances = factory.create_batch(5) for instance in instances: self.assertTrue(instance.validate(instance.toJsonDict())) self.assertTrue(instance.referenceBases in ["A", "C", "G", "T"]) ## register CancerReportedVariantsFactory GenericFactoryAvro.register_factory( protocols.reports_3_0_0.ReportedVariantCancer, CancerReportedVariantsFactory, version="3.0.0") factory = GenericFactoryAvro.get_factory_avro( protocols.reports_3_0_0.CancerInterpretationRequest, "3.0.0") instances = factory.create_batch(5) for instance in instances: self.assertTrue(instance.validate(instance.toJsonDict())) for tiered_variant in instance.TieredVariants: self.assertTrue(tiered_variant.reportedVariantCancer.reference in ["A", "C", "G", "T"]) self.assertTrue(tiered_variant.reportedVariantCancer.alternate in ["A", "C", "G", "T"])
def setUp(self): # avoids infinite recursion in mocked data # now creates another factory generating values for nullable fields file_factory = GenericFactoryAvro.get_factory_avro( protocols.reports_3_0_0.File, VERSION_300, False, False) GenericFactoryAvro.register_factory(protocols.reports_3_0_0.File, file_factory, VERSION_300, True)
def setUp(self): GenericFactoryAvro.register_factory(reports_4_0_0.Actions, ActionFactory, VERSION_400, fill_nullables=True) GenericFactoryAvro.register_factory(reports_4_0_0.Actions, ActionFactory, VERSION_400, fill_nullables=False)
def setUp(self): GenericFactoryAvro.register_factory(reports_3_0_0.File, self.FileFactory300, self.version_3_0_0, fill_nullables=True) GenericFactoryAvro.register_factory(reports_3_0_0.File, self.FileFactory300, self.version_3_0_0, fill_nullables=False)
def test_batch(self): # builds a batch of 5 interpretation requests interpretation_request_factory = GenericFactoryAvro.get_factory_avro( protocols.reports_4_2_0.InterpretationRequestRD, version=VERSION_500 ) instances = interpretation_request_factory.create_batch(5) for instance in instances: self.assertTrue(instance.validate(instance.toJsonDict())) self.assertTrue(instances[0].interpretationRequestId != instances[1].interpretationRequestId) GenericFactoryAvro.register_factory(protocols.ga4gh_3_0_0.Variant, GA4GHVariantFactory) GenericFactoryAvro.get_factory_avro(protocols.ga4gh_3_0_0.Variant)
def test_nullable_fields(self): # creates a factory for File not filling nullable fields and registers it in cache # as a factory that fill nullable fields # NOTE: this is the workaround to circumvent the loop in model definition file_factory = GenericFactoryAvro.get_factory_avro( protocols.reports_4_2_0.File, VERSION_500, False, False) GenericFactoryAvro.register_factory(protocols.reports_4_2_0.File, file_factory, VERSION_500, True) # get an interpretation request RD for reports 4.2.0 interpretation_request_factory = GenericFactoryAvro.get_factory_avro( protocols.reports_4_2_0.InterpretationRequestRD, version=VERSION_500, fill_nullables=True) instance = interpretation_request_factory() self.assertTrue(instance.validate(instance.toJsonDict()))
def setUp(self): GenericFactoryAvro.register_factory( reports_4_0_0.Actions, self.ActionFactory, self.version_4_0_0, fill_nullables=True) GenericFactoryAvro.register_factory( reports_4_0_0.Actions, self.ActionFactory, self.version_4_0_0, fill_nullables=False) GenericFactoryAvro.register_factory( participant_1_0_0.CancerParticipant, self.CancerParticipantFactory, self.version_4_0_0, fill_nullables=False) GenericFactoryAvro.register_factory( participant_1_0_0.CancerParticipant, self.CancerParticipantFactoryNulls, self.version_4_0_0, fill_nullables=True)
def test_migrate_cancer_participant(self): GenericFactoryAvro.register_factory( clazz=participant_1_0_0.CancerParticipant, factory=CancerParticipantFactory) old_participant = GenericFactoryAvro.get_factory_avro( clazz=participant_1_0_0.CancerParticipant, version=VERSION_400)() matched_samples = self.old_model.MatchedSamples( germlineSampleId='test_germline_id', tumourSampleId='test_tumour_id') old_participant.matchedSamples = [matched_samples] old_participant.LDPCode = 'test_LDP_code' self.assertIsInstance(old_participant, self.old_model.CancerParticipant) self._validate(old_participant) migrated_participant = MigrationParticipants100To103( ).migrate_cancer_participant(old_instance=old_participant) self.assertIsInstance(migrated_participant, self.new_model.CancerParticipant) self._validate(migrated_participant) self.assertIsInstance(migrated_participant.versionControl, self.new_model.VersionControl) self.assertDictEqual(migrated_participant.versionControl.toJsonDict(), {"GitVersionControl": "1.0.3"}) for germline_sample in migrated_participant.germlineSamples: self.assertEqual(germline_sample.LDPCode, old_participant.LDPCode) for tumour_sample in migrated_participant.tumourSamples: self.assertEqual(tumour_sample.LDPCode, old_participant.LDPCode) for matched_sample in migrated_participant.matchedSamples: self.assertIsInstance(matched_sample, self.new_model.MatchedSamples) for old_matched_sample, new_matched_sample in zip( old_participant.matchedSamples, migrated_participant.matchedSamples): self.assertDictEqual(old_matched_sample.toJsonDict(), new_matched_sample.toJsonDict())
def test_register_custom_factory(self): # creates a custom factory and registers it class ReportVersionControlFactory(FactoryAvro): def __init__(self, *args, **kwargs): super(ReportVersionControlFactory, self).__init__(*args, **kwargs) class Meta: model = protocols.reports_4_2_0.ReportVersionControl _version = VERSION_500 gitVersionControl = "4.3.0-SNAPSHOT" GenericFactoryAvro.register_factory( protocols.reports_4_2_0.ReportVersionControl, ReportVersionControlFactory, version=VERSION_500 ) interpretation_request_factory = GenericFactoryAvro.get_factory_avro( protocols.reports_4_2_0.InterpretationRequestRD, version=VERSION_500 ) instance_ir = interpretation_request_factory() self.assertTrue(instance_ir.validate(instance_ir.toJsonDict())) self.assertTrue(instance_ir.versionControl.gitVersionControl == "4.3.0-SNAPSHOT") # now creates another factory generating values for nullable fields file_factory = GenericFactoryAvro.get_factory_avro( protocols.reports_4_2_0.File, VERSION_500, False, False ) GenericFactoryAvro.register_factory( protocols.reports_4_2_0.File, file_factory, VERSION_500, True ) interpretation_request_factory2 = GenericFactoryAvro.get_factory_avro( protocols.reports_4_2_0.InterpretationRequestRD, version=VERSION_500, fill_nullables=True ) instance_ir2 = interpretation_request_factory2() self.assertTrue(instance_ir2.validate(instance_ir2.toJsonDict())) self.assertFalse(instance_ir2.versionControl.gitVersionControl == "4.3.0-SNAPSHOT") # now registers the factory for ReportVersionControl when filling nullables GenericFactoryAvro.register_factory( protocols.reports_4_2_0.ReportVersionControl, ReportVersionControlFactory, version=VERSION_500, fill_nullables=True ) instance_ir3 = interpretation_request_factory2() self.assertTrue(instance_ir3.validate(instance_ir3.toJsonDict())) self.assertTrue(instance_ir3.versionControl.gitVersionControl == "4.3.0-SNAPSHOT")
def setUp(self): GenericFactoryAvro.register_factory( reports_4_0_0.Actions, ActionFactory400, VERSION_400, fill_nullables=True) GenericFactoryAvro.register_factory( reports_3_0_0.Actions, ActionFactory300, VERSION_300, fill_nullables=True) GenericFactoryAvro.register_factory( reports_3_0_0.File, FileFactory300, VERSION_300, fill_nullables=True) GenericFactoryAvro.register_factory( reports_2_1_0.File, FileFactory210, VERSION_210, fill_nullables=True) GenericFactoryAvro.register_factory( reports_4_0_0.Actions, ActionFactory400, VERSION_400, fill_nullables=False) GenericFactoryAvro.register_factory( reports_3_0_0.Actions, ActionFactory300, VERSION_300, fill_nullables=False) GenericFactoryAvro.register_factory( reports_3_0_0.File, FileFactory300, VERSION_300, fill_nullables=False) GenericFactoryAvro.register_factory( reports_2_1_0.File, FileFactory210, VERSION_210, fill_nullables=False)
def setUp(self): GenericFactoryAvro.register_factory(reports_3_0_0.File, self.FileFactory300, self.version_3_0_0, fill_nullables=True) GenericFactoryAvro.register_factory(reports_3_0_0.File, self.FileFactory300, self.version_3_0_0, fill_nullables=False) GenericFactoryAvro.register_factory(reports_3_0_0.CalledGenotype, self.CalledGenotypeFactory300, self.version_3_0_0, fill_nullables=True) GenericFactoryAvro.register_factory(reports_3_0_0.RDParticipant, self.RDParticipantFactory300, self.version_3_0_0, fill_nullables=False) GenericFactoryAvro.register_factory(reports_3_0_0.RDParticipant, self.RDParticipantFactory300Nulls, self.version_3_0_0, fill_nullables=True) GenericFactoryAvro.register_factory(reports_3_0_0.Pedigree, self.PedigreeFactory300, self.version_3_0_0, fill_nullables=False) GenericFactoryAvro.register_factory(reports_3_0_0.Pedigree, self.PedigreeFactory300Nulls, self.version_3_0_0, fill_nullables=True) GenericFactoryAvro.register_factory(reports_3_0_0.HpoTerm, self.HpoTermFactory, self.version_3_0_0, fill_nullables=True)
def setUp(self): valid_tiers = [self.old_model.Tier.TIER1, self.old_model.Tier.TIER2, self.old_model.Tier.TIER3, self.old_model.Tier.NONE] # registers factories to ensure that tiers in report events are only valid ones class ReportEventFactoryNulls(FactoryAvro): def __init__(self, *args, **kwargs): super(ReportEventFactoryNulls, self).__init__(*args, **kwargs) class Meta: model = self.old_model.ReportEvent _version = VERSION_61 _fill_nullables = True tier = factory.fuzzy.FuzzyChoice(valid_tiers) GenericFactoryAvro.register_factory(self.old_model.ReportEvent, ReportEventFactoryNulls, VERSION_61, True) class ReportEventFactory(FactoryAvro): def __init__(self, *args, **kwargs): super(ReportEventFactory, self).__init__(*args, **kwargs) class Meta: model = self.old_model.ReportEvent _version = VERSION_61 _fill_nullables = False tier = factory.fuzzy.FuzzyChoice(valid_tiers) GenericFactoryAvro.register_factory(self.old_model.ReportEvent, ReportEventFactory, VERSION_61, False) valid_genomic_features = [self.old_model.GenomicEntityType.regulatory_region, self.old_model.GenomicEntityType.gene, self.old_model.GenomicEntityType.transcript] class GenomicEntityFactoryNulls(FactoryAvro): def __init__(self, *args, **kwargs): super(GenomicEntityFactoryNulls, self).__init__(*args, **kwargs) class Meta: model = self.old_model.GenomicEntity _version = VERSION_61 _fill_nullables = True type = factory.fuzzy.FuzzyChoice(valid_genomic_features) GenericFactoryAvro.register_factory(self.old_model.GenomicEntity, GenomicEntityFactoryNulls, VERSION_61, True) class GenomicEntityFactory(FactoryAvro): def __init__(self, *args, **kwargs): super(GenomicEntityFactory, self).__init__(*args, **kwargs) class Meta: model = self.old_model.GenomicEntity _version = VERSION_61 _fill_nullables = False type = factory.fuzzy.FuzzyChoice(valid_genomic_features) GenericFactoryAvro.register_factory(self.old_model.GenomicEntity, GenomicEntityFactory, VERSION_61, False) valid_genotypes = [ self.old_model.Zygosity.reference_homozygous, self.old_model.Zygosity.heterozygous, self.old_model.Zygosity.alternate_homozygous, self.old_model.Zygosity.missing, self.old_model.Zygosity.half_missing_reference, self.old_model.Zygosity.half_missing_alternate, self.old_model.Zygosity.alternate_hemizigous, self.old_model.Zygosity.reference_hemizigous, self.old_model.Zygosity.unk, ] class VariantCallFactoryNulls(FactoryAvro): def __init__(self, *args, **kwargs): super(VariantCallFactoryNulls, self).__init__(*args, **kwargs) class Meta: model = self.old_model.VariantCall _version = VERSION_61 _fill_nullables = True zygosity = factory.fuzzy.FuzzyChoice(valid_genotypes) GenericFactoryAvro.register_factory(self.old_model.VariantCall, VariantCallFactoryNulls, VERSION_61, True) class VariantCallFactory(FactoryAvro): def __init__(self, *args, **kwargs): super(VariantCallFactory, self).__init__(*args, **kwargs) class Meta: model = self.old_model.VariantCall _version = VERSION_61 _fill_nullables = False zygosity = factory.fuzzy.FuzzyChoice(valid_genotypes) GenericFactoryAvro.register_factory(self.old_model.VariantCall, VariantCallFactory, VERSION_61, False) # ensures that IR RD always have a pedigree class InterpretationRequestRDFactory(FactoryAvro): def __init__(self, *args, **kwargs): super(InterpretationRequestRDFactory, self).__init__(*args, **kwargs) class Meta: model = self.old_model.InterpretationRequestRD _version = VERSION_61 _fill_nullables = False pedigree = GenericFactoryAvro.get_factory_avro( self.old_model.Pedigree, VERSION_61, fill_nullables=_fill_nullables).create() GenericFactoryAvro.register_factory( self.old_model.InterpretationRequestRD, InterpretationRequestRDFactory, VERSION_61, False)