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”.
1 2 3 4 |
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
1 2 |
sublist = random.sample(rowlist, numsel) |
Then use the list of selected row numbers to make selections
1 2 3 |
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
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 |
#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.
Last updated 15 November 2018