-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathliftoverCoords.py
executable file
·139 lines (123 loc) · 3.75 KB
/
liftoverCoords.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
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
#!/usr/bin/python
import sys
import os
import getopt
import pyliftover
import csv
import pandas as pd
def main():
params = parseArgs()
if params.liftover:
lo = pyliftover.LiftOver(params.liftover)
if params.table:
tab=pd.read_csv(params.table, sep="\t")
print("Read table:")
print(tab)
def convert(row):
name="chr"+row[params.chrom]
ret=lo.convert_coordinate(name, row[params.bp])
return(int(ret[0][1]))
tab[params.ocol] = tab.apply(convert,axis = 1)
print("Writing the output table:")
print(tab)
tab.to_csv(params.oname, sep="\t", index=False)
if params.marey:
marey=make_marey(tab, params.chrom, params.ocol)
print("Created the following Marey Map input:")
print(marey)
mout=params.oname+"_mmap.txt"
marey.to_csv(mout, sep=" ", quoting=csv.QUOTE_NONNUMERIC, index=False)
else:
params.display_help("Error: No table provided")
else:
params.display_help("Error: No liftover file provided")
#function writes a spoof marey map file from a table of :
#chr \t bp \t cM \t liftover.bp
def make_marey(table, chrom, bp):
ret=pd.DataFrame()
ret["map"] = "chr"+table[chrom].astype(str)
ret["set"] = "fakeset"
ret["mkr"] = "fakemarker"
ret["phys"] = table[bp].astype(int)
ret["gen"] = table["cM"].astype(float)
return(ret)
#Object to parse command-line arguments
class parseArgs():
def __init__(self):
#Define options
try:
options, remainder = getopt.getopt(sys.argv[1:], 'hf:t:p:c:n:o:m', \
["help"])
except getopt.GetoptError as err:
print(err)
self.display_help("\nExiting because getopt returned non-zero exit status.")
#Default values for params
#Input params
self.table = None
self.liftover = None
self.chrom = "chr"
self.bp = "bp"
self.ocol = "liftover.bp"
self.oname = None
self.marey=False
#First pass to see if help menu was called
for o, a in options:
if o in ("-h", "-help", "--help"):
self.display_help("Exiting because help menu was called.")
#Second pass to set all args.
for opt, arg_raw in options:
arg = arg_raw.replace(" ","")
arg = arg.strip()
opt = opt.replace("-","")
#print(opt,arg)
if opt == "h" or opt == "help":
continue
elif opt == "f":
self.liftover=arg
elif opt == "t":
self.table = arg
elif opt == "p":
self.bp=arg
elif opt == "c":
self.chrom=str(arg)
elif opt == "n":
self.ocol=str(arg)
elif opt == "o":
self.oname=str(arg)
elif opt == "m":
self.marey=True
else:
assert False, "Unhandled option %r"%opt
#Check manditory options are set
if not self.liftover or not self.table:
self.display_help("No files provided.")
self.oname=self.table + ".liftover"
def display_help(self, message=None):
if message is not None:
print()
print (message)
print ("\nliftoverCoords.py\n")
print("Author: Tyler K Chafin, University of Arkansas")
print ("Contact: tkchafin@uark.edu")
print ("Description: Converts a table of physical positions from one genome assembly to another given an \".over.chain.gz\" database")
print("""
Arguments:
-h, --help : Display help menu
-f : Path to .over.chain.gz file
-t : Tab-delimited table including coordinates
-p : Column name in table containing the physical (bp) coordinates
[default = \"bp\"]
-c : Column name in table containing the chromosome names
[default = \"chr\"]
-n : Output column name for new table
[default = \"liftover.bp\"]
-o : Output file name
[default = \"<infile>.liftover\"]
-m : (Boolean) Additionally output Marey-Map input file
NOTE: Chromosomes should be named e.g. as \"chr1\" or \"chrX\" in the
.over.chain.gz file, but without the \"chr\" in the table file """)
print()
sys.exit()
#Call main function
if __name__ == '__main__':
main()