-
Notifications
You must be signed in to change notification settings - Fork 189
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
427e0be
commit 2c64fe9
Showing
1 changed file
with
78 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,78 @@ | ||
""" | ||
Pedigree file manipulation. | ||
""" | ||
|
||
import sys | ||
|
||
from dataclasses import dataclass | ||
from typing import Optional | ||
|
||
from ..apps.base import OptionParser, ActionDispatcher | ||
from ..formats.base import BaseFile | ||
|
||
|
||
@dataclass | ||
class Sample: | ||
""" | ||
A sample in the pedigree file. | ||
""" | ||
|
||
name: str | ||
dad: Optional[str] | ||
mom: Optional[str] | ||
|
||
def is_terminal(self) -> bool: | ||
""" | ||
Return True if the sample is terminal. | ||
""" | ||
return self.dad is None and self.mom is None | ||
|
||
|
||
class Pedigree(BaseFile): | ||
""" | ||
Read a pedigree file and store the information. | ||
""" | ||
|
||
def __init__(self, pedfile: str): | ||
super().__init__(pedfile) | ||
self.samples = {} | ||
with open(self.filename, encoding="utf-8") as fp: | ||
for row in fp: | ||
row = row.strip() | ||
if row[0] == "#": # header | ||
continue | ||
if not row: | ||
continue | ||
atoms = row.split() | ||
_, name, dad, mom = atoms[:4] | ||
dad = dad if dad != "0" else None | ||
mom = mom if mom != "0" else None | ||
sample = Sample(name, dad, mom) | ||
self.samples[sample.name] = sample | ||
|
||
|
||
def inbreeding(args): | ||
""" | ||
%prog inbreeding pedfile | ||
Calculate inbreeding coefficients from a pedigree file. | ||
""" | ||
p = OptionParser(inbreeding.__doc__) | ||
_, args = p.parse_args(args) | ||
|
||
if len(args) != 1: | ||
sys.exit(not p.print_help()) | ||
|
||
(pedfile,) = args | ||
ped = Pedigree(pedfile) | ||
print(ped.samples) | ||
|
||
|
||
def main(): | ||
actions = (("inbreeding", "calculate inbreeding coefficients"),) | ||
p = ActionDispatcher(actions) | ||
p.dispatch(globals()) | ||
|
||
|
||
if __name__ == "__main__": | ||
main() |