harijay / xtaltools

Python scripts to aid certain aspects of crystallographic analysis

This URL has Read+Write access

xtaltools / maskconvert.py
c90448fe » harijay 2009-07-01 Mask conversion script with... 1 #!/usr/bin/python
2 import subprocess
3 import os.path
4 # To change this template, choose Tools | Templates
5 # and open the template in the editor.
6
7 __author__="hari"
8 __date__ ="$Jun 29, 2009 10:09:03 AM$"
9
10 import os,sys,argparse
11 class Converter(object):
12 program = None
13 # Mask convert maskconvert -i input mask -o outputformat
14 def __init__(self,toformat=None ,infilepath=None):
15 self.toformat = toformat
16 self.infilepath = infilepath
17 if "linux" in sys.platform:
18 gotprog = self.__checkformapman("lx_mapman")
19 if gotprog:
20 Converter.program = gotprog
21 print "Using %s for mask conversion" % self.program
22
23 elif self.__checkformapman("mapman"):
24 gotprog = self.__checkformapman("mapman")
25 Converter.program = gotprog
26 print "Using %s for mask conversion" % self.program
27
28 elif "darwin" in sys.platform:
29 gotprog = self.__checkformapman("osx_mapman")
30 if gotprog:
31 Converter.program = gotprog
32 print "Using %s for mask conversion" % self.program
33
34 elif __checkformapman("mapman"):
35 gotprog = __checkformapman("mapman")
36 Converter.program = gotprog
37 print "Using %s for mask conversion" % self.program
38 else:
39 print "Exiting no mapman in path"
40 raise
41
42 def __checkformapman(self,myprogram):
43 def is_exe(tested_program):
44 return os.path.exists(tested_program) and os.access(tested_program, os.X_OK)
45
46 fpath, fname = os.path.split(myprogram)
47
48 if fpath:
49 if is_exe(os.path.join(fpath,fname)):
50 program = os.path.join(fpath, fname)
51 return program
52
53 else:
54 for path in os.environ["PATH"].split(os.pathsep):
55 exe_file = os.path.join(path, myprogram)
56 if is_exe(exe_file):
57 program = exe_file
58 return program
59 return None
60
61 def convertto(self,format,informat="CCP4"):
62 print "Converting format to %s using %s " % ( format , self.program)
63 base,name = os.path.split(self.infilepath)
64 name_root = os.path.splitext(name)[0]
65 # The coot file browser does not filter and show files ending with synonymous X-PLOR format
66 # So converting it to CNS
67
68 if format == "X-PLOR":
69 format = "CNS"
70 outfile = os.path.join(base,"".join([name_root,".",format.lower()]))
71 print os.path.join(base, outfile)
72 if os.path.lexists(self.infilepath):
73 mapman_scr = """re m1 %s %s\nwr m1 %s %s\nquit\n""" % (self.infilepath,informat,outfile,format)
74 tmp = open("tmp.scr","w")
75 tmp.write(mapman_scr)
76 tmp.close()
77 a = os.system(self.program + " < tmp.scr ")
78
79
80
81
82
83 if __name__ == "__main__":
84 parser = argparse.ArgumentParser()
85 parser.add_argument("-i" , dest="infile" ,required=True)
86 parser.add_argument("-d" , dest="output_format", required=True, choices=["CCP4", "X-PLOR","CNS","OMAP"])
87 parser.add_argument("-s",dest="input_format",choices=["CCP4", "X-PLOR","CNS","OMAP"])
88 cli = parser.parse_args()
89 c = Converter(infilepath=cli.infile)
90 if cli.input_format:
91 c.convertto(cli.output_format,cli.input_format)
92 else:
93 c.convertto(cli.output_format)
94