forked from garudlab/Harris_etal_response
-
Notifications
You must be signed in to change notification settings - Fork 0
/
convertMS.py
executable file
·87 lines (61 loc) · 1.99 KB
/
convertMS.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
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
#! /bin/env python
# bedTileElements - tile each record in input.bed with potentially shorter elements which are written to output.bed
import sys
from optparse import OptionParser
import copy
import csv
#import numpy
import linecache
import random
######################
def convertMS(inFile, outFile):
data = {}
dictionary = {}
dictionary['0'] = 'A'
dictionary['1'] = 'C'
sweep_center = 0
for line in inFile:
line = line.strip()
if line[0] == 'p':
positions_vector_tmp = line.split(' ')
positions_vector = positions_vector_tmp[1:len(positions_vector_tmp)]
# Locate where 0.50000 is in the positions vector (this is where the selected allele is)
#sweep_center = positions_vector.index('0.50000')
# Create a vector to store each position:
for i in range(0, len(positions_vector)):
data[i] = []
else:
for x in range(0, len(line)):
data[x].append(dictionary[line[x]])
# print out
for y in range(0, len(positions_vector)):
s = str(float(positions_vector[y])) + ','
s += ','.join(data[y]) + '\n'
outFile.write(s)
###############
def mkOptionParser():
""" Defines options and returns parser """
usage = """%prog <input file> <output file>
%prog converts MS output to genotypes (0/1 to A/G). """
parser = OptionParser(usage)
return parser
def main():
""" see usage in mkOptionParser. """
parser = mkOptionParser()
options, args= parser.parse_args()
if len(args) != 2:
parser.error("Incorrect number of arguments")
inFN = args[0]
outFN = args[1]
if inFN == '-':
inFile = sys.stdin
else:
inFile = open(inFN, 'r')
if outFN == '-':
outFile = sys.stdout
else:
outFile = open(outFN, 'w')
convertMS(inFile, outFile)
#run main
if __name__ == '__main__':
main()