/** Batch Searching of a Structure Table button, from the Query form
*
* @author Erin Bolstad (ebolstad@chemaxon.com)
* Jan 2012
*/
import javax.swing.SwingUtilities
import com.im.df.api.dml.*
import com.im.commons.progress.*
import com.im.df.api.chem.MarvinStructure
import chemaxon.formats.MolImporter
import chemaxon.struc.Molecule
import com.im.df.api.support.*
import com.im.df.api.util.DIFUtilities
import static com.im.df.query.JChemSearchConstants.*
import chemaxon.sss.search.JChemSearchOptions
import chemaxon.sss.SearchConstants
import chemaxon.sss.search.options.HomologyTranslationOption
import javax.swing.*
import java.awt.GridBagConstraints
import groovy.swing.SwingBuilder
import org.openide.NotifyDescriptor
import org.openide.DialogDisplayer
evaluate = { widget ->
if (SwingUtilities.isEventDispatchThread()) {
Thread.start() {
evaluateImpl(widget)
}
} else {
evaluateImpl(widget)
}
}
evaluateImpl = { widget ->
def rs = widget.form.resultSet
def dataTree = rs.dataTree
// Script assumes that table infrastructure is already set up with relationships, and script is on the Query Results table
def queryEty = dataTree.rootVertex.entity
def queryEdp = queryEty.schema.dataProvider.getEntityDataProvider(queryEty)
def schema = queryEty.schema
def queryVS = rs.rootVertexState
def VMNSvertex = dataTree.rootVertex.edges.find { it.destination.entity.name == 'VMNS' }
def VMNSety = VMNSvertex.destination.entity
def VMNSfld = VMNSety.fields.items.find { it.name == 'Markush structure' }
def VMNSedp = VMNSety.schema.dataProvider.getEntityDataProvider(VMNSety)
def queryStrucFld = queryEty.fields.items.find { it.name == 'Query structure' }
def queryIdFld = queryEty.fields.items.find { it.name == 'CdId' }
def queryNameFld = queryEty.fields.items.find { it.name == 'Query Name' }
def queryDateFld = queryEty.fields.items.find { it.name == 'Query Date' }
def queryUserFld = queryEty.fields.items.find { it.name == 'User' }
def queryHitsFld = queryEty.fields.items.find { it.name == 'Hits' }
assert queryStrucFld != null
assert queryIdFld != null
assert queryNameFld != null
assert queryDateFld != null
assert queryUserFld != null
assert queryHitsFld != null
QUERYSTRUCFLD = queryStrucFld
QUERYIDFLD = queryIdFld
QUERYDATEFLD = queryDateFld
QUERYUSERFLD = queryUserFld
QUERYNAMEFLD = queryNameFld
QUERYHITSFLD = queryHitsFld
def userName = schema.getUsername()
USERNAME = userName
def timeStamp = new Date()
TIMESTAMP = timeStamp
rel = DIFUtilities.findUsagesInRelationships(queryEty)
firstRel = rel.get(0)
FIRSTREL = firstRel
// Get name of the search
def querySel = new SwingBuilder()
querySel.setVariable('properties',[:])
def vars = querySel.variables
def frame = querySel.dialog(title:'Batch Query', modal:true) {
panel () {
gridBagLayout()
label(text:"Enter query name:", constraints:gbc(
gridx:0,
gridy:0,
insets:[10,10,10,0]))
textField(id:'newName', constraints:gbc(
gridx:1,
ipadx:200,
gridy:0,
fill:GridBagConstraints.HORIZONTAL,
insets:[10,5,10,10]))
translation = buttonGroup()
radioButton(id:'broadTrans', text:"Homology Broad Translation", buttonGroup:translation, selected:true, constraints:gbc(
gridx:0,
gridy:1,
gridwidth:2,
anchor:LINE_START))
radioButton(id:'narrowTrans', text:"Homology Narrow Translation", buttonGroup:translation, constraints:gbc(
gridx:0,
gridy:2,
gridwidth:2,
anchor:LINE_START))
button(id:'ok', label: "OK", constraints:gbc(
gridx:0,
gridy:3,
anchor:LINE_END,
insets:[10,0,10,0]),
actionPerformed: {
vars.buttonResults = 'ok'
dispose()})
button(id:'cancel', label: "Cancel", constraints:gbc(
gridx:1,
gridy:3,
anchor:LINE_START,
insets:[10,0,10,0]),
actionPerformed: {
vars.buttonResults = 'quit'
dispose()})
}
}
frame.pack()
frame.setLocationRelativeTo(null)
frame.show()
def chosenAction = vars.buttonResults
if (chosenAction == 'quit') {
return
}
if (chosenAction == 'ok') {
searchName = vars.newName.text
homoGroup = (vars.broadTrans.selected ? 'Homology Broad Translation' : 'Homology Narrow Translation')
}
switch(homoGroup) {
case "Homology Broad Translation":
jcopts = new JChemSearchOptions(SearchConstants.SUBSTRUCTURE)
jcopts.homologyBroadTranslation = HomologyTranslationOption.ALL
break
case "Homology Narrow Translation":
jcopts = new JChemSearchOptions(SearchConstants.SUBSTRUCTURE)
jcopts.homologyNarrowTranslation = HomologyTranslationOption.NONE
break
}
JCOPTS = jcopts
SEARCHNAME = searchName
// Prompt for file of SDFs to search against
def chooser = new JFileChooser()
chooser.setDialogTitle('Select SDF containing the query molecules')
if (chooser.showOpenDialog(null)==JFileChooser.APPROVE_OPTION) {
File fileName = chooser.getSelectedFile()
NAME = fileName.getCanonicalPath()
} else {
return
}
importer = new MolImporter(NAME)
importer.grabbingEnabled = true
mol = new Molecule()
name = NAME
queryNum = 1
while (importer.read(mol)) {
String molStr = importer.grabbedMoleculeString
def queryMol = new MarvinStructure(mol)
QUERYMOL = queryMol
MOLPASS = mol
def queryIDs = queryEdp.queryForIds(DFTermExpression.ALL_DATA, null, DFEnvironmentRO.DEV_NULL)
def id = queryIDs.max() + 1
ID = id
// Build and execute substructure query
def queryMsg = "Running query $queryNum"
def envQuery = EnvUtils.createDefaultEnvironmentRO(queryMsg, true)
try {
jcopts = JCOPTS
queryMol = QUERYMOL
def q = DFTermsFactory.createFieldOperatorValueExpr(Operators.STRUCTURE_EXACT, VMNSfld, [(JCHEM_SEARCH_OPTIONS): jcopts], queryMol)
List resultIds = VMNSedp.queryForIds(q, SortDirective.EMPTY, envQuery)
RESULTIDS = resultIds
} finally {
envQuery?.feedback.finish()
}
List resultIds = RESULTIDS
def lockRL = DIFUtilities.getLockable(queryEdp).obtainLock('Query')
def envRL = EnvUtils.createDefaultEnvironmentRW(lockRL, 'Updating Relationship Data', true)
try {
id = ID
resultIds = RESULTIDS
firstRel = FIRSTREL
// Everything inserted here
mol = MOLPASS
queryStrucFld = QUERYSTRUCFLD
queryIdFld = QUERYIDFLD
queryMol = QUERYMOL
queryDateFld = QUERYDATEFLD
queryUserFld = QUERYUSERFLD
queryNameFld = QUERYNAMEFLD
queryHitsFld = QUERYHITSFLD
timeStamp = TIMESTAMP
userName = USERNAME
searchName = SEARCHNAME
//Update the Query Results table
def hitNum = resultIds.size()
vals =[(queryStrucFld.id):queryMol]
vals.putAt(queryIdFld.id, id)
vals.putAt(queryDateFld.id, timeStamp)
vals.putAt(queryUserFld.id, userName)
vals.putAt(queryHitsFld.id, hitNum)
vals.putAt(queryNameFld.id, searchName)
queryEdp.insert(vals, null, envRL)
if (resultIds.isEmpty() == false) {
resultIds.each { hit ->
def x = id
def y = hit
DIFUtilities.connectRelationalData(firstRel.forward, x, y, envRL)
}
}
} finally {
lockRL?.release()
envRL?.feedback.finish()
}
queryNum++
}
importer.close()
def message = "Batch searching done! To view the new queries, please go to the Query Grid View, and select 'Show All' when in query mode (click Query at the upper left corner of the form)"
NotifyDescriptor d = new NotifyDescriptor.Message(message)
DialogDisplayer.getDefault().notify(d)
}