-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbinary.py
More file actions
61 lines (44 loc) · 1.52 KB
/
Copy pathbinary.py
File metadata and controls
61 lines (44 loc) · 1.52 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
import collections
import pandas
from majmin import MajMin
class Binary():
'Class for converting pandas dataframe to binary format'
def __init__(self, df, popdata):
self.pdf = df
self.pd = popdata
mm = MajMin(self.pdf, 0, 1, 2)
self.recode12 = mm.getMajorMinor() #stores major/minor allele for converting to binary format. 2 = missing, 0 = major, 1 = minor
def convert(self):
output = list()
output = self.makeBinary(output)
return output
def makeBinary(self, output):
# make header and append to output
headerList = list()
headerList.append("Sample")
headerList.append("Population_ID")
columns = list(self.pdf)
for col in columns:
headerList.append(col)
headerList.append("")
headerLine = ' '.join(headerList)
output.append(headerLine)
for sampleName, row in self.pdf.iterrows():
lineList = list()
lineList.append(sampleName)
lineList.append(self.pd[sampleName])
# add population name here if desired
for (locus, genotype) in row.items():
alleles = self.split(str(genotype))
# next line is testing for original data missing value (0) instead of binary recoded missing value (2).
if len(alleles) == 1 and alleles[0] == "0":
lineList.append(self.recode12[locus][alleles[0]])
lineList.append(self.recode12[locus][alleles[0]])
else:
for allele in alleles:
lineList.append(self.recode12[locus][allele])
lineString = ' '.join(lineList)
output.append(lineString)
return output
def split(self, word):
return [char for char in word]