###########################################################################
#                                                                         #
# C O P Y R I G H T   N O T I C E                                         #
#  Copyright (c) 2003-10 by:                                              #
#    * California Institute of Technology                                 #
#                                                                         #
#    All Rights Reserved.                                                 #
#                                                                         #
# Permission is hereby granted, free of charge, to any person             #
# obtaining a copy of this software and associated documentation files    #
# (the "Software"), to deal in the Software without restriction,          #
# including without limitation the rights to use, copy, modify, merge,    #
# publish, distribute, sublicense, and/or sell copies of the Software,    #
# and to permit persons to whom the Software is furnished to do so,       #
# subject to the following conditions:                                    #
#                                                                         #
# The above copyright notice and this permission notice shall be          #
# included in all copies or substantial portions of the Software.         #
#                                                                         #
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,         #
# EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF      #
# MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND                   #
# NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS     #
# BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN      #
# ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN       #
# CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE        #
# SOFTWARE.                                                               #
###########################################################################
#
try:
    from pysqlite2 import dbapi2 as sqlite
except:
    from sqlite3 import dbapi2 as sqlite

__all__ = ["analyzeMotifs", "experiment", "locate",  "phyloFoot", "phyloTest",
           "randomset", "simple", "varyLength",  "orthology"]

experimentTypes = [("simple", "Simple"), ("fasta", "Fasta"), ("locate", "Locate"),
                   ("varyLength", "VaryLength"), ("phyloFoot", "PhyloFoot"),
                   ("phyloTest", "PhyloTest"), ("orthology", "Orthology"),
                   ("experiment", "generic")]

webExperimentTypes = [("simple", "Simple"), ("orthology", "Orthology")]


def loadExperiment(expID, expFilePath, analysisID="default", conservationID="default", consDB=""):
    newExp = ""
    try:
        db = sqlite.connect(expFilePath)
        sql = db.cursor()
        sql.execute('select data from settings where expID = :expID and settingName = "experimentType"', locals())
        expType = str(sql.fetchone()[0])
        sql.close()
        db.close()
        expClass = ""
        for etype in experimentTypes:
            if etype[0] == expType:
                expClass = etype[1]

        if expClass != "":
            importString = "from %s import %s" % (expType, expClass)
            exec importString
            expString = 'newExp = %s("%s","%s")' % (expClass, expID, expFilePath)
            exec expString
        else:
            print "Could not find associated class for %s" % expType
    except:
        print "Could not load experiment %s from database %s " % (expID, expFilePath)

    if consDB == "":
        consDB = expFilePath
    try:
        expString = 'newExp.loadAnalysis("%s")' % analysisID
        exec expString
        print "loaded analysis %s" % analysisID
        expString = "newExp.loadConservation('%s', '%s')" % (conservationID, consDB)   
        exec expString
        print "loaded conservation %s" % conservationID
    except:
        pass

    return newExp


def listExperiments(expFilePath):
    result = []
    try:
        db = sqlite.connect(expFilePath)
        sql = db.cursor()
        sql.execute('select distinct expID, data from settings where settingName = "experimentType" ')
        data = sql.fetchall()
        sql.close()
        db.close()
        for (expID, expType) in data:
            result.append((str(expID), str(expType)))
    except:
        print "Could not list experiments in database %s " % (expFilePath)		

    return result