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 create_descriptor(): """Returns a ISA-Tab descriptor using a simple sample plan for illustration.""" investigation = Investigation(identifier='I1') plan = SampleAssayPlan() plan.add_sample_type('liver') plan.add_sample_plan_record('liver', 5) plan.add_sample_type('blood') plan.add_sample_plan_record('blood', 3) plan.group_size = 2 f1 = StudyFactor(name='AGENT', factor_type=OntologyAnnotation(term='pertubation agent')) f2 = StudyFactor(name='INTENSITY', factor_type=OntologyAnnotation(term='intensity')) f3 = StudyFactor(name='DURATION', factor_type=OntologyAnnotation(term='time')) treatment_factory = TreatmentFactory(factors=[f1, f2, f3]) treatment_factory.add_factor_value(f1, {'cocaine', 'crack', 'aether'}) treatment_factory.add_factor_value(f2, {'low', 'medium', 'high'}) treatment_factory.add_factor_value(f3, {'short', 'long'}) ffactorial_design_treatments = treatment_factory\ .compute_full_factorial_design() treatment_sequence = TreatmentSequence( ranked_treatments=ffactorial_design_treatments) # treatment_factory.add_factor_value('intensity', 1.05) study = IsaModelObjectFactory(plan, treatment_sequence)\ .create_study_from_plan() study.filename = 's_study.txt' investigation.studies = [study] print(isatab.dumps(investigation))
def test_create_study_from_plan(self): plan = SampleAssayPlan() plan.add_sample_type('liver') plan.add_sample_plan_record('liver', 5) plan.add_sample_type('blood') plan.add_sample_plan_record('blood', 3) plan.group_size = 2 treatment_factory = TreatmentFactory( factors=[self.f1, self.f2, self.f3]) treatment_factory.add_factor_value(self.f1, {'cocaine', 'crack', 'aether'}) treatment_factory.add_factor_value(self.f2, {'low', 'medium', 'high'}) treatment_factory.add_factor_value(self.f3, {'short', 'long'}) ffactorial_design_treatments = \ treatment_factory.compute_full_factorial_design() treatment_sequence = TreatmentSequence( ranked_treatments={(x, i) for i, x in enumerate( ffactorial_design_treatments)}) # makes each study group ranked in sequence study = IsaModelObjectFactory( plan, treatment_sequence).create_study_from_plan() study.filename = 's_study.txt' self.investigation.studies = [study] self.assertEqual(36, len(study.sources)) self.assertEqual(288, len(study.samples))
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 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 test_sequences_plan_property(self): other_test_sequence = TreatmentSequence( ranked_treatments=[(self.first_treatment, 2), (self.second_treatment, 1)]) other_sample_plan = SampleAssayPlan(group_size=12) sequences_plan = { self.test_sequence: self.sample_plan, other_test_sequence: other_sample_plan } self.design.sequences_plan = sequences_plan self.assertEqual(self.design.sequences_plan, sequences_plan)
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))
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_create_study_from_plan_with_qc(self): plan = SampleAssayPlan() plan.add_sample_type('liver') plan.add_sample_plan_record('liver', 5) plan.add_sample_type('blood') plan.add_sample_plan_record('blood', 3) plan.group_size = 2 plan.add_sample_type('solvent') plan.add_sample_qc_plan_record('solvent', 8) treatment_factory = TreatmentFactory( factors=[self.f1, self.f2, self.f3]) treatment_factory.add_factor_value(self.f1, {'cocaine', 'crack', 'aether'}) treatment_factory.add_factor_value(self.f2, {'low', 'medium', 'high'}) treatment_factory.add_factor_value(self.f3, {'short', 'long'}) ffactorial_design_treatments = \ treatment_factory.compute_full_factorial_design() treatment_sequence = TreatmentSequence( ranked_treatments={(x, i) for i, x in enumerate( ffactorial_design_treatments)}) # makes each study group ranked in sequence study = IsaModelObjectFactory( plan, treatment_sequence).create_study_from_plan() study.filename = 's_study.txt' # print(isatab.dumps(self.investigation)) self.investigation.studies = [study] # 36 sources, 36 QC sources self.assertEqual(72, len(study.sources)) self.assertEqual( 36, len([ x for x in study.sources if x.get_char('Material Type').value.term == 'solvent' ])) # 288 samples plus 36 QC samples self.assertEqual(324, len(study.samples))
def create_from_plan_parameters(galaxy_parameters_file, sample_assay_plans_file, study_info_file, treatment_plans_file, target_dir): decoder = SampleAssayPlanDecoder() if galaxy_parameters_file: galaxy_parameters = json.load(galaxy_parameters_file) sample_and_assay_plans, study_info, treatment_plan_params = \ map_galaxy_to_isa_create_json(galaxy_parameters) plan = decoder.load(io.StringIO(json.dumps(sample_and_assay_plans))) elif sample_assay_plans_file and study_info_file and treatment_plans_file: plan = decoder.load(sample_assay_plans_file) study_info = json.load(study_info_file) treatment_plan_params = json.load(treatment_plans_file) else: raise IOError('Wrong parameters provided') study_type = treatment_plan_params['study_type_cond']['study_type'] if study_type != 'intervention': raise NotImplementedError('Only supports Intervention studies') single_or_multiple = treatment_plan_params['study_type_cond'][ 'one_or_more']['single_or_multiple'] if single_or_multiple == 'multiple': raise NotImplementedError( 'Multiple treatments not yet implemented. Please select Single') intervention_type = treatment_plan_params['study_type_cond'][ 'one_or_more']['intervention_type']['select_intervention_type'] if intervention_type != 'chemical intervention': raise NotImplementedError( 'Only Chemical Interventions supported at this time') treatment_factory = TreatmentFactory( intervention_type=INTERVENTIONS['CHEMICAL'], factors=BASE_FACTORS) agent_levels = treatment_plan_params['study_type_cond']['one_or_more'][ 'intervention_type']['agent'].split(',') for agent_level in agent_levels: treatment_factory.add_factor_value(BASE_FACTORS[0], agent_level.strip()) dose_levels = treatment_plan_params['study_type_cond']['one_or_more'][ 'intervention_type']['intensity'].split(',') for dose_level in dose_levels: treatment_factory.add_factor_value(BASE_FACTORS[1], dose_level.strip()) duration_of_exposure_levels = treatment_plan_params['study_type_cond'][ 'one_or_more']['intervention_type']['duration'].split(',') for duration_of_exposure_level in duration_of_exposure_levels: treatment_factory.add_factor_value(BASE_FACTORS[2], duration_of_exposure_level.strip()) treatment_sequence = TreatmentSequence( ranked_treatments=treatment_factory.compute_full_factorial_design()) isa_object_factory = IsaModelObjectFactory(plan, treatment_sequence) s = isa_object_factory.create_assays_from_plan() contact = Person() contact.affiliation = study_info['study_pi_affiliation'] contact.last_name = study_info['study_pi_last_name'] contact.email = study_info['study_pi_email'] contact.first_name = study_info['study_pi_first_name'] s.contacts = [contact] s.description = study_info['study_description'] s.filename = 's_study.txt' s.title = 'ISA created {}'.format(datetime.datetime.now().isoformat()) s.identifier = 'ISA-{}'.format(uuid.uuid4().hex[:8]) i = Investigation() i.contacts = [contact] i.description = s.description i.title = s.title i.identifier = s.identifier i.studies = [s] isatab.dump(isa_obj=i, output_path=target_dir, i_file_name='i_investigation.txt') for assay in s.assays: for data_file in assay.data_files: data_file_path = os.path.join(target_dir, data_file.filename) with open(data_file_path, 'a'): os.utime(data_file_path, None)
class TreatmentSequenceTest(unittest.TestCase): 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 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 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_init_with_treatments_list_with_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'))), 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.assertTrue(isinstance(new_sequence.ranked_treatments, set)) self.assertTrue(treatments[0] in new_sequence.ranked_treatments) self.assertTrue(treatments[1] in new_sequence.ranked_treatments) def test_add_treatment_single(self): self.sequence.add_treatment(self.test_treatment) self.assertEqual(self.sequence.ranked_treatments, {(self.test_treatment, 1)}) 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_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)) def test_neq(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) ] other_treatments = [ (Treatment(treatment_type=INTERVENTIONS['CHEMICAL'], factor_values=(FactorValue(factor_name=self.agent, value='spirit'), 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='spirit'), FactorValue(factor_name=self.intensity, value='low'), FactorValue(factor_name=self.duration, value='long'))), 2) ] first_sequence = TreatmentSequence(treatments) second_sequence = TreatmentSequence(other_treatments) self.assertNotEqual(first_sequence, second_sequence) self.assertNotEqual(hash(first_sequence), hash(second_sequence)) 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_subject_count_setter(self): subject_count = 20 self.sequence.subject_count = subject_count self.assertTrue(self.sequence.subject_count, subject_count)