Macs in Chemistry

Insanely great science

 

Scripting the Chemical Identifier Resolver 2

In the previous applescript we used ChemBioDraw to render a structure generated by the Chemical Identifier Resolver, I was asked if it would be possible to do the same thing for other drawing packages such as MarvinSketch?

The answer is yes but because MarvinSketch does not have applescript support we have to do it slightly differently. Rather than using scripting commands we script system events to evoke the “Paste” command.

tell application "System Events"
      keystroke "v" using {command down}
        delay 0.5

  end tell

However this approach does have the drawback that the application needs to be running, and getting the timing correct whilst waiting for an application to open can cause problems. If we simply tell the application to activate you may find the application window obscures the subsequent dialog box, so we tell “MarvinSketch” to activate and then hide it.

tell application "MarvinSketch" to activate
tell application "Finder"
  set visible of process "MarvinSketch" to false
end tell

The rest of the script is the same as the previous one.

The script can be downloaded from here CIRStructureMarvin.scpt.zip

See also the corresponding script for ChemBioDraw.

The Script

--Created by Macs in Chemistry (http://www.macinchem.org)

tell application "MarvinSketch" to activate
tell application "Finder"
  set visible of process "MarvinSketch" to false
end tell

set the clipboard to «class ktxt» of ((the clipboard as text) as record)

set the_clip to the clipboard
--Comment out if not needed
--display dialog the_clip


display dialog "Input Name" default answer the_clip buttons {"Cancel", "Get Structure"} default button 2
copy the result as list to {text_returned, button_pressed}
--Need to encode text to sure URL is OK
set the_encode_text to encode_text(text_returned, true, false)


--set theURL to "http://cactus.nci.nih.gov/chemical/structure/name/smiles"

set theURL to "http://cactus.nci.nih.gov/chemical/structure/" & the_encode_text & "/smiles"


set the_SMILES to (do shell script "curl -L  " & theURL)


set the clipboard to the_SMILES


tell application "MarvinSketch"
  
    activate

    tell application "System Events"
      keystroke "v" using {command down}
        delay 0.5

  end tell




end tell


on encode_text(this_text, encode_URL_A, encode_URL_B)
  set the standard_characters to "abcdefghijklmnopqrstuvwxyz0123456789"
    set the URL_A_chars to "$+!'/?;&@=#%><{}[]\"~`^\\|*"
    set the URL_B_chars to ".-_:"
    set the acceptable_characters to the standard_characters
    if encode_URL_A is false then set the acceptable_characters to the acceptable_characters & the URL_A_chars
    if encode_URL_B is false then set the acceptable_characters to the acceptable_characters & the URL_B_chars
    set the encoded_text to ""
    repeat with this_char in this_text
      if this_char is in the acceptable_characters then
          set the encoded_text to (the encoded_text & this_char)
      else
          set the encoded_text to (the encoded_text & encode_char(this_char)) as string
      end if
  end repeat
    return the encoded_text
end encode_text

on encode_char(this_char)
  set the ASCII_num to (the ASCII number this_char)
    set the hex_list to {"0", "1", "2", "3", "4", "5", "6", "7", "8", "9", "A", "B", "C", "D", "E", "F"}
    set x to item ((ASCII_num div 16) + 1) of the hex_list
    set y to item ((ASCII_num mod 16) + 1) of the hex_list
    return ("%" & x & y) as string
end encode_char