def trio_persons(family_id="trio_family"): return [ Person( **{ "family_id": family_id, "person_id": "mom", "sex": "F", "role": "mom", "status": 1 }), Person( **{ "family_id": family_id, "person_id": "dad", "sex": "M", "role": "dad", "status": 1 }), Person( **{ "family_id": family_id, "person_id": "p1", "sex": "M", "role": "prb", "status": 2 }), ]
def get_persons(self, roles=None, person_ids=None, family_ids=None): persons = self.rest_client.post_pheno_persons(self.dataset_id, roles, person_ids, family_ids) for k, v in persons.items(): persons[k] = Person(**v) return persons
def member7(): return Person( person_id="id3", family_id="fam3", mom_id="mom3", dad_id="0", sex="1", status="2", role="prb", )
def member5(): return Person( person_id="mom2", family_id="fam2", mom_id="0", dad_id="0", sex="2", status="1", role="mom", layout="1:50.0,50.0", generated=False, )
def member4(): return Person( person_id="id2", family_id="fam2", mom_id="mom2", dad_id="dad2", sex="1", status="2", role="prb", layout="2:100.0,75.0", generated=False, )
def member3(): return Person( person_id="dad1", family_id="fam1", mom_id="0", dad_id="0", sex="1", status="1", role="dad", layout="error", generated=True, )
def member2(): return Person( person_id="mom1", family_id="fam1", mom_id="0", dad_id="0", sex="2", status="1", role="mom", layout="error", generated=False, )
def _build_families(self): families = dict() families_details = self.rest_client.get_all_family_details( self._remote_study_id) for family in families_details: family_id = family["family_id"] person_jsons = family["members"] family_members = [] for person_json in person_jsons: family_members.append(Person(**person_json)) families[family_id] = Family.from_persons(family_members) self._families = FamiliesData.from_families(families)
def multiplex_persons(): persons = trio_persons("multiplex_family") persons.append( Person( **{ "family_id": "multiplex_family", "person_id": "s1", "sex": "M", "role": "sib", "status": 2 })) return persons
def multigenerational_persons(): persons = trio_persons("multigenerational_family") persons.append( Person( **{ "family_id": "multigenerational_family", "person_id": "grandparent", "sex": "M", "role": str(Role.maternal_grandfather), "status": 1 })) return persons
def quad_persons(): persons = trio_persons("quad_family") persons.append( Person( **{ "family_id": "quad_family", "person_id": "s1", "sex": "M", "role": "sib", "status": 1 })) return persons
def member6(): return Person( person_id="dad2", family_id="fam2", mom_id="0", dad_id="0", sex="1", status="1", role="dad", layout="1:50.0,100.0", generated=True, )
def test_family_type_multigenerational(role): persons = list(trio_persons("multigenerational")) persons.append( Person( **{ "family_id": "multigenerational", "person_id": "grandparent", "sex": "U", "role": str(role), "status": 1 })) family = Family.from_persons(persons) assert family.family_type is FamilyType.MULTIGENERATIONAL
def simplex_persons_2(): persons = trio_persons("simplex_family") persons[0]._status = 2 persons[0]._attributes["status"] = 2 persons.append( Person( **{ "family_id": "simplex_family", "person_id": "s1", "sex": "M", "role": "sib", "status": 1 })) return persons
def get_persons( self, roles: Optional[Iterable[Role]] = None, person_ids: Optional[Iterable[str]] = None, family_ids: Optional[Iterable[str]] = None) -> Dict[str, Person]: """Returns individuals data from phenotype database. `roles` -- specifies persons of which role should be returned. If not specified returns all individuals from phenotype database. `person_ids` -- list of person IDs to filter result. Only data for individuals with person_id in the list `person_ids` are returned. `family_ids` -- list of family IDs to filter result. Only data for individuals that are members of any of the specified `family_ids` are returned. Returns a dictionary of (`personId`, `Person()`) where the `Person` object is the same object used into `VariantDB` families. """ persons = {} df = self.get_persons_df( roles=roles, person_ids=person_ids, family_ids=family_ids) for row in df.to_dict("records"): person_id = row["person_id"] p = Person(**row) # p.person_id = person_id # p.family_id = family_id assert row["role"] in Role, "{} not a valid role".format( row["role"] ) assert row["sex"] in Sex, "{} not a valid sex".format(row["sex"]) assert row["status"] in Status, "{} not a valid status".format( row["status"] ) persons[person_id] = p return persons
def add_missing_members(family): new_members = [] id_to_individual = defaultdict(Individual) for member in family.full_members: individual = id_to_individual[member.person_id] individual.member = member missing_father_mothers = {} missing_mother_fathers = {} for member in family.full_members: if member.mom_id == member.dad_id: continue if member.mom_id is None: assert member.dad_id is not None if member.dad_id not in missing_mother_fathers: missing_mother_fathers[member.dad_id] = Person( person_id=member.dad_id + ".mother", family_id=family.family_id, mom_id="0", dad_id="0", sex="2", status="-", role=Role.unknown, generated=True, ) new_members.append(missing_mother_fathers[member.dad_id]) member.mom_id = member.dad_id + ".mother" elif member.dad_id is None: assert member.mom_id is not None if member.mom_id not in missing_father_mothers: missing_father_mothers[member.mom_id] = Person( person_id=member.mom_id + ".father", family_id=family.family_id, mom_id="0", dad_id="0", sex="1", status="-", role=Role.unknown, generated=True, ) new_members.append(missing_father_mothers[member.mom_id]) member.dad_id = member.mom_id + ".father" mother = id_to_individual[member.mom_id] father = id_to_individual[member.dad_id] if mother.member is None and mother not in new_members: mother.member = Person( person_id=member.mom_id, family_id=family.family_id, mom_id="0", dad_id="0", sex=Sex.F, status=Status.unspecified, role=Role.unknown, generated=True, ) new_members.append(mother.member) if father.member is None and father not in new_members: father.member = Person( person_id=member.dad_id, family_id=family.family_id, mom_id="0", dad_id="0", sex="1", status="-", role=Role.unknown, generated=True, ) new_members.append(father.member) unique_new_members_ids = set([]) unique_new_members = [] for person in new_members: if person.person_id in unique_new_members_ids: continue unique_new_members.append(person) unique_new_members_ids.add(person.person_id) family.add_members(unique_new_members)
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)