-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathflashpca2evec
68 lines (46 loc) · 1.66 KB
/
flashpca2evec
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
#!/usr/bin/python
# This script converts the output of the flashpca into eigenstrat evec format
# By default flashpca produces two files eigenvectors.txt and eigenvals.txt
# so be default flashpca2evec uses these as input
# the --eigenvec and --eigenval flags can specify alternate names
# the --out flag is mandatory and specifies where output should go
import re
import sys
import os
import os.path
import argparse
import string
def setup():
parser = argparse.ArgumentParser(description='Convert flashpca format into evec format')
parser.add_argument('--fam',dest='fam',action='store',required=True)
parser.add_argument('--eigenvec',dest='eigenvec',action='store',default="eigenvectors.txt")
parser.add_argument('--eigenval',dest='eigenval',action='store',default="eigenvalues.txt")
parser.add_argument('--out',dest='out',action='store',required=True)
args = parser.parse_args()
return args
args=setup()
# Get eigenvalues if there are
eigenhead="# "
if os.path.exists(args.eigenval):
f = open(args.eigenval)
for x in f:
eigenhead = eigenhead + x.rstrip("\n")+" "
f.close()
outf = open(args.out,"w")
outf.write("%s\n"%eigenhead)
famf = open(args.fam)
eigf = open(args.eigenvec)
for indiv in famf:
vector = eigf.readline()
if not vector:
sys.exit("Insufficient lines in <%s>\n"%args.eigenvec)
mm = re.search("^\s*(\S+)\s+(\S+)\s*",indiv)
if not mm:
sys.exit("Can't parse line <%s> in fam file\n"%args.fam)
outf.write("%s:%s\t%s"%(mm.group(1),mm.group(2),vector))
line = eigf.readline()
if len(line)>1:
sys.exit("Extra lines in <%s>\n"%args.eigenvec)
famf.close()
eigf.close()
outf.close()