File: Jelly.py

package info (click to toggle)
pbsuite 15.8.24%2Bdfsg-2
  • links: PTS, VCS
  • area: main
  • in suites: stretch
  • size: 14,512 kB
  • ctags: 1,951
  • sloc: python: 10,962; sh: 147; xml: 21; makefile: 14
file content (270 lines) | stat: -rwxr-xr-x 10,350 bytes parent folder | download | duplicates (3)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
#!/usr/bin/python
"""
document the protocol
"""

import os, sys, subprocess, logging, time
from optparse import OptionParser
from string import Template
from xml.etree import ElementTree
from glob import glob

STAGES = ["setup", "mapping", "support", "extraction", "assembly", "output"]


from pbsuite.jelly import Stages
from pbsuite.utils.setupLogging import *
from pbsuite.utils.CommandRunner import * 


USAGE = """USAGE: Jelly.py <stage> <protocol.xml> [-x \"--options for stage\"]
    
    Jelly is the driver for each stage of the 
    reference genome upgrade. 
    
    <stage> is one of
        %s
    <protocol.xml> contains the information about the
    data and parameters Jelly will run. See README.txt
    or the documentation for the Protocol's format.""" % ("\n\t".join(STAGES))

class JellyProtocol():
    """
    Independent method to parse protocols
    """
    def __init__(self, fileName):
        self.protocolName = os.path.abspath(fileName)
        self.parseProtocol()
    
    def parseProtocol(self):
        """
        Parses a Jelly Protocol
        """
        try:
            file = ElementTree.parse(self.protocolName)
        except Exception:
            logging.error(("If you're actually sure the input Protocol is "\
                           "where you said it is, then the Protocol doesn't "\
                           "have valid XML Format. Use an XML validator."))
            sys.exit(1)
        
        root = file.getroot()
        
        refNode = root.find("reference")
        
        if refNode is None:
            logging.error("Protocol doesn't have <reference> element.")
            sys.exit(1)
        else:
            self.scaffoldName = refNode.text
            if not os.path.exists(self.scaffoldName):
                logging.error("Reference %s Does Not Exist!" % (self.scaffoldName))
                sys.exit(1)
            self.referenceNameBase = self.scaffoldName[:self.scaffoldName.rindex(".fasta")]
            self.scaffoldQualName = self.referenceNameBase+".qual"
            if not os.path.exists(self.scaffoldQualName):
                sys.stderr.write(("[WARNING] Couldn't find %s... assuming there " \
                                "isn't a quality file for the reference\n") % \
                                (self.scaffoldQualName))
                self.scaffoldQualName = None
                
            self.gapTable = self.referenceNameBase+".gapInfo.bed"
            
            self.reference = self.scaffoldName
             
            self.referenceIndex = self.reference+".sa"
            
        inputNode = root.find("input")
        if inputNode.attrib.has_key("baseDir"):
            self.baseDir = inputNode.attrib["baseDir"]
        else:
            self.baseDir = ""
        
        self.inputs = []
        for input in inputNode:
            
            #Do some checking on the inputs
            fullPath = os.path.join(self.baseDir, input.text)
            if not os.path.exists(fullPath):
                logging.error("Input %s doesn't exit! Exiting" % (fullPath))
                exit(0)
            if not (fullPath.lower().endswith('.fasta') or fullPath.lower().endswith('.fastq')):
                logging.error("Input %s needs to end with .fasta or .fastq! Exiting" % (fullPath))
                exit(0)
            
            self.inputs.append(fullPath)
        if len(self.inputs) == 0:
            logging.error("Protocol doesn't specifiy any input inputs!")
            sys.exit(1)

        
        outputNode = root.find("outputDir")
        if outputNode is None:
            logging.warning("Output directory not specified. Using pwd. Hope you're cool with that...")
            self.outDir = os.getcwd()
        else:
            self.outDir = outputNode.text
        
        #Command Runner is going to sort all this out.
        #And create an object to do the calls.
        self.runXML = root.find("cluster")
        
        blasrNode = root.find("blasr")
        if blasrNode is None:
            logging.warning("No blasr parameters!?")
            self.blasrParams = ""
        else:
            self.blasrParams = blasrNode.text
    

class JellyRunner():
    """
    Take a JellyProtocol and loads in the variables in a way that 
    JellyRunner can use it easily!
    """
    def __init__(self):
        """
        Given a protocol fn, load it up so we are ready to run. 
        """
        self.parseArgs()
        setupLogging(self.options.debug)
        sys.stderr.write("""
Please Cite: English, Adam C., Stephen Richards, Yi Han, Min Wang,
             Vanesa Vee, Jiaxin Qu, Xiang Qin, et al. "Mind the
             Gap: Upgrading Genomes with Pacific Biosciences RS
             Long-Read Sequencing Technology." PLoS ONE 7, no. 11
             (November 21, 2012): e47768.
             doi:10.1371/journal.pone.0047768.\n\n""")
        self.parseProtocol()
        
    def parseProtocol(self):
        self.protocol = JellyProtocol(self.protocolName)
        self.parseCluster(self.protocol.runXML)
        
    def parseCluster(self, xmlNode):
        if xmlNode is None:
            self.runCmd = CommandRunner()
        else:
            command = xmlNode.find("command")
            if command is None:
                logging.error(("You're trying to use a cluster " \
                               "template, but you didn't specify the " \
                               "template. Please read the documentation." \
                               "Exiting.\n"""))
                sys.exit(1)
            nJobs = xmlNode.find("nJobs")
            
            if nJobs is None or nJobs.text == '0':
                logging.warning(("Not Specifying number of jobs may " \
                                  "overload clusters."))
                nJobs = 0
            else:
                nJobs = int(nJobs.text)
            
            cmdTemplate = command.text
            self.runCmd = CommandRunner(cmdTemplate, nJobs)
                    
    def parseArgs(self):
        """
        Uses OptionParser to parse out input
        Jelly.py <stage> <protocol>
        """
        parser = OptionParser(USAGE)
        parser.remove_option("-h")
        parser.add_option("-h", "--help", action="store_true", default=False)
        
        parser.add_option("--debug",action="store_true",default=False)
        parser.add_option("-x", dest="extras", type="string", default="", 
                help="-x \"<options>\" are options to pass into the stage you're running")
        
        self.options, args = parser.parse_args()

        if self.options.help == True:
            if len(args) == 1:
                if args[0] in STAGES:
                    print exe(Stages.PRINT_HELPS[args[0]])[1]
                    sys.exit(0)
                #Else, this will drop down to the next parser.error
            else:
                print parser.format_help()
                sys.exit(0)
        if len(args) != 2 or args[0] not in STAGES:
            parser.error("Invalid Arguments. Expected one of\n'%s'" % "', '".join(STAGES))
            sys.exit(1)
        self.executeStage = args[0]
        self.protocolName = os.path.abspath(args[1])
        
        
    def run(self):
        logging.info("Executing Stage: %s" % self.executeStage)
        
        if self.options.debug:
            Stages.DEBUG = "--debug"
        
        #Setup before a stage and run
        if self.executeStage == "setup":
            wDir = os.path.dirname(self.protocol.scaffoldName)
            myCommands = [Stages.setup(self.protocol.scaffoldName, \
                        self.protocol.scaffoldQualName, self.protocol.gapTable, \
                        self.options.extras)]
        
        elif self.executeStage == "mapping":
            wDir = os.path.join(self.protocol.outDir, "mapping")
            try:
                os.mkdir(wDir)
            except OSError:
                logging.debug("%s already exists" % wDir)
            if not os.path.exists(wDir):
                logging.warning("%s was not created. Check write permissions!" % wDir)
                sys.exit(1)
            
            myCommands = Stages.mapping(self.protocol.inputs, wDir, \
                        self.protocol.reference, self.protocol.referenceIndex, \
                        self.protocol.blasrParams, self.options.extras)
        
        elif self.executeStage == "support":
            wDir = os.path.join(self.protocol.outDir, "support")
            try:
                os.mkdir(wDir)
            except OSError:
                logging.debug("%s already exists" % wDir)
            if not os.path.exists(wDir):
                logging.warning("%s was not created. Check write permissions!" % wDir)
                sys.exit(1)
       
            myCommands = Stages.support(self.protocol.outDir, self.protocol.gapTable, \
                    wDir, self.options.extras) 
        
        elif self.executeStage == "extraction":
            wDir = os.path.join(self.protocol.outDir, "assembly")
            try:
                os.mkdir(wDir)
            except OSError:
                logging.debug("%s already exists" % wDir)
            if not os.path.exists(wDir):
                logging.warning("%s was not created. Check write permissions!" % wDir)
                sys.exit(1)
            
            myCommands = [Stages.extraction(wDir, \
                                            self.protocolName, \
                                            self.options.extras)]
        
        elif self.executeStage == "assembly":
            wDir = os.path.join(self.protocol.outDir, "assembly")
            
            myCommands = Stages.assembly(wDir, self.protocol.gapTable, \
                                         self.options.extras)
        
        elif self.executeStage == "output":
            wDir = os.path.join(self.protocol.outDir, "assembly")
            
            myCommands = [Stages.collection(self.protocol.outDir, self.protocol, self.options.extras)]
        
        logging.debug("CommandRunner Returned: " + 
            str(self.runCmd(myCommands, wDir, self.executeStage )) )
        
        logging.info("Finished %s Stage: %s" % (self.runCmd.runType, self.executeStage))
        
if __name__ == '__main__':
    prog = JellyRunner()
    prog.run()