class AlphaDiversityDatum(mdb.EmbeddedDocument): """AlphaDiv datum type.""" metrics = mdb.ListField(mdb.StringField()) category_value = mdb.StringField(required=True) # metric -> distribution by_metric = mdb.MapField(field=mdb.ListField(mdb.FloatField()))
class AlphaDiversityResult(mdb.EmbeddedDocument): """Embedded results for alpha diversity.""" # Categories dict has form: {<category_name>: [<category_value>, ...]} categories = mdb.MapField(field=mdb.ListField(mdb.StringField()), required=True) tool_names = mdb.ListField(mdb.StringField()) by_tool = mdb.MapField(field=EmDoc(AlphaDiversityTool), required=True)
class HMPResult(mdb.EmbeddedDocument): # pylint: disable=too-few-public-methods """HMP document type.""" categories = mdb.MapField(field=StringList, required=True) sites = mdb.ListField(mdb.StringField(), required=True) data = mdb.MapField(field=EmDocList(HMPDatum), required=True) def clean(self): """Ensure integrity of result content.""" for category, values in self.categories.items(): if category not in self.data: msg = f'Category \'{category}\' is not present in \'data\'!' raise ValidationError(msg) values_present = [datum.name for datum in self.data[category]] for value in values: if value not in values_present: msg = f'Value \'{value}\' is not present in \'data\'!' raise ValidationError(msg) for category_name, category_data in self.data.items(): if len(category_data) != len(self.categories[category_name]): msg = (f'Category data for {category_name} does not match size of ' f'category values ({len(self.categories[category_name])})!') raise ValidationError(msg) for datum in category_data: if len(datum.data) != len(self.sites): msg = (f'Datum <{datum.name}> of size {len(datum.data)} ' f'does not match size of sites ({len(self.sites)})!') raise ValidationError(msg)
class XYZPoint(mdb.EmbeddedDocument): # pylint: disable=too-few-public-methods """Represent a 3d point.""" xval = mdb.FloatField(required=True) yval = mdb.FloatField(required=True) zval = mdb.FloatField(default=1) name = mdb.StringField()
class AnalysisResultWrapper(mongoDB.Document): # pylint: disable=too-few-public-methods """Base mongo result class.""" status = mongoDB.StringField(required=True, max_length=1, choices=ANALYSIS_RESULT_STATUS, default='P') data = mongoDB.GenericEmbeddedDocumentField()
class BaseSample(Document): """Sample model.""" uuid = mongoDB.UUIDField(required=True, primary_key=True, binary=False, default=uuid4) name = mongoDB.StringField(unique=True) metadata = mongoDB.DictField(default={}) analysis_result = mongoDB.LazyReferenceField(AnalysisResultMeta) theme = mongoDB.StringField(default='') created_at = mongoDB.DateTimeField(default=datetime.datetime.utcnow) meta = {'allow_inheritance': True} @property def tool_result_names(self): """Return a list of all tool results present for this Sample.""" all_fields = [mod.name() for mod in all_tool_results] return [ field for field in all_fields if getattr(self, field, None) is not None ] def fetch_safe(self, tools=None): """Return the sample with all tool result documents fetched and jsonified.""" if not tools: tools = self.tool_result_names safe_sample = { tool: jsonify(getattr(self, tool).fetch()) for tool in tools } safe_sample['name'] = self.name safe_sample['metadata'] = self.metadata safe_sample['theme'] = self.theme if self.analysis_result: safe_sample['analysis_result'] = str(self.analysis_result.pk) return safe_sample
# pylint: disable=too-few-public-methods """Average Genome Size display models.""" from app.extensions import mongoDB as mdb from app.display_modules.shared_models import DistributionResult # Define aliases EmbeddedDoc = mdb.EmbeddedDocumentField # pylint: disable=invalid-name StringList = mdb.ListField(mdb.StringField()) # pylint: disable=invalid-name class AGSResult(mdb.EmbeddedDocument): """AGS document type.""" # Categories dict has form: {<category_name>: [<category_value>, ...]} categories = mdb.MapField(field=StringList, required=True) # Distribution dict has form: {<category_name>: {<category_value>: <dist>}} distributions = mdb.MapField( field=mdb.MapField(field=EmbeddedDoc(DistributionResult)), required=True)
class HMPDatum(mdb.EmbeddedDocument): # pylint: disable=too-few-public-methods """HMP datum type.""" name = mdb.StringField(required=True) data = mdb.ListField(mdb.ListField(mdb.FloatField()), required=True)
class ToolDocument(mdb.EmbeddedDocument): # pylint: disable=too-few-public-methods """Tool document type.""" x_label = mdb.StringField(required=True) y_label = mdb.StringField(required=True)
class AlphaDiversityTool(mdb.EmbeddedDocument): """Store a map of rank -> AlphaDiversityRank.""" taxa_ranks = mdb.ListField(mdb.StringField()) by_taxa_rank = mdb.MapField(field=EmDoc(AlphaDiversityRank))