Skip to content

Commit

Permalink
Merge pull request #176 from teepean/plink_support
Browse files Browse the repository at this point in the history
Add support to detect plink generated 23andme files
  • Loading branch information
apriha authored Aug 21, 2024
2 parents 38a874f + 7589ad8 commit 9af329c
Showing 1 changed file with 86 additions and 0 deletions.
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

0 comments on commit 9af329c

Please sign in to comment.