-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmsSample.py
68 lines (57 loc) · 1.91 KB
/
msSample.py
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
62
63
64
65
66
67
68
import sys, gzip, random
import numpy as np
class msSample:
def __init__(self, segsites, pos,haplos):
self.segsites = segsites
self.positions = pos
self.haplotypes = haplos
self.n = len(self.haplotypes)
def convertHaplosFiniteSites(self,seqlength):
newH = np.zeros((self.n,seqlength))
if self.segsites > 0:
newPos = [np.floor(x*seqlength) for x in self.positions]
for i in list(range(0,self.n)):
for j in list(range(0,self.segsites)):
newH[i,int(newPos[j])]=self.haplotypes[i][j]
self.haplotypes=newH
class msFile:
def __init__(self):
self.repNumber=0
self.samples = []
def readFile(aFileName):
sampleList=[]
msStream = open(aFileName)
header = msStream.readline().strip().split()
program,numSamples,numSims = header[:3]
if len(header) > 3:
otherParams = " " + " ".join(header[3:])
else:
otherParams = ""
numSamples, numSims = int(numSamples),int(numSims)
#advance to first simulation
line = msStream.readline()
while line.strip() != "//":
line = msStream.readline()
repLs = []
while line:
if line.strip() != "//":
sys.exit("Malformed ms-style output file: read '%s' instead of '//'. AAAARRRRGGHHH!!!!!\n" %(line.strip()))
s = int(msStream.readline().strip().split()[-1]) #segsites line
h=[]
positionsLine=[]
if s > 0:
positionsLine = msStream.readline().strip().split()
if not positionsLine[0] == ("positions:"):
sys.exit("Malformed ms-style output file. AAAARRRRGGHHH!!!!!\n")
positionsLine.pop(0)
positionsLine = [float(x) for x in positionsLine]
for i in range(numSamples):
currLine = msStream.readline()
h.append([int(x) for x in list(currLine.strip())])
line = msStream.readline()
#advance to the next non-empty line or EOF
while line and line.strip() == "":
line = msStream.readline()
sampleList.append(msSample(s,positionsLine,h))
msStream.close()
return sampleList