1
2
3
4
5
6
7
8 """
9 opsin - A Cinfony module for accessing OPSIN from CPython and Jython
10
11 Global variables:
12 opsin - the underlying OPSIN library (uk.ac.cam.ch.wwmm.opsin)
13 informats - a dictionary of supported input formats
14 outformats - a dictionary of supported output formats
15 """
16
17 import os
18 import sys
19
20 if sys.platform[:4] == "java":
21 import uk.ac.cam.ch.wwmm.opsin as opsin
22 else:
23 import jpype
24 if not jpype.isJVMStarted():
25 _jvm = os.environ['JPYPE_JVM']
26 if _jvm[0] == '"':
27 _jvm = _jvm[1:-1]
28 _cp = os.environ['CLASSPATH']
29 jpype.startJVM(_jvm, "-Djava.class.path=" + _cp)
30 opsin = jpype.JPackage("uk").ac.cam.ch.wwmm.opsin
31
32 try:
33 _nametostruct = opsin.NameToStructure.getInstance()
34 _restoinchi = opsin.NameToInchi.convertResultToInChI
35 except TypeError:
36 raise ImportError("The OPSIN Jar file cannot be found.")
37
38 informats = {'iupac': 'IUPAC name'}
39 """A dictionary of supported input formats"""
40 outformats = {'cml': "Chemical Markup Language", 'inchi': "InChI",
41 'smi': "SMILES"}
42 """A dictionary of supported output formats"""
45 """Read in a molecule from a string.
46
47 Required parameters:
48 format - see the informats variable for a list of available
49 input formats
50 string
51
52 Example:
53 >>> input = "propane"
54 >>> mymol = readstring("iupac", input)
55 """
56 if format!="iupac":
57 raise ValueError("%s is not a recognised OPSIN format" % format)
58
59 result = _nametostruct.parseChemicalName(string)
60 if str(result.getStatus()) == "FAILURE":
61 raise IOError("Failed to convert '%s' to format '%s'\n%s" % (
62 string, format, result.getMessage()))
63
64 return Molecule(result)
65
68 """Represent a opsinjpype Molecule.
69
70 Required parameters:
71 OpsinResult -- the result of using OPSIN to parse an IUPAC string
72
73 Methods:
74 write()
75
76 The underlying OpsinResult can be accessed using the attribute:
77 OpsinResult
78 """
79 _cinfony = True
80
82 if hasattr(OpsinResult, "_cinfony"):
83 raise IOError, "An opsin Molecule cannot be created from another Cinfony Molecule"
84
85 self.OpsinResult = OpsinResult
86
89 @property
91 return (0, self.write("smi"))
92 - def write(self, format="smi", filename=None, overwrite=False):
93 """Write the molecule to a file or return a string.
94
95 Optional parameters:
96 format -- see the outformats variable for a list of available
97 output formats (default is "smi")
98 filename -- default is None
99 overwite -- if the output file already exists, should it
100 be overwritten? (default is False)
101
102 If a filename is specified, the result is written to a file.
103 Otherwise, a string is returned containing the result.
104 """
105 if format not in outformats:
106 raise ValueError,"%s is not a recognised OPSIN format" % format
107
108 if filename is not None and not overwrite and os.path.isfile(filename):
109 raise IOError, "%s already exists. Use 'overwrite=True' to overwrite it." % filename
110
111 if format == "cml":
112 result = str(self.OpsinResult.getCml().toXML())
113 elif format == "inchi":
114 result = str(_restoinchi(self.OpsinResult))
115 elif format == "smi":
116 result = str(self.OpsinResult.getSmiles())
117
118 if filename:
119 outputfile = open(filename, "w")
120 print >> outputfile, result
121 outputfile.close()
122 else:
123 return result
124
125 if __name__=="__main__":
126 mol = readstring("iupac", "propane")
127 print mol.write("inchi")
128