Пример #1
0
    def get_schema(self):
        schema_base = DataSchemas("COPO").get_ui_template().get("copo")
        x = data_utils.json_to_object(schema_base.get(self.component, dict()))

        return dict(schema_dict=schema_base.get(self.component, dict()).get("fields", list()),
                    schema=x.fields
                    )
Пример #2
0
    def get_schema(self):
        schema_base = DataSchemas("COPO").get_ui_template().get("copo")
        x = data_utils.json_to_object(schema_base.get(self.component, dict()))

        return dict(schema_dict=schema_base.get(self.component,
                                                dict()).get("fields", list()),
                    schema=x.fields)
Пример #3
0
    def generate_ui_schemas(self):
        """
        function generates ui schemas
        :return:
        """
        # instantiate data schema
        data_schema = DataSchemas("COPO")

        # generate core schemas
        temp_dict = DataFormats("COPO").generate_ui_template()

        # store schemas in DB
        if temp_dict["status"] == "success" and temp_dict["data"]:
            data_schema.add_ui_template(temp_dict["data"])

        return True
Пример #4
0
def get_fields_list(field_id):
    key_split = field_id.split(".")

    new_dict = DataSchemas(field_id.split(".")[0].upper()).get_ui_template()

    for kp in key_split[:-1]:
        if kp in new_dict:
            new_dict = new_dict[kp]

    return new_dict["fields"]
Пример #5
0
    def get_type_constraints(self, type_name):
        """
        given a type (or a subtype) function returns relevant schemas with associated constraints
        :param type_name:
        :return:
        """

        from dal.copo_base_da import DataSchemas
        schema_fields = DataSchemas("COPO").get_ui_template_node('cgCore')

        df = self.resolve_field_constraint(schema=schema_fields,
                                           type_name=type_name)
        return df
Пример #6
0
def resolve_ontology_term_data(data, elem):
    schema = DataSchemas("COPO").get_ui_template().get("copo").get("ontology_annotation").get("fields")

    resolved_data = list()

    for f in schema:
        if f.get("show_in_table", True):
            if f["id"].split(".")[-1] in data:
                resolved_data.append(data[f["id"].split(".")[-1]])

    if not resolved_data:
        resolved_data = str()
    elif len(resolved_data) == 1:
        resolved_data = resolved_data[0]
    return resolved_data
Пример #7
0
    def extract_repo_fields(self, datafile_id=str(), repo=str()):
        """
        given a datafile id, and repository type function returns a list of dictionaries of fields matching the repo
        :param datafile_id:
        :param repo:
        :return:
        """

        from dal.copo_da import DataFile, CGCore
        from dal.copo_base_da import DataSchemas

        if not repo:  # no repository to filter by
            return list()

        repo_type_option = lkup.DROP_DOWNS["REPO_TYPE_OPTIONS"]
        repo_type_option = [
            x for x in repo_type_option if x["value"].lower() == repo.lower()
        ]

        if not repo_type_option:
            return list()

        repo_type_option = repo_type_option[0]

        cg_schema = DataSchemas("COPO").get_ui_template_node('cgCore')

        # filter schema items by repo
        cg_schema = [
            x for x in cg_schema
            if x.get("target_repo", str()).strip() != str()
            and repo_type_option.get("abbreviation", str()) in
            [y.strip() for y in x.get("target_repo").split(',')]
        ]

        record = DataFile().get_record(datafile_id)
        description = record.get("description", dict())

        attributes = description.get("attributes", dict())
        stages = description.get("stages", list())

        schema_df = pd.DataFrame(cg_schema)
        schema_df.id = schema_df.id.str.lower().str.split(".").str[-1]
        schema_df.index = schema_df.id
        schema_df = schema_df[['ref', 'id', 'prefix']]
        schema_df = schema_df[~schema_df['ref'].isna()]

        # get all stage items
        all_items = [item for st in stages for item in st.get("items", list())]

        # filter stage items - stage items should conform to specifications of the repo
        schema_ids = list(schema_df.id)
        items = {
            item.get("id", str()).lower().split(".")[-1]: st.get("ref",
                                                                 "").lower()
            for st in stages for item in st.get("items", list())
            if item.get("id", str()).lower().split(".")[-1] in schema_ids
        }

        # ...also, account for any filtering performed by client agents (e.g., dependencies in COPO Wizard),
        # within the context of the target repo
        schema_df = schema_df[schema_df.index.isin(items.keys())]

        # obtain attributes for filtered stage items
        target_stages = list(set(items.values()))
        datafile_attributes = [
            v for k, v in attributes.items() if k in target_stages
        ]

        new_dict = dict()
        for d in datafile_attributes:
            new_dict.update(d)

        new_dict_series = pd.Series(new_dict)
        new_dict_series.index = new_dict_series.index.str.lower()
        schema_df['vals'] = new_dict_series
        schema_df['vals'] = schema_df['vals'].fillna('')

        schema_df = schema_df[['ref', 'id', 'vals', 'prefix']]

        # get composite attributes
        composite_attrib = [
            x for x in all_items if x["id"] in list(schema_df.id)
            and x.get("create_new_item", False)
        ]

        # expand composite attributes
        for cattrib in composite_attrib:
            comp_series = schema_df.loc[cattrib["id"]]
            schema_df = schema_df[~schema_df.id.isin([cattrib["id"]])]
            children_schemas = [
                x for x in cg_schema if x.get("dependency", str()).lower() ==
                comp_series.ref.lower()
            ]

            accessions = comp_series.vals
            if isinstance(accessions, str):
                accessions = accessions.split(",")

            object_ids = [ObjectId(x) for x in accessions if x.strip()]

            records = list()
            if len(object_ids):
                records = cursor_to_list(CGCore().get_collection_handle().find(
                    {"_id": {
                        "$in": object_ids
                    }}))

            attr_list = list()
            for child in children_schemas:
                child_dict = dict(ref=child["ref"],
                                  id=child["id"].split(".")[-1],
                                  prefix=child["prefix"],
                                  vals=[])
                attr_list.append(child_dict)
                for rec in records:
                    child_dict["vals"].append(rec.get(child_dict["id"], str()))

            if attr_list:
                attr_df = pd.DataFrame(attr_list)
                attr_df.index = attr_df.id
                schema_df = pd.concat([schema_df, attr_df])

        schema_df.rename(index=str,
                         columns={
                             "ref": "dc",
                             "id": "copo_id"
                         },
                         inplace=True)

        dc_list = schema_df.to_dict('records')

        return dc_list
Пример #8
0
def get_copo_schema(component, as_object=False):
    """
    function retrieves a required UI schema from the DB.
    :param component: a key in the schema_dict to be retrieved
    :param as_object: True returns the schema as an object whose element can be accessed using the '.' notation. False
            for the traditional python dictionary access
    :return:
    """
    from dal.copo_base_da import DataSchemas
    schema_base = DataSchemas("COPO").get_ui_template().get("copo")

    schema_dict = dict(
        publication=schema_base.get("publication").get("fields", list()),
        person=schema_base.get("person").get("fields", list()),
        datafile=schema_base.get("datafile").get("fields", list()),
        sample=schema_base.get("sample").get("fields", list()),
        source=schema_base.get("source").get("fields", list()),
        ontology_annotation=schema_base.get("ontology_annotation").get(
            "fields", list()),
        comment=schema_base.get("comment").get("fields", list()),
        material_attribute_value=schema_base.get(
            "material_attribute_value").get("fields", list()),
        duration=schema_base.get("duration").get("fields", list()),
        miappe_rooting_greenhouse=schema_base.get('miappe').get('rooting').get(
            'greenhouse').get("fields", list()),
        miappe_rooting_field=schema_base.get('miappe').get('rooting').get(
            'field').get("fields", list()),
        hydroponics=schema_base.get('miappe').get('nutrients').get(
            'hydroponics').get('fields', list()),
        soil=schema_base.get('miappe').get('nutrients').get('soil').get(
            'fields', list()),
        phenotypic_variables=schema_base.get("miappe").get(
            "phenotypic_variables").get("fields", list()),
        environment_variables=schema_base.get("miappe").get(
            "environment_variables").get("fields", list()),
        metadata_template=schema_base.get("metadata_template").get(
            "fields", list()))

    schema = schema_dict.get(component, list())

    if schema and as_object:
        schema = json_to_object(dict(fields=schema)).fields

    return schema
Пример #9
0
 def get_component_schema(self, **kwargs):
     return DataSchemas("COPO").get_ui_template_node(self.component)
Пример #10
0
    def get_ena_sequence_stages(self, next_stage_index):
        """
        stage callback function: resolves stages based on study type value
        :param next_stage_index:
        :return:
        """

        stage = dict()

        description = Description().GET(self.__wzh.description_token)

        stages = description["stages"]
        attributes = description["attributes"]

        if next_stage_index < len(stages):
            stage = stages[next_stage_index]

        study_type = attributes.get("study_type",
                                    dict()).get("study_type", str())

        if not study_type:
            # no study type specified, we can't really do anything but signal abort
            return dict()

        # re-validate dependency if necessary

        meta = description.get("meta", dict())
        study_type_old = meta.get(stage["ref"] + "_study_type", None)

        # remove stages dependent on 'study_type' - remove resolved stages preceding study_type
        if not study_type_old == study_type:
            cleared_stages = self.__wzh.remove_stage_dependency(
                next_stage_index)

            # get new dynamic stages based on user current choice
            new_stages = list()

            # get protocols
            protocols = ISAHelpers().get_protocols_parameter_values(study_type)

            # get study assay schema
            schema_fields = DataSchemas("COPO").get_ui_template_node(
                study_type)

            # get message dictionary
            message_dict = self.__wzh.wiz_message

            for pr in protocols:
                if len(pr.get("parameterValues", list())) > 0:

                    title = pr.get("name", str()).title()
                    ref = pr.get("name", str()).replace(" ", "_")
                    message = message_dict.get(ref + "_message",
                                               dict()).get("text", str())

                    stage_dict = dict(title=title,
                                      ref=ref,
                                      message=message,
                                      items=list())

                    for f in schema_fields:
                        if f['ref'] in pr.get("parameterValues", list()):
                            if f.get('show_in_form', False):
                                f["id"] = f['id'].strip(".").rsplit(".", 1)[1]
                                f["label"] = htags.trim_parameter_value_label(
                                    f["label"])

                                # convert select type controls to copo custom select
                                if f.get("control", str()) == "select":
                                    f["control"] = "copo-single-select"

                                stage_dict.get("items").append(f)

                    new_stages.append(stage_dict)

            # retain user choice for future reference
            meta[stage["ref"] + "_study_type"] = study_type

            # save meta
            Description().edit_description(self.__wzh.description_token,
                                           dict(meta=meta))

            if not new_stages:
                # no resolved stages; signal abort
                return dict()

            # resolve type and data source for generated stages
            self.__wzh.sanitise_stages(new_stages)

            # register dependency
            self.__wzh.set_stage_dependency(new_stages)

            # insert new stages to stage list
            stage_gap = next_stage_index + 1
            stages = cleared_stages[:stage_gap] + new_stages + cleared_stages[
                stage_gap:]

            # update description record
            Description().edit_description(self.__wzh.description_token,
                                           dict(stages=stages))

        return False