#   ############################   run_lowess.py  ############################

# PERFORMS LOWESS NORMALIZATION ON SELECTED COLUMNS IN THE DATA SET.

# This script in Avadis performa Lowess Normalization and appends the
# resulting columns as a Child dataset with a prefix "lowess_" attached to the name.
# Works on the current dataset.
# From a drop down menu, user can choose a reference column from the available
# categorical columns in the dataset. The user can also select appropriate columns 
# from menu for performing Lowess normalization against this selected reference column.
# We should ensure that proper signal columns are selected from the availbale 
# categorical columns in the current dataset.
# The values for other two parameters, "Smoothing parameter" and "Robust Iterations" 
# can be given as input through user interface. Displays the original and the normalized
# MVA plots along with the spreadsheet containing the results.
#-------------------------------------------------------------------------------------- 


from script.algorithm import *
from script.view import *
from script.dataset import *
from script.omega import createComponent, showDialog
from com.strandgenomics.cube.dataset import DatasetUtil
from java.lang import Float
from javax.swing import *
import string, math


# Dialog box for selecting the reference column and the columns on which Lowess normalization has to be performed.
# Defining a function for opening dialog boxes.

def openDialogBox2(dset):
	p1=createComponent(type="column",id="name2",description="Choose the reference column", columnType="continuous", dataset=dset)   
	p2=createComponent(type="columnlist",id="name1",description="Choose the columns for Lowess Normalization", columnType="continuous",dataset=dset)
	p3=createComponent(type="float",id="name3",description="Smoothing Parameter", value=0.2)
	p4=createComponent(type="Integer",id="name4",description="Robust Iterations", value=2)
	panel= createComponent(type="group", id="alltogether", description="Lowess Normalization",components=[p1,p2, p3, p4])
	result=showDialog(panel)
	colChoice = script.coercion.to_py(p2.getValue())
	refColChoice = p1.getValue()
	smoothParam = p3.getValue()
	robustIter = p4.getValue()
	return result, colChoice, refColChoice, smoothParam, robustIter



# Main script starts here.

#get current data set
node=script.project.getActiveDatasetNode()
d=node.getDataset()

#get the node tag for current data set.
nodeTag = node.getNodeTag()

#Default values for these parameters. They can be changed in the dialog box.
#See scripting manual for details.
smoothingParam = 0.2
robustIter = 2

#Getting input from dialog boxes
[res,columns,referenceCol,smoothingParam,robustIter] = openDialogBox2(d)

#Calling the function for Lowess Normalization. "if res!=None" takes care of
# the situation when "Cancel" button is pressed. Writes into a child dataset.

if res!=None and len(columns)!=0:
	LowessNormalization(outputOption="Child", dataset=d, columnIndices=columns, 	pairwise=0,referenceColumn=referenceCol, smoothingParameter=smoothingParam,robustIterations=robustIter,tag=nodeTag).execute().display()


