Wednesday, April 07, 2010

Control IGV from Excel or other Macintosh/Windows Application using Applescript/VBA

IGV is great for browsing genomes. Here is some quick Applescript|VBA that you can install into an Excel workbook (or other Mac|Windows app) to allow navigating IGV to the locus in the current cell. With this approach, I can deliver Excel workbooks containing RNASeq expression values, and the recipient can sort/filter or otherwise slice and dice them, and easily navigate to the genes that survive the dicing....

I'm likely to make some improvements on this and build an installer for it, but, I put it out here/now in case someone
* has already done this better
* has suggestions for improvement to take into consideration
* want's to use it as is
-- NAME: IGVGoTo.scpt
-- PURPOSE: Applescript to cause IGV (http://www.broad.mit.edu/igv/) to 'goto' the locus appearing in the Excel's active cell.
-- AUTHOR: malcolm_cook@stowers.org
-- REQUIRES:
-- * installing XNet as obtained from http://lestang.org/spip.php?article20
-- * the XOSL.Framework gets installed into Users//Library/Frameworks
-- * configuring IGV > View > Preferences > advanced > Enable Port
-- it must be checked ON and set to 60151 (the default)
-- INSTALLATION:
-- * Save this script as Users//Documents/Microsoft User Data/Excel Script Menu Items/IGVGoTo.scpt
-- * OPTIONAL: assign a keyboard shortcut:
-- * Open the System Preferences, click on "Keyboard & Mouse" and select the "Keyboard Shortcuts" tab.
-- * Click on the plus sign beneath the list view. In the dialog sheet that pops up, select Microsoft Excel from the Application menu (install it if needed - I had to)
-- * type "IGVGoTo" into the "Menu Title" field, tab to the Keyboard Shortcut field, and type in the shortcut you would like to use (I prefer Command-Option-I) and click the Add button
-- RUNNING IT:
-- * IGVGoTo will appear in Microsoft Excels's script menu (rightmost menu)
-- * navigate to a cell whose value is locus for the currently displayed IGV genome and run the script.
-- The active cells value will be used as the `locus` target of a `goto` command
-- * c.f. http://www.broadinstitute.org/igv/PortCommands for valid targets
-- TODO
-- * bring IGV to the front (optionally?)
-- * dispatch on the frontmost appliction - get the locus appropriate to the application - implement for
-- MSWord and FileMakerPro
-- * trap errors and provide diagnostics (i.e. XNet not installed, port not openable, IGV not running, bad response from IGV, etc)

tell application "Microsoft Excel"
my IGVGoTo("localhost", 60151, value of active cell as string) end tell

on IGVGoTo(theHost, thePort, theLocus)
tell application "XNet"
launch
set s to make new socket with properties {host:theHost, port:thePort}
socket open s
socket write s data "goto " & theLocus
delete s
end tell
end IGVGoTo

Here's the VBA for the Window's folk:

Public Sub IGV_Goto()
'PURPOSE: Excel Macro to cause IGV (http://www.broad.mit.edu/igv/) to 'goto' the locus appearing in the Excel's active cell.
' AUTHOR: malcolm_cook@stowers.org
' REQUIRES:
' * configuring IGV > View > Preferences > advanced > Enable Port
' it must be checked ON and set to 60151 (the default)
' * installing a free winsock implementation from "http://www.ostrosoft.com/oswinsck/oswinsck_vba.asp"
' INSTALLATION:
' * create a new VBA Macro out of this script in the desired Excel workbook - like this:
' alt-F11 to open VBA
' click on 'This workbook'
' paste in this script
' create a reference to the winsock library, by checking "OstroSoft Winsock Component" under "Tools > references".
' quit VBA with alt-q
' Alt-F8 (back in Excel) will list your macros - click option button to assign to a key (I use Ctrl-Shift-I)



On Error GoTo Err
Dim cmd As String
Dim locus As String
Dim r As Excel.Range
Set r = Excel.ActiveCell.EntireRow '.CurrentArray.RowDifferences..Rows
locus = Excel.ActiveCell.Value
cmd = "goto " & locus
Dim wso As New OSWINSCK.TCP ' free winsock implementation from "http://www.ostrosoft.com/oswinsck/oswinsck_vba.asp"
wso.Connect "localhost", 60151
wso.SendData cmd & vbCr
wso.Disconnect
ok:
Exit Sub
Err:
MsgBox Err.Description
End Sub

No comments: