Example #1
0
def caseinfo_to_case(caseinfo: dict, sample_path: utils.URLPath) -> "Case":
    # required keys
    assert_in_dict(CASE_REQUIRED_FIELDS, caseinfo)
    case_id = caseinfo["id"]
    case_date = utils.str_to_date(caseinfo["date"])
    samples = [
        sample.sampleinfo_to_sample(sampleinfo, case_id, sample_path)
        for sampleinfo in caseinfo["filepaths"]
    ]

    # optional keys
    infiltration = caseinfo.get("infiltration", 0.0)
    infiltration = float(
        infiltration.replace(",", ".") if isinstance(infiltration, str
                                                     ) else infiltration)
    assert infiltration <= 100.0 and infiltration >= 0.0, "Infiltration out of range 0-100"
    group = caseinfo.get("cohort", "")
    diagnosis = caseinfo.get("diagnosis", "")
    sureness = caseinfo.get("sureness", "")

    case = Case(
        id=case_id,
        date=case_date,
        samples=samples,
        infiltration=infiltration,
        group=group,
        diagnosis=diagnosis,
        sureness=sureness,
    )
    return case
Example #2
0
def sampleinfo_to_sample(sample_info: dict, case_id: str,
                         dataset_path: utils.URLPath) -> "Sample":
    """Create a tube sample from sample info dict."""
    assert "fcs" in sample_info and "path" in sample_info[
        "fcs"], "Path to sample_info is missing"
    assert "date" in sample_info, "Date is missing"
    path = utils.URLPath(sample_info["fcs"]["path"])
    date = utils.str_to_date(sample_info["date"])

    tube = str(sample_info.get("tube", "0"))
    material = Material.from_str(sample_info.get("material", ""))
    panel = sample_info.get("panel", "")

    markers = sample_info["fcs"].get("markers", None)
    count = int(sample_info["fcs"].get("event_count", 0)) or None

    sample_id = f"{case_id}_t{tube}_{material.name}_{sample_info['date']}"

    sample = FCSSample(id=sample_id,
                       case_id=case_id,
                       path=path,
                       dataset_path=dataset_path,
                       date=date,
                       tube=tube,
                       material=material,
                       panel=panel,
                       markers=markers,
                       count=count)
    return sample
Example #3
0
    def do_ls(self, arg):
        "List members of groups or cases."
        if arg == "":
            group_counts = self.data.group_count
            for name, count in group_counts.items():
                print(f"{name}: {count} cases")
        else:
            queries, save = tokenize(arg)

            cases = list(self.data.data)
            for query in queries:
                if query["type"] == "g":
                    cases = [c for c in cases if c.group == query["query"]]
                elif query["type"] == "ig":
                    cases = [
                        c for c in cases
                        if c.infiltration > float(query["query"])
                    ]
                elif query["type"] == "il":
                    cases = [
                        c for c in cases
                        if c.infiltration < float(query["query"])
                    ]
                elif query["type"] == "s":
                    cases = [
                        c for c in cases
                        if c.infiltration == int(query["query"])
                    ]
                elif query["type"] == "p":
                    cases = [
                        c for c in cases
                        if len(c.filepaths) == int(query["query"])
                    ]
                elif query["type"] == "dg":
                    date_min = utils.str_to_date(query["query"])
                    cases = [c for c in cases if c.date >= date_min]
                elif query["type"] == "dl":
                    date_max = utils.str_to_date(query["query"])
                    cases = [c for c in cases if c.date <= date_max]
                else:
                    print("Invalid type ", query["type"])
            [info_case(c) for c in cases[:10]]
            print(f"Total {len(cases)}")
            if save:
                print(f"Saving labels to {save}")
                labels = [c.id for c in cases]
                utils.save_json(labels, save)
Example #4
0
def json_to_fcssample(samplejson: dict) -> "FCSSample":
    samplejson["date"] = utils.str_to_date(samplejson["date"])
    samplejson["path"] = utils.URLPath(samplejson["path"])
    if samplejson["material"]:
        samplejson["material"] = Material[samplejson["material"]]
    else:
        samplejson["material"] = None
    return FCSSample(**samplejson)
Example #5
0
def json_to_case(jscase: dict) -> "Case":
    if jscase["used_material"]:
        material = Material[jscase["used_material"]]
    else:
        material = None
    jscase["used_material"] = material
    jscase["date"] = utils.str_to_date(jscase["date"])

    return Case(**jscase)
Example #6
0
def json_to_somsample(samplejson: dict) -> "SOMSample":
    samplejson["date"] = utils.str_to_date(samplejson["date"])
    samplejson["path"] = utils.URLPath(samplejson["path"])
    samplejson["dims"] = tuple(samplejson["dims"])
    return SOMSample(**samplejson)
Example #7
0
    if labels and case.id not in labels:
        reasons.append(f"labels")

    if infiltration and case.group != "normal":
        infiltration_min, infiltration_max = infiltration
        if infiltration_min and case.infiltration < infiltration_min:
            reasons.append("infiltration_min")
        if infiltration_max and case.infiltration > infiltration_max:
            reasons.append("infiltration_max")

    if date:
        date_min, date_max = date
        if date_min:
            if isinstance(date_min, str):
                date_min = utils.str_to_date(date_min)

            if case.date < date_min:
                reasons.append("date_min")
        if date_max:
            if isinstance(date_max, str):
                date_max = utils.str_to_date(date_max)
            if case.date > date_max:
                reasons.append("date_max")

    if counts:
        assert tubes, "Tubes required for counts"
        if not any(case.get_tube(t, min_count=counts) for t in tubes):
            reasons.append("counts")

    if materials: