Exemple #1
0
    def _draw_family(self, axes, family):
        col_labels = [
            "familyId",
            "individualId",
            "father",
            "mother",
            "sex",
            "status",
            "role",
            "layout",
            "generated",
            "not_sequenced",
        ]
        table_vals = []

        for member in family:
            table_vals.append([
                member.family_id,
                member.person_id,
                member.dad_id,
                member.mom_id,
                Sex.from_name(member.sex),
                member.status,
                member.role,
                member.layout,
                "G" if member.generated else "",
                "N" if member.not_sequenced else "",
            ])

        axes.table = plt.table(cellText=table_vals,
                               colLabels=col_labels,
                               loc="center")
    def _format_criterias(cls, standard_criterias):
        """
        Replicates functionality from denovo gene set config parser.
        Given a TOML config's standard criterias, it does additional formatting
        which was done before in the parser.
        """

        effect_type_criterias = []
        for name, criteria in standard_criterias.effect_types.segments.items():
            effect_type_criterias.append({
                "property":
                "effect_types",
                "name":
                name,
                "value":
                expand_effect_types(criteria),
            })
        sex_criterias = []
        for name, criteria in standard_criterias.sexes.segments.items():
            sex_criterias.append({
                "property": "sexes",
                "name": name,
                "value": [Sex.from_name(criteria)],
            })
        return (effect_type_criterias, sex_criterias)
Exemple #3
0
def test_sex_attribute():

    assert Sex.from_value(1) == Sex.male
    assert Sex.from_name("M") == Sex.male
    assert Sex.from_name("male") == Sex.male

    assert Sex.from_value(2) == Sex.female
    assert Sex.from_name("F") == Sex.female
    assert Sex.from_name("female") == Sex.female

    assert Sex.from_value(0) == Sex.unspecified
    assert Sex.from_name("U") == Sex.unspecified
    assert Sex.from_name("unspecified") == Sex.unspecified
Exemple #4
0
def read_in_sexes(stream):
    length = int.from_bytes(stream.read(4), "big", signed=False)
    out = []
    for _i in range(0, length):
        is_not_none = read_int8(stream)
        if is_not_none:
            out.append(Sex(read_int8(stream)))
        else:
            out.append(None)
    return out
Exemple #5
0
    def redefine(self):
        assert "person_id" in self._attributes
        self.family_id = self._attributes["family_id"]
        self.family = None
        self.person_id = self._attributes["person_id"]
        self.sample_id = self._attributes.get("sample_id", None)
        self.index = self._attributes.get("index", None)

        self._sex = Sex.from_name(self._attributes["sex"])
        if "role" not in self._attributes:
            self._role = None
        else:
            self._role = Role.from_name(self._attributes.get("role"))

        self._status = Status.from_name(self._attributes["status"])

        self._attributes["sex"] = self._sex
        self._attributes["role"] = self._role
        self._attributes["status"] = self._status

        self.mom_id = self.get_attr("mom_id")
        if self.mom_id == "0":
            self.mom_id = None
            self._attributes["mom_id"] = None
        self.dad_id = self.get_attr("dad_id")
        if self.dad_id == "0":
            self.dad_id = None
            self._attributes["dad_id"] = None
        self.mom = None
        self.dad = None
        assert self.mom_id is None or type(self.mom_id) == str, \
            (self, self._attributes)
        assert self.dad_id is None or type(self.dad_id) == str, \
            (self, self._attributes)
        if self._attributes.get("not_sequenced"):
            value = self._attributes.get("not_sequenced")
            if value == "None" or value == "0" or value == "False":
                self._attributes["not_sequenced"] = None
        if self._attributes.get("generated"):
            value = self._attributes.get("generated")
            if value == "None" or value == "0" or value == "False":
                self._attributes["generated"] = None
Exemple #6
0
    def _draw_members(self, axes, layout):
        for level in layout.positions:
            for individual in level:
                if individual.individual.member.generated or \
                        individual.individual.member.not_sequenced:
                    individual_color = "grey"
                elif individual.individual.member.status == Status.unaffected:
                    individual_color = "white"
                elif individual.individual.member.status == Status.affected:
                    individual_color = "red"
                else:
                    individual_color = "purple"

                if Sex.from_name(individual.individual.member.sex) == Sex.male:
                    coords = [
                        individual.x + self._x_offset,
                        individual.y + self._y_offset,
                    ]
                    axes.add_patch(
                        mpatches.Rectangle(
                            coords,
                            individual.size,
                            individual.size,
                            facecolor=individual_color,
                            edgecolor="black",
                        ))

                    cx = coords[0] + individual.size / 2.0
                    cy = coords[1] + individual.size / 2.0

                    dlx = coords[0]
                    dly = coords[1]
                elif (Sex.from_name(
                        individual.individual.member.sex) == Sex.female):
                    coords = [
                        individual.x_center + self._x_offset,
                        individual.y_center + self._y_offset,
                    ]
                    axes.add_patch(
                        mpatches.Circle(
                            coords,
                            individual.size / 2,
                            facecolor=individual_color,
                            edgecolor="black",
                        ))

                    cx = coords[0]
                    cy = coords[1]

                    dlx = coords[0] + (individual.size / 2.0) * math.cos(
                        math.radians(225))
                    dly = coords[1] + (individual.size / 2.0) * math.sin(
                        math.radians(225))
                else:
                    size = math.sqrt((individual.size**2) / 2)
                    coords = [
                        individual.x + self._x_offset + (individual.size / 2),
                        individual.y + self._y_offset,
                    ]
                    axes.add_patch(
                        mpatches.Rectangle(
                            coords,
                            size,
                            size,
                            facecolor=individual_color,
                            edgecolor="black",
                            angle=45.0,
                        ))

                    cx = coords[0]
                    cy = coords[1] + individual.size / 2.0

                    dlx = coords[0] - individual.size / 4
                    dly = coords[1] + individual.size / 4

                if individual.individual.member.role == Role.prb:
                    axes.add_patch(
                        mpatches.FancyArrow(
                            dlx - self._gap,
                            dly - self._gap,
                            self._gap,
                            self._gap,
                            length_includes_head=True,
                            color="black",
                            head_width=2.0,
                            linewidth=0.1,
                        ))

                if self.show_id:
                    axes.annotate(
                        individual.individual.member.person_id,
                        (cx, cy),
                        color="black",
                        weight="bold",
                        fontsize=2,
                        ha="center",
                        va="center",
                    )
Exemple #7
0
    def load_simple_families_file(infile, ped_sep="\t"):
        fam_df = pd.read_csv(
            infile,
            sep=ped_sep,
            index_col=False,
            skipinitialspace=True,
            converters={
                "role": lambda r: Role.from_name(r),
                "gender": lambda s: Sex.from_name(s),
                "sex": lambda s: Sex.from_name(s),
            },
            dtype={
                "familyId": str,
                "personId": str
            },
            comment="#",
        )

        fam_df = fam_df.rename(columns={
            "gender": "sex",
            "personId": "person_id",
            "familyId": "family_id",
            "momId": "mom_id",
            "dadId": "dad_id",
            "sampleId": "sample_id",
        }, )

        fam_df["status"] = pd.Series(index=fam_df.index, data=1)
        fam_df.loc[fam_df.role == Role.prb, "status"] = 2
        fam_df["status"] = fam_df.status.apply(lambda s: Status.from_value(s))

        fam_df["mom_id"] = pd.Series(index=fam_df.index, data="0")
        fam_df["dad_id"] = pd.Series(index=fam_df.index, data="0")

        if "sample_id" not in fam_df.columns:
            sample_ids = pd.Series(data=fam_df["person_id"].values)
            fam_df["sample_id"] = sample_ids

        families = defaultdict(list)
        for rec in fam_df.to_dict(orient="records"):
            families[rec["family_id"]].append(rec)

        result = defaultdict(list)
        for fam_id, members in families.items():
            mom_id = None
            dad_id = None
            children = []
            for member in members:
                role = member["role"]
                if role == Role.mom:
                    mom_id = member["person_id"]
                elif role == Role.dad:
                    dad_id = member["person_id"]
                else:
                    assert role in set([Role.prb, Role.sib])
                    children.append(member)
            for child in children:
                child["mom_id"] = mom_id
                child["dad_id"] = dad_id

            result[fam_id] = [Person(**member) for member in members]

        return FamiliesData.from_family_persons(result)
Exemple #8
0
def sex_converter(a):
    if not isinstance(a, Sex):
        return Sex.from_name(a)
    return a
Exemple #9
0
def test_bad_sex_name():
    with pytest.raises(ValueError):
        Sex.from_name("gaga")
Exemple #10
0
def test_bad_sex_value():
    with pytest.raises(ValueError):
        Sex.from_value(100)