예제 #1
0
 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))
예제 #2
0
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))
예제 #3
0
    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))
예제 #4
0
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)
예제 #5
0
 def setUp(self):
     self.factory = TreatmentFactory()
예제 #6
0
class TreatmentFactoryTest(unittest.TestCase):
    def setUp(self):
        self.factory = TreatmentFactory()

    def test_init(self):
        self.assertEqual(self.factory.intervention_type,
                         INTERVENTIONS['CHEMICAL'])
        self.assertTrue(isinstance(self.factory.factors, OrderedDict))

    def test_add_factor_value_str(self):
        factor = StudyFactor(name=BASE_FACTORS[0]['name'],
                             factor_type=BASE_FACTORS[0]['type'])
        self.factory.add_factor_value(factor, 'agent_orange')
        self.assertEqual(self.factory.factors.get(factor), {'agent_orange'})

    def test_add_factor_value_number(self):
        factor = StudyFactor(name=BASE_FACTORS[1]['name'],
                             factor_type=BASE_FACTORS[1]['type'])
        self.factory.add_factor_value(factor, 1.05)
        self.assertEqual(self.factory.factors.get(factor), {1.05})

    def test_add_factor_value_list(self):
        values_to_add = ['agent_orange', 'crack, cocaine']
        factor = StudyFactor(name=BASE_FACTORS[0]['name'],
                             factor_type=BASE_FACTORS[0]['type'])
        self.factory.add_factor_value(factor, values_to_add)
        self.assertEqual(self.factory.factors.get(factor), set(values_to_add))

    def test_add_factor_value_set(self):
        values_to_add = {'agent_orange', 'crack, cocaine'}
        factor = StudyFactor(name=BASE_FACTORS[0]['name'],
                             factor_type=BASE_FACTORS[0]['type'])
        self.factory.add_factor_value(factor, values_to_add)
        self.assertEqual(self.factory.factors.get(factor), values_to_add)

    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')))
            })

    def test_compute_full_factorial_design_empty_agents(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, set())
        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, set())

    def test_compute_full_factorial_design_empty_intensities(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, set())
        self.factory.add_factor_value(duration, {'short', 'long'})

        full_factorial = self.factory.compute_full_factorial_design()
        self.assertEqual(full_factorial, set())