Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add support to detect plink generated 23andme files #176

Merged
merged 1 commit into from
Aug 21, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
86 changes: 86 additions & 0 deletions src/snps/io/reader.py
Original file line number Diff line number Diff line change
Expand Up @@ -165,6 +165,8 @@ def read(self):
d = self.read_gsa(file, compression, comments)
elif "Circle" in first_line:
d = self.read_circledna(file, compression)
elif "# Below is a text version of your data." in comments:
d = self.read_plink(file, compression)

# detect build from comments if build was not already detected from `read` method
if not d["build"]:
Expand Down Expand Up @@ -1133,6 +1135,90 @@ def parser():

return self.read_helper("CircleDNA", parser)

def read_plink(self, file, compression):
"""Read and parse plink file.

Parameters
----------
file : str
path to file

Returns
-------
dict
result of `read_helper`
"""

def parser():
columnnames = ["rsid", "chrom", "pos", "genotype"]
df = pd.read_csv(
file,
comment="#",
sep="\t",
na_values=["--", "-"],
names=columnnames,
compression=compression,
)
# turn number numbers into string numbers
df["chrom"] = df["chrom"].map(
{
"1": "1",
"2": "2",
"3": "3",
"4": "4",
"5": "5",
"6": "6",
"7": "7",
"8": "8",
"9": "9",
"10": "10",
"11": "11",
"12": "12",
"13": "13",
"14": "14",
"15": "15",
"16": "16",
"17": "17",
"18": "18",
"19": "19",
"20": "20",
"21": "21",
"22": "22",
1: "1",
2: "2",
3: "3",
4: "4",
5: "5",
6: "6",
7: "7",
8: "8",
9: "9",
10: "10",
11: "11",
12: "12",
13: "13",
14: "14",
15: "15",
16: "16",
17: "17",
18: "18",
19: "19",
20: "20",
21: "21",
22: "22",
"X": "X",
"Y": "Y",
"XY": "Y",
"MT": "MT",
}
)
df = df.dropna(subset=["rsid", "chrom", "pos"])
df = df.astype(dtype=NORMALIZED_DTYPES)
df = df.set_index("rsid")
return (df,)

return self.read_helper("plink", parser)

def read_snps_csv(self, file, comments, compression):
"""Read and parse CSV file generated by ``snps``.

Expand Down