erange 4.0a dev release with integrated cistematic
[erange.git] / cistematic / experiments / fasta.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 # a simple use of the experiment class
31 from experiment import Experiment
32 from draw import Draw
33 from analyzeMotifs import AnalyzeMotifs
34 import sys
35
36 class Fasta(Experiment, AnalyzeMotifs, Draw):
37     experimentType = "fasta"
38
39
40     def run(self, fastaFile):
41         self.loadFasta(fastaFile)
42         Experiment.run(self)
43         datasetID = self.genepoolID
44         for (prog, settingsID) in self.programs:
45             if 1:
46                 prog.inputFile(fastaFile)
47                 settings = self.getSettingsID(settingsID)[1]
48                 prog.setSettings(eval(settings))
49                 runID = self.setRun(prog.name(), datasetID, settingsID)
50                 tag = str(runID)
51                 prog.setTagID(tag)
52                 prog.run()
53                 theMotifs = prog.getMotifs()
54                 for mot in theMotifs:
55                     self.appendResults(mot)
56             else:
57                 self.mlog("Error running program %s with settings %s" % (prog, settingsID))
58
59
60     def loadFasta(self, ffile):
61         """ load fasta file into genepool
62         """
63         f=open(ffile, "r")
64         line = f.readline()
65         i = 1
66         while line != "":
67             seq = ""
68             templine = f.readline()
69             while templine != "" and templine[0] != ">":
70                 seq = seq + templine[0:-1]
71                 templine = f.readline()
72                 name = "seq%s" % line.strip()[1:]
73                 # "progress bar" of dots...
74                 if (i % 10 == 0):
75                     sys.stderr.write(".")
76
77                 if (i % 1000 ==  0):
78                     sys.stderr.write("%s\n" % i)
79                 else:
80                     if (i % 100 == 0):
81                         sys.stderr.write(" ")
82
83                 i = i + 1
84
85             self.genepool[(ffile, name)] = seq
86             line = templine
87             print 
88         f.close()