def test_result_types(self):
     """Ensure sample group model is created correctly."""
     ags_result = AnalysisResultWrapper(status='S',
                                        data=AGSFactory()).save()
     analysis_result = AnalysisResultMeta()
     setattr(analysis_result, MODULE_NAME, ags_result)
     analysis_result.save()
     self.assertEqual(len(analysis_result.result_types), 1)
     self.assertIn(MODULE_NAME, analysis_result.result_types)
Exemple #2
0
def create_abrf_analysis_result(save=False):
    """Generate ABRF analysis result."""
    sample_similarity = AnalysisResultWrapper(
        status='S', data=load_sample_similarity()).save()
    ags = AnalysisResultWrapper(status='S', data=load_ags()).save()
    analysis_result = AnalysisResultMeta(sample_similarity=sample_similarity,
                                         average_genome_size=ags)
    if save:
        analysis_result.save()
    return analysis_result
    def test_persist_result_helper(self):
        """Ensure persist_result_helper works as intended."""
        wrapper = AnalysisResultWrapper().save()
        analysis_result = AnalysisResultMeta(sample_similarity=wrapper).save()
        sample_similarity = create_mvp_sample_similarity()

        persist_result_helper(sample_similarity, analysis_result.uuid,
                              'sample_similarity')
        analysis_result.reload()
        self.assertIn('sample_similarity', analysis_result)
        wrapper = getattr(analysis_result, 'sample_similarity').fetch()
        self.assertIn('status', wrapper)
        self.assertEqual('S', wrapper.status)
def seed_db():
    """Seed the database."""
    bchrobot = User(username='******',
                    email='*****@*****.**',
                    password='******')
    dcdanko = User(username='******',
                   email='*****@*****.**',
                   password='******')
    cmason = User(username='******',
                  email='*****@*****.**',
                  password='******')


    abrf_analysis_result_01 = AnalysisResultMeta(reads_classified=reads_classified).save()
    abrf_sample_01 = Sample(name='SomethingUnique_A',
                            analysis_result=abrf_analysis_result_01).save()
    abrf_analysis_result_02 = AnalysisResultMeta(reads_classified=reads_classified).save()
    abrf_sample_02 = Sample(name='SomethingUnique_B',
                            analysis_result=abrf_analysis_result_02).save()
    abrf_analysis_result.save()

    abrf_uuid = UUID('00000000-0000-4000-8000-000000000000')
    abrf_description = 'ABRF San Diego Mar 24th-29th 2017'
    abrf_2017_group = SampleGroup(name='ABRF 2017',
                                  analysis_result=abrf_analysis_result,
                                  description=abrf_description)
    abrf_2017_group.id = abrf_uuid
    abrf_2017_group.samples = [abrf_sample_01, abrf_sample_02]

    uw_analysis_result.save()
    uw_sample = Sample(name='UW_Madison_00', analysis_result=uw_analysis_result).save()
    uw_group_result = AnalysisResultMeta().save()
    uw_madison_group = SampleGroup(name='The UW Madison Project',
                                   analysis_result=uw_group_result)
    uw_madison_group.samples = [uw_sample]

    fuzz_uuid = UUID('00000000-0000-4000-8000-000000000001')
    fuzz_group = create_saved_group(uuid=fuzz_uuid)

    mason_lab = Organization(name='Mason Lab', admin_email='*****@*****.**')
    mason_lab.users = [bchrobot, dcdanko, cmason]
    mason_lab.sample_groups = [abrf_2017_group, uw_madison_group, fuzz_group]

    db.session.add(mason_lab)
    db.session.commit()

    mason_lab.add_admin(bchrobot)
    mason_lab.add_admin(dcdanko)
    db.session.commit()
 def test_add_ags(self):
     """Ensure Average Genome Size model is created correctly."""
     ags = AGSResult(categories=CATEGORIES, distributions=DISTRIBUTIONS)
     wrapper = AnalysisResultWrapper(data=ags).save()
     result = AnalysisResultMeta(average_genome_size=wrapper).save()
     self.assertTrue(result.id)
     self.assertTrue(result.average_genome_size)
Exemple #6
0
def add_sample_group(resp):  # pylint: disable=unused-argument
    """Add sample group."""
    try:
        post_data = request.get_json()
        name = post_data['name']
    except TypeError:
        raise ParseError('Missing Sample Group creation payload.')
    except KeyError:
        raise ParseError('Invalid Sample Group creation payload.')

    sample_group = SampleGroup.query.filter_by(name=name).first()
    if sample_group is not None:
        raise InvalidRequest('Sample Group with that name already exists.')

    try:
        analysis_result = AnalysisResultMeta().save()
        sample_group = SampleGroup(name=name, analysis_result=analysis_result)
        db.session.add(sample_group)
        db.session.commit()
        result = sample_group_schema.dump(sample_group).data
        return result, 201
    except IntegrityError as integrity_error:
        current_app.logger.exception('Sample Group could not be created.')
        db.session.rollback()
        raise InternalError(str(integrity_error))
 def test_add_sample_similarity(self):
     """Ensure Sample Similarity model is created correctly."""
     sample_similarity_result = SampleSimilarityResult(
         categories=CATEGORIES, tools=TOOLS, data_records=DATA_RECORDS)
     wrapper = AnalysisResultWrapper(data=sample_similarity_result).save()
     result = AnalysisResultMeta(sample_similarity=wrapper).save()
     self.assertTrue(result.id)
     self.assertTrue(result.sample_similarity)
 def test_add_user_duplicate_name(self):
     """Ensure duplicate group names are not allowed."""
     add_sample_group('Sample Group One', access_scheme='public')
     duplicate_group = SampleGroup(
         name='Sample Group One',
         analysis_result=AnalysisResultMeta().save(),
         access_scheme='public',
     )
     db.session.add(duplicate_group)
     self.assertRaises(IntegrityError, db.session.commit)
Exemple #9
0
 def test_add_taxon_abundance(self):
     """Ensure Taxon Abundance model is created correctly."""
     taxon_abundance = TaxonAbundanceResult(
         **
         {'by_tool': {
             'kraken': flow_model(),
             'metaphlan2': flow_model()
         }})
     wrapper = AnalysisResultWrapper(data=taxon_abundance).save()
     result = AnalysisResultMeta(taxon_abundance=wrapper).save()
     self.assertTrue(result.id)
     self.assertTrue(result.taxon_abundance)
def add_sample_group(name,
                     analysis_result=None,
                     access_scheme='public',
                     created_at=datetime.datetime.utcnow()):
    """Wrap functionality for adding sample group."""
    if not analysis_result:
        analysis_result = AnalysisResultMeta().save()
    group = SampleGroup(name=name,
                        analysis_result=analysis_result,
                        access_scheme=access_scheme,
                        created_at=created_at)
    db.session.add(group)
    db.session.commit()
    return group
def add_sample(
        name,
        analysis_result=None,
        metadata={},  # pylint: disable=dangerous-default-value
        created_at=datetime.datetime.utcnow(),
        sample_kwargs={}):
    """Wrap functionality for adding sample."""
    if not analysis_result:
        analysis_result = AnalysisResultMeta().save()
    return Sample(name=name,
                  metadata=metadata,
                  analysis_result=analysis_result,
                  created_at=created_at,
                  **sample_kwargs).save()
 def test_get_pending_average_genome_size(self):  # pylint: disable=invalid-name
     """Ensure getting a pending AGS behaves correctly."""
     average_genome_size = AGSFactory()
     wrapper = AnalysisResultWrapper(data=average_genome_size).save()
     analysis_result = AnalysisResultMeta(
         average_genome_size=wrapper).save()
     with self.client:
         response = self.client.get(
             f'/api/v1/analysis_results/{analysis_result.uuid}/average_genome_size',
             content_type='application/json',
         )
         data = json.loads(response.data.decode())
         self.assertEqual(response.status_code, 200)
         self.assertIn('success', data['status'])
         self.assertEqual(data['data']['status'], 'P')
Exemple #13
0
 def generic_getter_test(self, data, endpt, verify_fields=('samples',)):
     """Check that we can get an analysis result."""
     wrapper = AnalysisResultWrapper(data=data, status='S').save()
     analysis_result = AnalysisResultMeta(**{endpt: wrapper}).save()
     with self.client:
         response = self.client.get(
             f'/api/v1/analysis_results/{analysis_result.uuid}/{endpt}',
             content_type='application/json',
         )
         data = json.loads(response.data.decode())
         self.assertEqual(response.status_code, 200)
         self.assertIn('success', data['status'])
         analysis_result = data['data']
         self.assertEqual(analysis_result['status'], 'S')
         for field in verify_fields:
             self.assertIn(field, analysis_result['data'])
 def test_get_ags(self):
     """Ensure getting a single AGS result works correctly."""
     average_genome_size = AGSFactory()
     wrapper = AnalysisResultWrapper(data=average_genome_size,
                                     status='S').save()
     analysis_result = AnalysisResultMeta(
         average_genome_size=wrapper).save()
     with self.client:
         response = self.client.get(
             f'/api/v1/analysis_results/{analysis_result.id}/average_genome_size',
             content_type='application/json',
         )
         self.assertEqual(response.status_code, 200)
         data = json.loads(response.data.decode())
         self.assertIn('success', data['status'])
         self.assertEqual(data['data']['status'], 'S')
         self.assertIn('data', data['data'])
         ags_result = data['data']['data']
         self.assertIn('categories', ags_result)
         self.assertIn('distributions', ags_result)
         self.assertTrue(len(ags_result['distributions']) > 0)
Exemple #15
0
def add_sample(resp):  # pylint: disable=unused-argument
    """Add sample."""
    try:
        post_data = request.get_json()
        sample_group_uuid = post_data['sample_group_uuid']
        sample_name = post_data['name']
    except TypeError:
        raise ParseError('Missing Sample creation payload.')
    except KeyError:
        raise ParseError('Invalid Sample creation payload.')

    try:
        sample_group = SampleGroup.query.filter_by(id=sample_group_uuid).one()
    except NoResultFound:
        raise InvalidRequest('Sample Group does not exist!')

    sample = Sample.objects(name=sample_name).first()
    if sample is not None:
        raise InvalidRequest('A Sample with that name already exists.')

    try:
        analysis_result = AnalysisResultMeta().save()
        sample = Sample(name=sample_name,
                        analysis_result=analysis_result,
                        metadata={
                            'name': sample_name
                        }).save()
        sample_group.sample_ids.append(sample.uuid)
        db.session.commit()
        result = sample_schema.dump(sample).data
        return result, 201
    except ValidationError as validation_error:
        current_app.logger.exception('Sample could not be created.')
        raise InternalError(str(validation_error))
    except IntegrityError as integrity_error:
        current_app.logger.exception(
            'Sample could not be added to Sample Group.')
        db.session.rollback()
        raise InternalError(str(integrity_error))
Exemple #16
0
def create_saved_group(owner, uuid=None):
    """Create and save a Sample Group with all the fixings (plus gravy)."""
    if uuid is None:
        uuid = uuid4()
    analysis_result = AnalysisResultMeta().save()
    group_description = 'Includes factory-produced analysis results from all display_modules'
    sample_group = SampleGroup(name='Fuzz Testing',
                               owner_uuid=owner.uuid,
                               owner_name=owner.username,
                               is_library=True,
                               analysis_result=analysis_result,
                               description=group_description)
    sample_group.uuid = uuid
    db.session.add(sample_group)
    db.session.commit()

    # Add the results
    analysis_result.average_genome_size = wrap_result(AGSFactory())
    analysis_result.sample_similarity = wrap_result(SampleSimilarityFactory())
    analysis_result.save()

    return sample_group
Exemple #17
0
# pylint: disable=invalid-name

"""MetaGenScope seed data from ARBF 2017."""

from app.analysis_results.analysis_result_models import AnalysisResultMeta, AnalysisResultWrapper

from .loader import (
    load_sample_similarity,
    load_taxon_abundance,
    load_reads_classified,
    load_hmp,
    load_ags,
)


sample_similarity = AnalysisResultWrapper(status='S', data=load_sample_similarity()).save()
taxon_abundance = AnalysisResultWrapper(status='S', data=load_taxon_abundance()).save()
hmp = AnalysisResultWrapper(status='S', data=load_hmp()).save()
ags = AnalysisResultWrapper(status='S', data=load_ags()).save()

abrf_analysis_result = AnalysisResultMeta(sample_similarity=sample_similarity,
                                          taxon_abundance=taxon_abundance,
                                          hmp=hmp,
                                          average_genome_size=ags)
def create_saved_group(uuid=None):
    """Create and save a Sample Group with all the fixings (plus gravy)."""
    if uuid is None:
        uuid = uuid4()
    analysis_result = AnalysisResultMeta().save()
    group_description = 'Includes factory-produced analysis results from all display_modules'
    sample_group = SampleGroup(name='Fuzz Testing',
                               analysis_result=analysis_result,
                               description=group_description)
    sample_group.id = uuid
    db.session.add(sample_group)
    db.session.commit()

    # Add the results
    analysis_result.average_genome_size = wrap_result(AGSFactory())
    analysis_result.card_amr_genes = wrap_result(CARDGenesFactory())
    analysis_result.functional_genes = wrap_result(FunctionalGenesFactory())
    analysis_result.hmp = wrap_result(HMPFactory())
    analysis_result.macrobe_abundance = wrap_result(MacrobeFactory())
    analysis_result.methyltransferases = wrap_result(MethylsFactory())
    analysis_result.microbe_directory = wrap_result(MicrobeDirectoryFactory())
    analysis_result.pathways = wrap_result(PathwayFactory())
    analysis_result.read_stats = wrap_result(ReadStatsFactory())
    analysis_result.reads_classified = wrap_result(ReadsClassifiedFactory())
    analysis_result.sample_similarity = wrap_result(
        create_mvp_sample_similarity())
    # analysis_result.taxon_abundance =
    analysis_result.virulence_factors = wrap_result(VFDBFactory())
    analysis_result.save()

    return sample_group
# pylint: disable=invalid-name
"""MetaGenScope seed data from UW Madison Project."""

from app.analysis_results.analysis_result_models import AnalysisResultMeta, AnalysisResultWrapper

from .loader import load_reads_classified

reads_classified = AnalysisResultWrapper(status='S',
                                         data=load_reads_classified()).save()

uw_analysis_result = AnalysisResultMeta(reads_classified=reads_classified)
Exemple #20
0
def drop_mongo_collections():
    """Drop all mongo collections."""
    AnalysisResultMeta.drop_collection()
    Sample.drop_collection()
    for tool_result in all_tool_results:
        tool_result.result_model().drop_collection()
Exemple #21
0
 def generic_adder_test(self, data, endpt):
     """Check that we can add an analysis result."""
     wrapper = AnalysisResultWrapper(data=data).save()
     result = AnalysisResultMeta(**{endpt: wrapper}).save()
     self.assertTrue(result.uuid)
     self.assertTrue(getattr(result, endpt))
 def test_sample_group_analysis_result(self):  # pylint: disable=invalid-name
     """Ensure sample group's analysis result can be accessed."""
     analysis_result = AnalysisResultMeta().save()
     sample_group = add_sample_group('Sample Group One',
                                     analysis_result=analysis_result)
     self.assertEqual(sample_group.analysis_result, analysis_result)