erange 4.0a dev release with integrated cistematic
[erange.git] / cistematic / experiments / __init__.py
1 ###########################################################################
2 #                                                                         #
3 # C O P Y R I G H T   N O T I C E                                         #
4 #  Copyright (c) 2003-10 by:                                              #
5 #    * California Institute of Technology                                 #
6 #                                                                         #
7 #    All Rights Reserved.                                                 #
8 #                                                                         #
9 # Permission is hereby granted, free of charge, to any person             #
10 # obtaining a copy of this software and associated documentation files    #
11 # (the "Software"), to deal in the Software without restriction,          #
12 # including without limitation the rights to use, copy, modify, merge,    #
13 # publish, distribute, sublicense, and/or sell copies of the Software,    #
14 # and to permit persons to whom the Software is furnished to do so,       #
15 # subject to the following conditions:                                    #
16 #                                                                         #
17 # The above copyright notice and this permission notice shall be          #
18 # included in all copies or substantial portions of the Software.         #
19 #                                                                         #
20 # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,         #
21 # EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF      #
22 # MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND                   #
23 # NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS     #
24 # BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN      #
25 # ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN       #
26 # CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE        #
27 # SOFTWARE.                                                               #
28 ###########################################################################
29 #
30 try:
31     from pysqlite2 import dbapi2 as sqlite
32 except:
33     from sqlite3 import dbapi2 as sqlite
34
35 __all__ = ["analyzeMotifs", "experiment", "locate",  "phyloFoot", "phyloTest",
36            "randomset", "simple", "varyLength",  "orthology"]
37
38 experimentTypes = [("simple", "Simple"), ("fasta", "Fasta"), ("locate", "Locate"),
39                    ("varyLength", "VaryLength"), ("phyloFoot", "PhyloFoot"),
40                    ("phyloTest", "PhyloTest"), ("orthology", "Orthology"),
41                    ("experiment", "generic")]
42
43 webExperimentTypes = [("simple", "Simple"), ("orthology", "Orthology")]
44
45
46 def loadExperiment(expID, expFilePath, analysisID="default", conservationID="default", consDB=""):
47     newExp = ""
48     try:
49         db = sqlite.connect(expFilePath)
50         sql = db.cursor()
51         sql.execute('select data from settings where expID = :expID and settingName = "experimentType"', locals())
52         expType = str(sql.fetchone()[0])
53         sql.close()
54         db.close()
55         expClass = ""
56         for etype in experimentTypes:
57             if etype[0] == expType:
58                 expClass = etype[1]
59
60         if expClass != "":
61             importString = "from %s import %s" % (expType, expClass)
62             exec importString
63             expString = 'newExp = %s("%s","%s")' % (expClass, expID, expFilePath)
64             exec expString
65         else:
66             print "Could not find associated class for %s" % expType
67     except:
68         print "Could not load experiment %s from database %s " % (expID, expFilePath)
69
70     if consDB == "":
71         consDB = expFilePath
72     try:
73         expString = 'newExp.loadAnalysis("%s")' % analysisID
74         exec expString
75         print "loaded analysis %s" % analysisID
76         expString = "newExp.loadConservation('%s', '%s')" % (conservationID, consDB)   
77         exec expString
78         print "loaded conservation %s" % conservationID
79     except:
80         pass
81
82     return newExp
83
84
85 def listExperiments(expFilePath):
86     result = []
87     try:
88         db = sqlite.connect(expFilePath)
89         sql = db.cursor()
90         sql.execute('select distinct expID, data from settings where settingName = "experimentType" ')
91         data = sql.fetchall()
92         sql.close()
93         db.close()
94         for (expID, expType) in data:
95             result.append((str(expID), str(expType)))
96     except:
97         print "Could not list experiments in database %s " % (expFilePath)              
98
99     return result