Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
sagemath
GitHub Repository: sagemath/sagesmc
Path: blob/master/src/sage/interfaces/jmoldata.py
8814 views
1
r"""
2
Interface for extracting data and generating images from Jmol readable files.
3
4
JmolData is a no GUI version of Jmol useful for extracting data from files Jmol
5
reads and for generating image files.
6
7
AUTHORS:
8
9
- Jonathan Gutow (2012-06-14): complete doctest coverage
10
- Jonathan Gutow (2012-03-21): initial version
11
"""
12
13
#*******************************************************************************
14
# Copyright (C) 2012 Jonathan Gutow ([email protected])
15
#
16
# Distributed under the terms of the GNU General Public License (GPL)
17
# as published by the Free Software Foundation; either version 2 of
18
# the License, or (at your option) any later version.
19
# http://www.gnu.org/licenses/
20
#*******************************************************************************
21
22
from sage.structure.sage_object import SageObject
23
24
from sage.misc.misc import SAGE_LOCAL, DOT_SAGE, sage_makedirs
25
from sage.misc.temporary_file import tmp_filename
26
27
import subprocess
28
import os
29
30
class JmolData(SageObject):
31
r"""
32
.. todo::
33
34
Create an animated image file (GIF) if spin is on and put data
35
extracted from a file into a variable/string/structure to return
36
"""
37
def __init__(self):
38
"""
39
EXAMPLES:
40
41
Create a JmolData object::
42
43
sage: from sage.interfaces.jmoldata import JmolData
44
sage: JData = JmolData()
45
"""
46
pass
47
48
def is_jvm_available(self):
49
"""
50
Returns True if the Java Virtual Machine is available and False if not.
51
52
EXAMPLES:
53
54
Check that it returns a boolean::
55
56
sage: from sage.interfaces.jmoldata import JmolData
57
sage: JData = JmolData()
58
sage: type(JData.is_jvm_available())
59
<type 'bool'>
60
"""
61
#scratch file for Jmol errors and status
62
jmolscratch = os.path.join(DOT_SAGE, "sage_notebook.sagenb", "jmol_scratch")
63
if not os.path.exists(jmolscratch):
64
sage_makedirs(jmolscratch)
65
scratchout = os.path.join(jmolscratch,"jmolout.txt")
66
jout=open(scratchout,'w')
67
testjavapath = os.path.join(SAGE_LOCAL, "share", "jmol", "testjava.sh")
68
result = subprocess.call([testjavapath],stdout=jout)
69
jout.close()
70
if (result == 0):
71
return (True)
72
else:
73
return (False)
74
75
def export_image(self,
76
targetfile,
77
datafile, #name (path) of data file Jmol can read or script file telling it what to read or load
78
datafile_cmd='script', #"script" or "load"
79
image_type ='PNG', #PNG, JPG, GIF
80
figsize=5,
81
**kwds):
82
r"""
83
This executes JmolData.jar to make an image file.
84
85
INPUT:
86
87
- targetfile -- the full path to the file where the image
88
should be written.
89
90
- datafile -- full path to the data file Jmol can read or
91
text of a script telling Jmol what to read or load.
92
93
- datafile_cmd -- (default ``'script'``) ``'load'`` or ``'script'``
94
should be ``"load"`` for a data file.
95
96
- image_type -- (default ``"PNG"``) ``'PNG'`` ``'JPG'`` or ``'GIF'``
97
98
- figsize -- number (default 5) equal to (pixels/side)/100
99
100
OUTPUT:
101
102
Image file, .png, .gif or .jpg (default .png)
103
104
.. note::
105
106
Examples will generate an error message if a functional Java Virtual Machine (JVM)
107
is not installed on the machine the Sage instance is running on.
108
109
.. warning::
110
111
Programmers using this module should check that the JVM is
112
available before making calls to avoid the user getting
113
error messages. Check for the JVM using the function
114
:meth:`is_jvm_available`, which returns True if a JVM is available.
115
116
EXAMPLES:
117
118
Use Jmol to load a pdb file containing some DNA from a web data
119
base and make an image of the DNA. If you execute this in the
120
notebook, the image will appear in the output cell::
121
122
sage: from sage.interfaces.jmoldata import JmolData
123
sage: JData = JmolData()
124
sage: script = "load =1lcd;display DNA;moveto 0.0 { -473 -713 -518 59.94} 100.0 0.0 0.0 {21.17 26.72 27.295} 27.544636 {0.0 0.0 0.0} -25.287832 64.8414 0.0;"
125
sage: testfile = tmp_filename(ext="DNA.png")
126
sage: JData.export_image(targetfile=testfile,datafile=script,image_type="PNG") # optional -- java internet
127
sage: print os.path.exists(testfile) # optional -- java internet
128
True
129
130
Use Jmol to save an image of a 3-D object created in Sage.
131
This method is used internally by plot3d to generate static images.
132
This example doesn't have correct scaling::
133
134
sage: from sage.interfaces.jmoldata import JmolData
135
sage: JData = JmolData()
136
sage: D=dodecahedron()
137
sage: from sage.misc.misc import SAGE_TMP
138
sage: archive_name=os.path.join(SAGE_TMP, "archive.jmol.zip")
139
sage: D.export_jmol(archive_name) #not scaled properly...need some more steps.
140
sage: testfile = os.path.join(SAGE_TMP, "testimage.png")
141
sage: script = 'set defaultdirectory "%s"\n script SCRIPT\n'%archive_name
142
sage: JData.export_image(targetfile =testfile,datafile = script, image_type="PNG") # optional -- java
143
sage: print os.path.exists(testfile) # optional -- java
144
True
145
146
"""
147
# Set up paths, file names and scripts
148
jmolpath = os.path.join(SAGE_LOCAL, "share", "jmol", "JmolData.jar")
149
launchscript = ""
150
if (datafile_cmd!='script'):
151
launchscript = "load "
152
launchscript = launchscript + datafile
153
imagescript = "write "+ image_type +" "+targetfile+"\n"
154
155
sizeStr = "%sx%s" %(figsize*100,figsize*100)
156
# Scratch file for Jmol errors
157
scratchout = tmp_filename(ext=".txt")
158
with open(scratchout, 'w') as jout:
159
# Now call the java application and write the file.
160
subprocess.call(["java", "-Xmx512m", "-Djava.awt.headless=true",
161
"-jar", jmolpath, "-iox", "-g", sizeStr,
162
"-J", launchscript, "-j", imagescript], stdout=jout, stderr=jout)
163
if not os.path.isfile(targetfile):
164
raise RuntimeError("Jmol failed to create file %s, see %s for details"%(repr(targetfile), repr(scratchout)))
165
os.unlink(scratchout)
166
167