erange 4.0a dev release with integrated cistematic
[erange.git] / cistematic / programs / mafft.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 # mafft.py
31 from cistematic.programs import Program
32 import os, time
33 from cistematic.core.motif import Motif
34
35
36 class Mafft(Program):
37     """ Multiple Alignment using Fast Fourier Transform. Uses fftnsi as described in:
38         K. Katoh, K. Misawa, K. Kuma and T. Miyata (2002)
39         Nucleic Acids Research 30: 3059-3066.
40     """
41     mafftPath = "%s/mafft/" % Program.programRoot
42     motifs = []
43     argDict = {}
44
45
46     def getSettings(self):
47         return self.argDict
48
49
50     def setSettings(self, settings):
51         self.motifs = []
52         self.argDict = settings
53
54
55     def buildCommand(self):
56         cmd = self.mafftPath + "fftnsi "
57         for arg in self.argDict.keys():
58             cmd = cmd + " --" + arg + " %s" % str(self.argDict[arg])
59         cmd +=  " --quiet " + self.inputFilePath 
60         print "cmd is %s" % (cmd)
61
62         return cmd
63
64
65     def setGapOpening(self, op):
66         """ Gap opening penalty. Default is 1.58
67         """
68         self.argDict["op"] = op
69
70
71     def setOffset(self, ep):
72         """ Offset - like a gap expansion penalty. Default is 0.120
73         """
74         self.argDict["ep"] = ep
75
76
77     def setScoringMatrix(self, bl):
78         """ set Blossum scoring matrix. Choices are 30, 45, 62, and 80.
79         """
80         self.argDict["bl"] = bl
81
82
83     def setMaxiterate(self, maxi):
84         """ maximum number of iterations in progressive method.
85         """
86         self.argDict["maxiterate"] = maxi
87
88
89     def setRetree(self, tnum):
90         """ number of tree building in progressive method.
91         """
92         self.argDict["retree"] = tnum
93
94
95     def run(self):
96         startTime = time.time()
97         self.contents = os.popen(Mafft.buildCommand(self)).readlines()
98         stopTime = time.time()
99     
100         print "\nThis run took %.3f - %.3f = %.3f seconds" % (startTime, stopTime, stopTime - startTime)
101
102
103     def getAlignment(self):
104         """ take the results stored in self.contents and return a dictionary for inclusion into the genepool.
105         """
106         alignedDict= {}
107         dictKey = ""
108         sequence = ""
109         for line in self.contents:
110             if line[0] == ">":
111                 if len(dictKey) > 0 and len(sequence) > 0:
112                     alignedDict[dictKey] = sequence
113                 dictKey = line[2:-1]
114                 sequence = ""
115             else:
116                 sequence += line[:-1]
117
118         if len(dictKey) > 0 and len(sequence) > 0:
119             alignedDict[dictKey] = sequence
120
121         return alignedDict