Пример #1
0
    def test_WildtypeFilter(self):
        f = data_query.WildtypeFilter()

        ov = data_model.ObservedValue(
            metadata=data_model.ObservedResultMetadata(genetics=data_model.Genetics(variation='')))
        self.assertEqual(f.score(None, ov), 1)

        ov = data_model.ObservedValue(
            metadata=data_model.ObservedResultMetadata(genetics=data_model.Genetics(variation='Delta gene-01')))
        self.assertEqual(f.score(None, ov), -1)
Пример #2
0
 def test_filter_observed_results(self):
     gen = self.ConcreteDataQueryGenerator()
     gen.filters = [
         data_query.TemperatureRangeFilter(min=36., max=38.),
     ]
     observed_results = [
         data_model.ObservedValue(metadata=data_model.ObservedResultMetadata(environment=data_model.Environment(temperature=36.5))),
         data_model.ObservedValue(metadata=data_model.ObservedResultMetadata(environment=data_model.Environment(temperature=35.0))),
         data_model.ObservedValue(metadata=data_model.ObservedResultMetadata(environment=data_model.Environment(temperature=37.0))),
     ]
     result = gen.filter_observed_results(None, observed_results)
     self.assertEqual(set(result.observed_results), set([observed_results[0], observed_results[2]]))
Пример #3
0
    def test_score(self):
        ov = [
            data_model.ObservedValue(value=1, metadata=data_model.ObservedResultMetadata(
                environment=data_model.Environment(temperature=37, ph=7))),
            data_model.ObservedValue(value=1, metadata=data_model.ObservedResultMetadata(
                environment=data_model.Environment(temperature=37, ph=6))),
            data_model.ObservedValue(value=1, metadata=data_model.ObservedResultMetadata(
                environment=data_model.Environment(temperature=36, ph=7))),
            data_model.ObservedValue(value=1, metadata=data_model.ObservedResultMetadata(
                environment=data_model.Environment(temperature=36, ph=6))),
            data_model.ObservedValue(value=2, metadata=data_model.ObservedResultMetadata(
                environment=data_model.Environment(temperature=37, ph=7))),
            data_model.ObservedValue(value=2, metadata=data_model.ObservedResultMetadata(
                environment=data_model.Environment(temperature=37, ph=6))),
            data_model.ObservedValue(value=2, metadata=data_model.ObservedResultMetadata(
                environment=data_model.Environment(temperature=36, ph=7))),
            data_model.ObservedValue(value=2, metadata=data_model.ObservedResultMetadata(
                environment=data_model.Environment(temperature=36, ph=6))),
        ]
        f = [
            data_query.TemperatureRangeFilter(min=36.5, max=37.5),
            data_query.PhRangeFilter(min=6.5, max=7.5),
            data_query.TemperatureNormalFilter(mean=37., std=1),
        ]

        runner = data_query.FilterRunner(f[0])
        self.assertEqual(list(runner.score(None, ov).ravel()), [1., 1., -1., -1., 1., 1., -1., -1.])

        runner = data_query.FilterRunner(f[1])
        self.assertEqual(list(runner.score(None, ov).ravel()), [1., -1., 1., -1., 1., -1., 1., -1.])

        runner = data_query.FilterRunner(f[2])
        s = 2 * scipy.stats.norm.cdf(-1)
        numpy.testing.assert_almost_equal(list(runner.score(None, ov).ravel()), [1., 1., s, s, 1., 1., s, s], decimal=5)
Пример #4
0
    def test_TaxonomicDistanceFilter(self):
        # example 1
        f = data_query.TaxonomicDistanceFilter('Mycoplasma pneumoniae M129')
        self.assertEqual(f.max, 8.)

        ov = data_model.ObservedValue(
            metadata=data_model.ObservedResultMetadata(genetics=data_model.Genetics(taxon='Mycoplasma pneumoniae M129')))
        self.assertEqual(f.score(None, ov), 1.)

        ov = data_model.ObservedValue(
            metadata=data_model.ObservedResultMetadata(genetics=data_model.Genetics(taxon='Mycoplasma pneumoniae')))
        self.assertEqual(f.scale, 8./5)
        self.assertEqual(f.score(None, ov), math.exp(-1/f.scale))

        ov = data_model.ObservedValue(
            metadata=data_model.ObservedResultMetadata(genetics=data_model.Genetics(taxon='Escherichia coli')))
        self.assertEqual(f.score(None, ov), math.exp(-5))

        # example 2
        f = data_query.TaxonomicDistanceFilter('Mycoplasma pneumoniae M129', max=5)
        self.assertEqual(f.max, 5.)

        ov = data_model.ObservedValue(
            metadata=data_model.ObservedResultMetadata(genetics=data_model.Genetics(taxon='Mycoplasma pneumoniae M129')))
        self.assertEqual(f.score(None, ov), 1.)

        ov = data_model.ObservedValue(
            metadata=data_model.ObservedResultMetadata(genetics=data_model.Genetics(taxon='Mycoplasma pneumoniae')))
        self.assertEqual(f.score(None, ov), math.exp(-1/f.scale))

        ov = data_model.ObservedValue(
            metadata=data_model.ObservedResultMetadata(genetics=data_model.Genetics(taxon='Escherichia coli')))
        self.assertEqual(f.score(None, ov), -1)

        # example 3
        f = data_query.TaxonomicDistanceFilter('Mycoplasma genitalium')
        self.assertEqual(f.max, 7.)

        ov = data_model.ObservedValue(
            metadata=data_model.ObservedResultMetadata(genetics=data_model.Genetics(taxon='Mycoplasma genitalium G37')))
        self.assertEqual(f.score(None, ov), 1.)

        ov = data_model.ObservedValue(
            metadata=data_model.ObservedResultMetadata(genetics=data_model.Genetics(taxon='Mycoplasma genitalium')))
        self.assertEqual(f.score(None, ov), 1)

        ov = data_model.ObservedValue(
            metadata=data_model.ObservedResultMetadata(genetics=data_model.Genetics(taxon='Mycoplasma')))
        self.assertEqual(f.score(None, ov), math.exp(-1/f.scale))
Пример #5
0
    def test_run(self):
        ov = [
            data_model.ObservedValue(value=1, metadata=data_model.ObservedResultMetadata(
                environment=data_model.Environment(temperature=37, ph=7))),
            data_model.ObservedValue(value=1, metadata=data_model.ObservedResultMetadata(
                environment=data_model.Environment(temperature=37, ph=6))),
            data_model.ObservedValue(value=1, metadata=data_model.ObservedResultMetadata(
                environment=data_model.Environment(temperature=36, ph=7))),
            data_model.ObservedValue(value=1, metadata=data_model.ObservedResultMetadata(
                environment=data_model.Environment(temperature=36, ph=6))),
            data_model.ObservedValue(value=2, metadata=data_model.ObservedResultMetadata(
                environment=data_model.Environment(temperature=37.1, ph=7))),
            data_model.ObservedValue(value=2, metadata=data_model.ObservedResultMetadata(
                environment=data_model.Environment(temperature=37.1, ph=6))),
            data_model.ObservedValue(value=2, metadata=data_model.ObservedResultMetadata(
                environment=data_model.Environment(temperature=37.01, ph=7))),
            data_model.ObservedValue(value=2, metadata=data_model.ObservedResultMetadata(
                environment=data_model.Environment(temperature=37.01, ph=6))),
        ]
        f = [
            data_query.TemperatureNormalFilter(mean=37, std=1),
            data_query.PhRangeFilter(min=6.5, max=7.5),
        ]
        runner = data_query.FilterRunner(f)

        # return_info=True
        result = runner.run(None, ov, return_info=True)
        self.assertEqual(result.all_observed_results, ov)
        s1 = 2 * scipy.stats.norm.cdf(-1)
        s01 = 2 * scipy.stats.norm.cdf(-0.1)
        s001 = 2 * scipy.stats.norm.cdf(-0.01)
        numpy.testing.assert_almost_equal(result.all_scores, numpy.array([[1., 1., s1, s1, s01, s01, s001, s001],
                                                                          [1., -1., 1., -1., 1., -1., 1., -1.]], ndmin=2).transpose())

        self.assertEqual(result.observed_results, [ov[0], ov[6], ov[4], ov[2]])
        numpy.testing.assert_almost_equal(result.scores, numpy.array([[1., s001, s01, s1], [1., 1., 1., 1.]], ndmin=2).transpose())
        self.assertEqual(result.observed_value_indices, [0, 6, 4, 2])

        # return_info=False
        ordered_observed_results = runner.run(None, ov, return_info=False)
        self.assertEqual(ordered_observed_results, [ov[0], ov[6], ov[4], ov[2]])
Пример #6
0
    def test_order(self):
        ov = [
            data_model.ObservedValue(value=1, metadata=data_model.ObservedResultMetadata(
                environment=data_model.Environment(temperature=37, ph=7))),
            data_model.ObservedValue(value=1, metadata=data_model.ObservedResultMetadata(
                environment=data_model.Environment(temperature=37, ph=6))),
            data_model.ObservedValue(value=1, metadata=data_model.ObservedResultMetadata(
                environment=data_model.Environment(temperature=36, ph=7))),
            data_model.ObservedValue(value=1, metadata=data_model.ObservedResultMetadata(
                environment=data_model.Environment(temperature=36, ph=6))),
            data_model.ObservedValue(value=2, metadata=data_model.ObservedResultMetadata(
                environment=data_model.Environment(temperature=37, ph=7))),
            data_model.ObservedValue(value=2, metadata=data_model.ObservedResultMetadata(
                environment=data_model.Environment(temperature=37, ph=6))),
            data_model.ObservedValue(value=2, metadata=data_model.ObservedResultMetadata(
                environment=data_model.Environment(temperature=36, ph=7))),
            data_model.ObservedValue(value=2, metadata=data_model.ObservedResultMetadata(
                environment=data_model.Environment(temperature=36, ph=6))),
        ]

        # one filter
        s = numpy.array([0.9, -1, 0.5, 1, -1.1, 0, -1.3, 0.3], ndmin=2).transpose()
        runner = data_query.FilterRunner([])
        order_o, order_s, i_order_o = runner.order(ov, s)

        self.assertEqual(order_o, [ov[3], ov[0], ov[2], ov[7], ov[5], ov[1], ov[4], ov[6]])
        numpy.testing.assert_equal(order_s, numpy.array([1, 0.9, 0.5, 0.3, 0, -1, -1.1, -1.3], ndmin=2).transpose())
        self.assertEqual(i_order_o, [3, 0, 2, 7, 5, 1, 4, 6])

        # multiple filters
        s = numpy.array([[0.9, -1, 0.5, 1, -1.1, 0, -1.3, 0.3], [0, 0, 0, 0, 0, 0, 0, 0]], ndmin=2).transpose()
        runner = data_query.FilterRunner([])
        order_o, order_s, i_order_o = runner.order(ov, s)

        self.assertEqual(order_o, [ov[3], ov[0], ov[2], ov[7], ov[5], ov[1], ov[4], ov[6]])
        numpy.testing.assert_equal(order_s, numpy.array(
            [[1, 0.9, 0.5, 0.3, 0, -1, -1.1, -1.3], [0, 0, 0, 0, 0, 0, 0, 0]], ndmin=2).transpose())
        self.assertEqual(i_order_o, [3, 0, 2, 7, 5, 1, 4, 6])
Пример #7
0
    def test_filter(self):
        ov = [
            data_model.ObservedValue(value=1, metadata=data_model.ObservedResultMetadata(
                environment=data_model.Environment(temperature=37, ph=7))),
            data_model.ObservedValue(value=1, metadata=data_model.ObservedResultMetadata(
                environment=data_model.Environment(temperature=37, ph=6))),
            data_model.ObservedValue(value=1, metadata=data_model.ObservedResultMetadata(
                environment=data_model.Environment(temperature=36, ph=7))),
            data_model.ObservedValue(value=1, metadata=data_model.ObservedResultMetadata(
                environment=data_model.Environment(temperature=36, ph=6))),
            data_model.ObservedValue(value=2, metadata=data_model.ObservedResultMetadata(
                environment=data_model.Environment(temperature=37, ph=7))),
            data_model.ObservedValue(value=2, metadata=data_model.ObservedResultMetadata(
                environment=data_model.Environment(temperature=37, ph=6))),
            data_model.ObservedValue(value=2, metadata=data_model.ObservedResultMetadata(
                environment=data_model.Environment(temperature=36, ph=7))),
            data_model.ObservedValue(value=2, metadata=data_model.ObservedResultMetadata(
                environment=data_model.Environment(temperature=36, ph=6))),
        ]

        # one filter
        s = numpy.array([1, -1, 0.5, 1, -1, 0, -1, 0.3], ndmin=2).transpose()
        runner = data_query.FilterRunner([])
        filt_o, filt_s, i_filt_o = runner.filter(ov, s)

        self.assertEqual(filt_o, [ov[0], ov[2], ov[3], ov[5], ov[7]])
        numpy.testing.assert_equal(filt_s, numpy.array([1, 0.5, 1, 0, 0.3], ndmin=2).transpose())
        self.assertEqual(i_filt_o, [0, 2, 3, 5, 7])

        # multiple filters
        s = numpy.array([[1, -1, 0.5, 1, -1, 0, -1, 0.3], [0, 0, 0, 0, 0, 0, 0, -1]], ndmin=2).transpose()
        runner = data_query.FilterRunner([])
        filt_o, filt_s, i_filt_o = runner.filter(ov, s)

        self.assertEqual(filt_o, [ov[0], ov[2], ov[3], ov[5]])
        numpy.testing.assert_equal(filt_s, numpy.array([[1, 0.5, 1, 0], [0, 0, 0, 0]], ndmin=2).transpose())
        self.assertEqual(i_filt_o, [0, 2, 3, 5])
Пример #8
0
    def test_OptionsFilter(self):
        f = data_query.OptionsFilter(('metadata', 'genetics', 'variation', ), [''])
        ov = data_model.ObservedValue(
            metadata=data_model.ObservedResultMetadata(genetics=data_model.Genetics(variation='')))
        self.assertEqual(f.score(None, ov), 1)
        ov = data_model.ObservedValue(
            metadata=data_model.ObservedResultMetadata(genetics=data_model.Genetics(variation='wildtype')))
        self.assertEqual(f.score(None, ov), -1)
        ov = data_model.ObservedValue(
            metadata=data_model.ObservedResultMetadata(genetics=data_model.Genetics(variation='Delta gene-01')))
        self.assertEqual(f.score(None, ov), -1)

        f = data_query.OptionsFilter(('metadata', 'genetics', 'variation', ), ['wildtype'])
        ov = data_model.ObservedValue(
            metadata=data_model.ObservedResultMetadata(genetics=data_model.Genetics(variation='')))
        self.assertEqual(f.score(None, ov), -1)
        ov = data_model.ObservedValue(
            metadata=data_model.ObservedResultMetadata(genetics=data_model.Genetics(variation='wildtype')))
        self.assertEqual(f.score(None, ov), 1)
        ov = data_model.ObservedValue(
            metadata=data_model.ObservedResultMetadata(genetics=data_model.Genetics(variation='Delta gene-01')))
        self.assertEqual(f.score(None, ov), -1)
Пример #9
0
    def metadata_dump(self, component):
        """ Calculate a consensus statistical representation of the one or more observed values

        Args:
            component (:obj:`models.Observation`): model component dump data for

        Returns:
            :obj:`list` of :obj:`data_model.ObservedResultMetadata`: data model metadata object
        """
        genetics = None
        environment = None
        cross_references = []
        method = None
        synonym = []
        meta = component._metadata

        taxon = meta.taxon[0].name if meta.taxon else None
        variation = meta.cell_line[0].name if meta.cell_line else None
        genetics = data_model.Genetics(taxon=taxon, variation=variation)

        temperature = meta.conditions[
            0].temperature if meta.conditions else None
        ph = meta.conditions[0].ph if meta.conditions else None
        media = meta.conditions[0].media if meta.conditions else None
        growth_status = meta.conditions[
            0].growth_status if meta.conditions else None
        growth_system = meta.conditions[
            0].growth_system if meta.conditions else None
        environment = data_model.Environment(temperature=temperature,
                                             ph=ph,
                                             media=media,
                                             growth_status=growth_status,
                                             growth_system=growth_system)

        name = meta.method[0].name if meta.method else None
        description = meta.method[0].comments if meta.method else None
        performer = meta.method[0].performer if meta.method else None
        hardware = meta.method[0].hardware if meta.method else None
        software = meta.method[0].software if meta.method else None
        method = data_model.Method(name=name,
                                   description=description,
                                   performer=performer,
                                   hardware=hardware,
                                   software=software)

        if meta.resource:
            for item in meta.resource:
                cross_references.append(
                    data_model.Resource(namespace=item.namespace, id=item._id))
        else:
            cross_references.append(
                data_model.Resource(namespace=None, id=None))

        if meta.synonym:
            for item in meta.synonym:
                synonym.append(data_model.Synonym(name=item.name))
        else:
            synonym.append(data_model.Synonym(name=None))

        metadata_result = data_model.ObservedResultMetadata(
            genetics=genetics,
            environment=environment,
            cross_references=cross_references,
            method=method,
            synonym=synonym)

        return metadata_result
Пример #10
0
 def obs_val(temperature):
     return data_model.ObservedValue(
         metadata=data_model.ObservedResultMetadata(environment=data_model.Environment(temperature=temperature)))
Пример #11
0
 def obs_val(ph):
     return data_model.ObservedValue(
         metadata=data_model.ObservedResultMetadata(environment=data_model.Environment(ph=ph)))
Пример #12
0
    def test_ObservedResultMetadata(self):
        o = data_model.ObservedResultMetadata()
        o.genetics = data_model.Genetics(taxon='Mycoplasma pneumoniae',
                                         variation='ΔMPN001')
        o.environment = data_model.Environment(temperature=37,
                                               ph=7.,
                                               media='Hayflick')
        o.reference = data_model.Reference(title='title',
                                           author='author',
                                           year=2017,
                                           volume=1,
                                           number=1,
                                           pages='1-10')
        o.method = data_model.ExperimentalMethod(
            name='assay', description='description of assay')

        observable = data_model.Observable(
            interaction=data_model.Reaction(id='AtpSynthase'),
            specie=data_model.Specie(id='ATP'),
            compartment=data_model.Compartment(id='c'),
            property='K_m',
        )

        ov = data_model.ObservedValue(
            observable=observable,
            value=1.0,
            error=0.5,
            units='U/mg',
        )

        o.observed_result.append(ov)

        o.validate()

        self.assertEqual(o.observed_result, [ov])
        self.assertEqual(ov.metadata, o)
        self.assertEqual(ov.observable.interaction.id, 'AtpSynthase')
        self.assertEqual(ov.observable.specie.id, 'ATP')
        self.assertEqual(ov.observable.compartment.id, 'c')
        self.assertEqual(ov.observable.property, 'K_m')

        self.assertEqual(ov.value, 1.0)
        self.assertEqual(ov.error, 0.5)
        self.assertEqual(ov.units, 'U/mg')

        self.assertEqual(o.genetics.taxon, 'Mycoplasma pneumoniae')
        self.assertEqual(o.genetics.variation, 'ΔMPN001')

        self.assertEqual(o.environment.temperature, 37.)
        self.assertEqual(o.environment.ph, 7.)
        self.assertEqual(o.environment.media, 'Hayflick')

        self.assertEqual(o.reference.title, 'title')
        self.assertEqual(o.reference.author, 'author')
        self.assertEqual(o.reference.year, 2017)
        self.assertEqual(o.reference.volume, 1)
        self.assertEqual(o.reference.number, 1)
        self.assertEqual(o.reference.pages, '1-10')

        self.assertEqual(o.method.name, 'assay')
        self.assertEqual(o.method.description, 'description of assay')