def test_init_with_single_treatment(self): treatment = Treatment( treatment_type=INTERVENTIONS['CHEMICAL'], factor_values=(FactorValue(factor_name=self.agent, value='crack'), FactorValue(factor_name=self.intensity, value='low'), FactorValue(factor_name=self.duration, value='short'))) new_sequence = TreatmentSequence(ranked_treatments=treatment) self.assertTrue((treatment, 1) in new_sequence.ranked_treatments)
def setUp(self): self.maxDiff = None self.treatment = Treatment( factor_values=(FactorValue(factor_name=BASE_FACTORS[0][NAME], value=FACTORS_0_VALUE), FactorValue(factor_name=BASE_FACTORS[1][NAME], value=FACTORS_1_VALUE, unit=FACTORS_1_UNIT), FactorValue(factor_name=BASE_FACTORS[2][NAME], value=FACTORS_2_VALUE, unit=FACTORS_2_UNIT)))
def test_eq(self): same_treatment = Treatment( factor_values=(FactorValue(factor_name=BASE_FACTORS[0][NAME], value=FACTORS_0_VALUE), FactorValue(factor_name=BASE_FACTORS[1][NAME], value=FACTORS_1_VALUE, unit=FACTORS_1_UNIT), FactorValue(factor_name=BASE_FACTORS[2][NAME], value=FACTORS_2_VALUE, unit=FACTORS_2_UNIT))) self.assertEqual(self.treatment, same_treatment) self.assertEqual(hash(self.treatment), hash(same_treatment))
def test_ne(self): other_treatment = Treatment( factor_values=(FactorValue(factor_name=BASE_FACTORS[0][NAME], value=FACTORS_0_VALUE), FactorValue(factor_name=BASE_FACTORS[1][NAME], value=FACTORS_1_VALUE, unit=FACTORS_1_UNIT), FactorValue(factor_name=BASE_FACTORS[2][NAME], value=FACTORS_2_VALUE_ALT, unit=FACTORS_2_UNIT))) self.assertNotEqual(self.treatment, other_treatment) self.assertNotEqual(hash(self.treatment), hash(other_treatment))
def setUp(self): self.maxDiff = None self.sequence = TreatmentSequence() self.agent = StudyFactor(name=BASE_FACTORS[0]['name'], factor_type=BASE_FACTORS[0]['type']) self.intensity = StudyFactor(name=BASE_FACTORS[1]['name'], factor_type=BASE_FACTORS[1]['type']) self.duration = StudyFactor(name=BASE_FACTORS[2]['name'], factor_type=BASE_FACTORS[2]['type']) self.test_treatment = Treatment( treatment_type=INTERVENTIONS['CHEMICAL'], factor_values=(FactorValue(factor_name=self.agent, value='crack'), FactorValue(factor_name=self.intensity, value='low'), FactorValue(factor_name=self.duration, value='short')))
def setUp(self): self.design = InterventionStudyDesign() self.agent = StudyFactor(name=BASE_FACTORS[0]['name'], factor_type=BASE_FACTORS[0]['type']) self.intensity = StudyFactor(name=BASE_FACTORS[1]['name'], factor_type=BASE_FACTORS[1]['type']) self.duration = StudyFactor(name=BASE_FACTORS[2]['name'], factor_type=BASE_FACTORS[2]['type']) self.first_treatment = Treatment( treatment_type=INTERVENTIONS['CHEMICAL'], factor_values=(FactorValue(factor_name=self.agent, value='crack'), FactorValue(factor_name=self.intensity, value='low'), FactorValue(factor_name=self.duration, value='medium'))) self.second_treatment = Treatment( treatment_type=INTERVENTIONS['CHEMICAL'], factor_values=(FactorValue(factor_name=self.agent, value='crack'), FactorValue(factor_name=self.intensity, value='high'), FactorValue(factor_name=self.duration, value='medium'))) self.test_sequence = TreatmentSequence( ranked_treatments=[(self.first_treatment, 1), (self.second_treatment, 2)]) self.sample_plan = SampleAssayPlan(group_size=10, sample_plan={}, assay_plan=None)
def unserialize_factor_value(json_obj): factor_name = StudyFactor() if 'factorName' in json_obj and json_obj['factorName'] is not None: factor_name = unserialize_study_factor(json_obj['factorName']) value = OntologyAnnotation() if 'value' in json_obj and json_obj['value'] is not None: value = unserialize_ontology_annotation(json_obj['value']) unit = OntologyAnnotation() if 'unit' in json_obj and json_obj['unit'] is not None: unit = unserialize_ontology_annotation(json_obj['value']) comments = list() if 'comments' in json_obj and json_obj['comments'] is not None: for comment in json_obj['comments']: comments.append(unserialize_comment(comment)) return FactorValue(factor_name=factor_name, value=value, unit=unit, comments=comments)
def test_repr(self): treatments = [ (Treatment(treatment_type=INTERVENTIONS['CHEMICAL'], factor_values=(FactorValue(factor_name=self.agent, value='crack'), FactorValue(factor_name=self.intensity, value='low'), FactorValue(factor_name=self.duration, value='short'))), 1), (Treatment(treatment_type=INTERVENTIONS['CHEMICAL'], factor_values=(FactorValue(factor_name=self.agent, value='crack'), FactorValue(factor_name=self.intensity, value='low'), FactorValue(factor_name=self.duration, value='long'))), 2) ] new_sequence = TreatmentSequence(ranked_treatments=treatments) self.assertEqual( 'TreatmentSequence([(Treatment(factor_type=chemical ' 'intervention, factor_values=(FactorValue(factor_name=' 'StudyFactor(name="AGENT", factor_type=' 'OntologyAnnotation(term="perturbation agent", ' 'term_source=None, term_accession="", comments=[]), ' 'comments=[]), value=crack, unit=None), FactorValue(' 'factor_name=StudyFactor(name="INTENSITY", ' 'factor_type=OntologyAnnotation(term="intensity", ' 'term_source=None, term_accession="", comments=[]), ' 'comments=[]), value=low, unit=None), FactorValue(' 'factor_name=StudyFactor(name="DURATION", ' 'factor_type=OntologyAnnotation(term="time", ' 'term_source=None, term_accession="", comments=[]), ' 'comments=[]), value=short, unit=None))), 1), (' 'Treatment(factor_type=chemical intervention, ' 'factor_values=(FactorValue(factor_name=StudyFactor(' 'name="AGENT", factor_type=OntologyAnnotation(' 'term="perturbation agent", term_source=None, ' 'term_accession="", comments=[]), comments=[]), ' 'value=crack, unit=None), FactorValue(factor_name=' 'StudyFactor(name="INTENSITY", factor_type=' 'OntologyAnnotation(term="intensity", ' 'term_source=None, term_accession="", comments=[]), ' 'comments=[]), value=low, unit=None), FactorValue(' 'factor_name=StudyFactor(name="DURATION", ' 'factor_type=OntologyAnnotation(term="time", ' 'term_source=None, term_accession="", comments=[]), ' 'comments=[]), value=long, unit=None))), 2)])', repr(new_sequence))
def test_init_with_treatments_list_no_rank(self): treatments = [ Treatment(treatment_type=INTERVENTIONS['CHEMICAL'], factor_values=(FactorValue(factor_name=self.agent, value='crack'), FactorValue(factor_name=self.intensity, value='low'), FactorValue(factor_name=self.duration, value='short'))), Treatment(treatment_type=INTERVENTIONS['CHEMICAL'], factor_values=(FactorValue(factor_name=self.agent, value='crack'), FactorValue(factor_name=self.intensity, value='low'), FactorValue(factor_name=self.duration, value='long'))) ] new_sequence = TreatmentSequence(ranked_treatments=treatments) self.assertTrue((treatments[0], 1) in new_sequence.ranked_treatments) self.assertTrue((treatments[1], 1) in new_sequence.ranked_treatments)
def test_ranked_treatments_setter(self): treatments = [ (Treatment(treatment_type=INTERVENTIONS['CHEMICAL'], factor_values=(FactorValue(factor_name=self.agent, value='crack'), FactorValue(factor_name=self.intensity, value='low'), FactorValue(factor_name=self.duration, value='short'))), 2), (Treatment(treatment_type=INTERVENTIONS['CHEMICAL'], factor_values=(FactorValue(factor_name=self.agent, value='crack'), FactorValue(factor_name=self.intensity, value='low'), FactorValue(factor_name=self.duration, value='long'))), 1) ] self.sequence.ranked_treatments = treatments self.assertTrue(isinstance(self.sequence.ranked_treatments, set)) self.assertTrue(treatments[0] in self.sequence.ranked_treatments) self.assertTrue(treatments[1] in self.sequence.ranked_treatments)
def test_eq(self): treatments = [ (Treatment(treatment_type=INTERVENTIONS['CHEMICAL'], factor_values=(FactorValue(factor_name=self.agent, value='crack'), FactorValue(factor_name=self.intensity, value='low'), FactorValue(factor_name=self.duration, value='short'))), 1), (Treatment(treatment_type=INTERVENTIONS['CHEMICAL'], factor_values=(FactorValue(factor_name=self.agent, value='crack'), FactorValue(factor_name=self.intensity, value='low'), FactorValue(factor_name=self.duration, value='long'))), 2) ] first_sequence = TreatmentSequence(treatments) second_sequence = TreatmentSequence(reversed(treatments)) self.assertEqual(first_sequence, second_sequence) self.assertEqual(hash(first_sequence), hash(second_sequence))
characteristics=characteristics) # print(source) study.sources.append(source) sample = Sample(name=ou["observationUnitDbId"]) if 'treatments' in ou.keys(): for element in ou['treatments']: for key in element.keys(): f = StudyFactor(name=key, factor_type=OntologyAnnotation(term=key)) if f not in study.factors: study.factors.append(f) fv = FactorValue(factor_name=f, value=OntologyAnnotation(term=str( element[key]), term_source="", term_accession="")) sample.factor_values.append(fv) print(sample) if 'observations' in ou.keys(): for ob in ou['observations']: phenotyping_process = Process( executes_protocol=phenotyping_protocol) phenotyping_process.name = "assay-name-{}".format(counter) phenotyping_process.inputs.append(sample) # print(ob['observations']) if 'season' in ou['observations'][0].keys(): # if 'season' in phenotyping_protocol.parameter_list: # pass
def create_study_sample_and_assay(client, brapi_study_id, isa_study, sample_collection_protocol, phenotyping_protocol): obsunit_to_isasample_mapping_dictionary = { "X": "X", "Y": "Y", "blockNumber": "Block Number", "plotNumber": "Plot Number", "plantNumber": "Plant Number", "observationLevel": "Observation unit type" } obsunit_to_isaassay_mapping_dictionary = { "X": "X", "Y": "Y", "blockNumber": "Block Number", "plotNumber": "Plot Number", "plantNumber": "Plant Number", "observationLevel": "Observation unit type" } allready_converted_obs_unit = [] # Allow to handle multiyear observation units for obs_unit in client.get_study_observation_units(brapi_study_id): # Getting the relevant germplasm used for that observation event: # --------------------------------------------------------------- this_source = isa_study.get_source(obs_unit['germplasmName']) #logger.debug("testing for the source reference: ", str(this_source)) # TODO Assumed one assay by study. Will need to move to one assay by level/datalink this_assay = isa_study.assays[0] # Sample == Observation Unit if this_source is not None and obs_unit['observationUnitName'] not in allready_converted_obs_unit: #The observationUnitName is the buisness ID of the Observation unit (ie plot number) while the ID is the PK this_isa_sample = Sample( name= obs_unit['observationUnitName'], #name=obs_unit['observationUnitDbId'] + "_" + obs_unit['observationUnitName'], derives_from=[this_source]) allready_converted_obs_unit.append(obs_unit['observationUnitName']) for key in obs_unit.keys(): if key in obsunit_to_isasample_mapping_dictionary.keys(): if isinstance(obsunit_to_isasample_mapping_dictionary[key], str) and str(obs_unit[key]) is not None : c = Characteristic(category=OntologyAnnotation(term=obsunit_to_isasample_mapping_dictionary[key]), value=OntologyAnnotation(term=str(obs_unit[key]), term_source="", term_accession="")) this_isa_sample.characteristics.append(c) # else: #no defult behaviour, it is maped or ignored # c = Characteristic(category=OntologyAnnotation(term=key), # value=OntologyAnnotation(term=str(obs_unit[key]), # term_source="", # term_accession="")) # this_isa_sample.characteristics.append(c) if key in obsunit_to_isaassay_mapping_dictionary.keys(): if isinstance(obsunit_to_isaassay_mapping_dictionary[key], str): c = Characteristic(category=OntologyAnnotation(term=obsunit_to_isaassay_mapping_dictionary[key]), value=OntologyAnnotation(term=str(obs_unit[key]), term_source="", term_accession="")) #TODO: quick workaround used to store observation units characteristics this_assay.comments.append(c) # if 'observationLevel' in obs_unit.keys(): # # TODO: if absent, a warning should be logged as this is a MIAPPE requirement # Looking for treatment in BRAPI and mapping to ISA Study Factor Value # -------------------------------------------------------------------- if 'treatments' in obs_unit.keys(): for element in obs_unit['treatments']: for key in element.keys(): f = StudyFactor(name=key, factor_type=OntologyAnnotation(term=key)) if f not in isa_study.factors: isa_study.factors.append(f) fv = FactorValue(factor_name=f, value=OntologyAnnotation(term=str(element[key]), term_source="", term_accession="")) this_isa_sample.factor_values.append(fv) isa_study.samples.append(this_isa_sample) # print("counting observations: ", i, "before: ", this_source.name) # TODO: Add Comment[Factor Values] : iterate through BRAPI treatments to obtain all possible values for a given Factor # Creating the corresponding ISA sample entity for structure the document: # ------------------------------------------------------------------------ sample_collection_process = Process(executes_protocol=sample_collection_protocol) sample_collection_process.performer = "n.a." sample_collection_process.date = datetime.datetime.today().isoformat() sample_collection_process.inputs.append(this_source) sample_collection_process.outputs.append(this_isa_sample) isa_study.process_sequence.append(sample_collection_process) #logger.debug(str(this_assay)) # Creating the relevant ISA protocol application / Assay from BRAPI Observation Events: # ------------------------------------------------------------------------------------- create_data_file(obs_unit, this_assay, sample_collection_process, this_isa_sample, phenotyping_protocol)
def test_compute_full_factorial_design(self): agent = StudyFactor(name=BASE_FACTORS[0]['name'], factor_type=BASE_FACTORS[0]['type']) intensity = StudyFactor(name=BASE_FACTORS[1]['name'], factor_type=BASE_FACTORS[1]['type']) duration = StudyFactor(name=BASE_FACTORS[2]['name'], factor_type=BASE_FACTORS[2]['type']) self.factory.add_factor_value(agent, {'cocaine', 'crack', 'aether'}) self.factory.add_factor_value(intensity, {'low', 'medium', 'high'}) self.factory.add_factor_value(duration, {'short', 'long'}) full_factorial = self.factory.compute_full_factorial_design() self.assertEqual( full_factorial, { Treatment(treatment_type=INTERVENTIONS['CHEMICAL'], factor_values=(FactorValue(factor_name=agent, value='cocaine'), FactorValue(factor_name=intensity, value='high'), FactorValue(factor_name=duration, value='long'))), Treatment(treatment_type=INTERVENTIONS['CHEMICAL'], factor_values=(FactorValue(factor_name=agent, value='cocaine'), FactorValue(factor_name=intensity, value='high'), FactorValue(factor_name=duration, value='short'))), Treatment(treatment_type=INTERVENTIONS['CHEMICAL'], factor_values=(FactorValue(factor_name=agent, value='cocaine'), FactorValue(factor_name=intensity, value='low'), FactorValue(factor_name=duration, value='long'))), Treatment(treatment_type=INTERVENTIONS['CHEMICAL'], factor_values=(FactorValue(factor_name=agent, value='cocaine'), FactorValue(factor_name=intensity, value='low'), FactorValue(factor_name=duration, value='short'))), Treatment(treatment_type=INTERVENTIONS['CHEMICAL'], factor_values=(FactorValue(factor_name=agent, value='cocaine'), FactorValue(factor_name=intensity, value='medium'), FactorValue(factor_name=duration, value='long'))), Treatment(treatment_type=INTERVENTIONS['CHEMICAL'], factor_values=(FactorValue(factor_name=agent, value='cocaine'), FactorValue(factor_name=intensity, value='medium'), FactorValue(factor_name=duration, value='short'))), Treatment(treatment_type=INTERVENTIONS['CHEMICAL'], factor_values=(FactorValue(factor_name=agent, value='crack'), FactorValue(factor_name=intensity, value='high'), FactorValue(factor_name=duration, value='long'))), Treatment(treatment_type=INTERVENTIONS['CHEMICAL'], factor_values=(FactorValue(factor_name=agent, value='crack'), FactorValue(factor_name=intensity, value='high'), FactorValue(factor_name=duration, value='short'))), Treatment(treatment_type=INTERVENTIONS['CHEMICAL'], factor_values=(FactorValue(factor_name=agent, value='crack'), FactorValue(factor_name=intensity, value='low'), FactorValue(factor_name=duration, value='long'))), Treatment(treatment_type=INTERVENTIONS['CHEMICAL'], factor_values=(FactorValue(factor_name=agent, value='crack'), FactorValue(factor_name=intensity, value='low'), FactorValue(factor_name=duration, value='short'))), Treatment(treatment_type=INTERVENTIONS['CHEMICAL'], factor_values=(FactorValue(factor_name=agent, value='crack'), FactorValue(factor_name=intensity, value='medium'), FactorValue(factor_name=duration, value='long'))), Treatment(treatment_type=INTERVENTIONS['CHEMICAL'], factor_values=(FactorValue(factor_name=agent, value='crack'), FactorValue(factor_name=intensity, value='medium'), FactorValue(factor_name=duration, value='short'))), Treatment(treatment_type=INTERVENTIONS['CHEMICAL'], factor_values=(FactorValue(factor_name=agent, value='aether'), FactorValue(factor_name=intensity, value='high'), FactorValue(factor_name=duration, value='long'))), Treatment(treatment_type=INTERVENTIONS['CHEMICAL'], factor_values=(FactorValue(factor_name=agent, value='aether'), FactorValue(factor_name=intensity, value='high'), FactorValue(factor_name=duration, value='short'))), Treatment(treatment_type=INTERVENTIONS['CHEMICAL'], factor_values=(FactorValue(factor_name=agent, value='aether'), FactorValue(factor_name=intensity, value='low'), FactorValue(factor_name=duration, value='long'))), Treatment(treatment_type=INTERVENTIONS['CHEMICAL'], factor_values=(FactorValue(factor_name=agent, value='aether'), FactorValue(factor_name=intensity, value='low'), FactorValue(factor_name=duration, value='short'))), Treatment(treatment_type=INTERVENTIONS['CHEMICAL'], factor_values=(FactorValue(factor_name=agent, value='aether'), FactorValue(factor_name=intensity, value='medium'), FactorValue(factor_name=duration, value='long'))), Treatment(treatment_type=INTERVENTIONS['CHEMICAL'], factor_values=(FactorValue(factor_name=agent, value='aether'), FactorValue(factor_name=intensity, value='medium'), FactorValue(factor_name=duration, value='short'))) })