Skip to content

Instantly share code, notes, and snippets.

Created December 31, 2008 14:43
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save anonymous/41976 to your computer and use it in GitHub Desktop.
Save anonymous/41976 to your computer and use it in GitHub Desktop.
"""Author: BKR
Copied of......hmmm... Inspired by the "fetch from webbook" plugin :-)
Ideas: Getting all alternative names from webbook and or other sources and
creating a Dialog to choose the one desired
"""
import os
##import subprocess
import popen2
import oasa_bridge
import dialogs
import ImageGrab
import tempfile
import Pmw
import StringIO
##from singleton_store import Store
##from main import interactors
def err_mess_box(mess, title="Error"): #Pops up error OK-box
message = ""
for m in mess:
message=message+m+"\n"
dialog = Pmw.Dialog(App.paper, buttons=('OK',),
defaultbutton='OK', title=title)
w = Pmw.LabeledWidget(dialog.interior(), labelpos='n', label_text=message)
w.pack(expand=1, fill='both', padx=4, pady=4)
dialog.activate()
def my_main():
osra = os.environ.get("OSRA", None)
if not osra or not os.path.isfile(osra):
err_mess_box(["You need to set the environment variable " \
"OSRA to point to the OSRA executable.\n" \
"When setting the variable, do not include quotation " \
"marks around the path."])
return
image = ImageGrab.grabclipboard()
if not image:
err_mess_box(["There isn't a suitable image on the clipboard.",
"You need to select an image of a chemical in a",
"PDF, for example, and choose Copy in Adode",
"Reader."])
return
filedes, filename = tempfile.mkstemp(suffix='.png')
sdf = ""
try:
image.save(filename)
dialog = dialogs.progress_dialog(App, title="Progress")
## dialog.activate()
dialog.update(0.1, top_text = "Calling OSRA...",
bottom_text = "OSRAgification in progress")
## process = subprocess.Popen([osra, "-f", "sdf", filename],
## stdout=subprocess.PIPE)
## sdf = process.stdout.read()
stdout, stdin, stderr = popen2.popen3('"%s" -f sdf %s' % (osra, filename))
sdf = stdout.read()
except:
err_mess_box(["There was a problem running OASA."])
os.close(filedes)
os.remove(filename)
dialog.close()
return
else:
os.close(filedes)
os.remove(filename)
if not sdf.rstrip().endswith("$$$$"):
err_mess_box(["Image could not be converted to a molecule."])
dialog.close()
return
dialog.update(0.9, top_text = "Adding molecules to workspace...",
bottom_text = "Almost there!")
mol = StringIO.StringIO(sdf)
molec = oasa_bridge.read_molfile(mol, App.paper)
mol.close()
averagey = sum([atom.y for atom in molec.atoms]) / float(len(molec.atoms))
for atom in molec.atoms:
atom.y = 2 * averagey - atom.y
N = 0
for minimol in molec.get_disconnected_subgraphs():
N += 1
App.paper.stack.append(minimol)
minimol.draw()
App.paper.add_bindings()
App.paper.start_new_undo_record()
dialog.close()
err_mess_box(["%d molecule%s added" % (N, ["s", ""][N==1])], "Info")
my_main()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment