Making a random selection
Sometimes it is the simplest scripts that prove to be the most useful. I regularly need to select a specified number of molecules in a random fashion and this script does just that. Import a sdf file containing structures and run the script to make a random selection.
The first part of the script is simply to create the GUI, this lets the user type in how many molecules they want randomly selecting, as the script stands you can select any number upto 10 million.
Once the user clicks 'OK" we gather the number from the dialog and check that the number wanted is smaller than the number of entires in the table.
The next step is to iterate through the table to set each row to unselected, and add the row number to "rowlist".
for r in range(0, rows): vtable.setRowSelected(r, False) #remove existing selections rowlist.append(r)
The next step is to make a random selection
sublist = random.sample(rowlist, numsel)
Then use the list of selected row numbers to make selections
for q in range(0, len(sublist)): vtable.setRowSelected(sublist[q], True)
I ran this script on the 1.8 million structures downloaded from ChEMBL and it took much less than a second to run.
The Vortex Script
#Select a defined number of rows at random. from javax.swing import JComboBox, JPanel, JLabel, JSpinner, SpinnerNumberModel import com.dotmatics.vortex.util.Layout as layout import random numsel = 10 #number to be Selected #GUI built here content = JPanel() label = JLabel("<html><b>Choose random sample</b>") content2 = JPanel() layout.fill(content2, JLabel("Number to select "), 0, 0) rangeSpinner = JSpinner(SpinnerNumberModel(numsel, 1, 100000000, 1)) layout.fill(content2, rangeSpinner, 1, 0) layout.fill(content,content2, 3, 4) #GUI displayed - when user click OK code continues ret = vortex.showInDialog(content, "Random Pick") if ret == vortex.OK: numsel = rangeSpinner.value #vortex.alert(rangeSpinner.value) rows = vtable.getRealRowCount() #check if selected number is smaller than number of entries if numsel > rows: vortex.alert('Random selection larger than number of rows.') else: rowlist = for r in range(0, rows): vtable.setRowSelected(r, False) #remove existing selections rowlist.append(r) sublist = random.sample(rowlist, numsel) for q in range(0, len(sublist)): vtable.setRowSelected(sublist[q], True) vtable.fireVortexTableChanged(vtable.SelectionChanged);
You can download the script here http://macinchem.org/reviews/vortex_scripts/MakeRandomSelection.vpy.zip.
Last updated 15 November 2018