##
##
## sarchitect designer 2.2 script to compute Tanimoto coefficients (all pairs within dataset)
##
## Shaillay Kumar Dogra
## editor@qsarworld.com
## Feb 20, 2007
##
## INPUT: Data with 'Identifier' and 'Structure' column
##
## MACCS key computation is called from backend algorithm
##
##
## OUTPUT: n X n matrix containing Tanimoto coefficient values
##
## TO DO: not compute half of the n X n matrix
##        



from com.strandgenomics.chem.molalgorithms.util import *
from java.lang import Float
from script.omega import createComponent, showDialog
from javax.swing import *


##--------------------------------------
## COMPUTE TANIMOTO COEFFICIENT
def tanimoto(key1, key2):
	nA = 0.0    ## no. of features ON in molecule A
	nB= 0.0     ## no. of features ON in molecule B	
	nAB = 0.0   ## no. of features ON in both molecule A & B

	for i in range(len(key1)):
		if (key1[i]!=0):  ## feature is ON in A
			nA = nA + 1  
			if (key2[i]!=0):  ## AND feature is ON in B also; count doesn't matter
				#print key1[i], key2[i]
				nAB = nAB + 1

		if (key2[i]!=0):  ## feature is ON in B
			nB = nB + 1

	if ((nA + nB - nAB)!=0):
		tanimoto = nAB / (nA + nB - nAB)
	else: tanimoto =  Float.MAX_VALUE

	return tanimoto

#--------------------------------



## MAIN

dataset = script.project.getActiveDataset()
strCol = dataset.getColumn('Structure')

result = script.algorithm.ComputeMACCSKey(structure=strCol).execute(displayResult=0)
keys = result['maccskeymatrix']
#print len(keys), keys[0], keys[1]


coeff_cols = [ ]
total_rows = dataset.getRowCount()
for i in range(total_rows):
	row_coeff = [ ]
	j = 0
	while (j < total_rows):
		coeff = tanimoto(keys[i], keys[j])
		row_coeff.append(coeff)
		j = j+1
	
	new_col = script.dataset.createFloatColumn(str(dataset[0][i]), row_coeff)
	coeff_cols.append(new_col)

## end of for loop


## create metric dataset, launch view
id_col = dataset.getColumn('Identifier')
coeffset = script.dataset.createDataset("Coefficient", [id_col])
for i in range(len(coeff_cols)):
	coeffset.addColumn(coeff_cols[i])

view = script.view.Table(Title='Tanimoto Coefficients', dataset=coeffset, rowHeight=40)
view.__state__['enableExportColumns'] = 1
view.show()


## report completion
parent=script.tool.getTool().getFrame()
mesg = "Done With Script Execution."
JOptionPane.showMessageDialog(parent,mesg,"STATUS!",JOptionPane.INFORMATION_MESSAGE)


##
## END
##
