bwaindex.py 2.54 KB
Newer Older
Jerome Mariette's avatar
Jerome Mariette committed
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
#
# Copyright (C) 2012 INRA
# 
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
# 
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
# 
# You should have received a copy of the GNU General Public License
# along with this program.  If not, see <http://www.gnu.org/licenses/>.
#

import os
19
from subprocess import Popen, PIPE
Jerome Mariette's avatar
Jerome Mariette committed
20
21

from jflow.component import Component
Jerome Mariette's avatar
Jerome Mariette committed
22
from workflows.formats import Formats
Jerome Mariette's avatar
Jerome Mariette committed
23
from weaver.function import PythonFunction, ShellFunction
Jerome Mariette's avatar
Jerome Mariette committed
24

Jerome Mariette's avatar
Jerome Mariette committed
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41

def bwa_index(exec_path, algorithm, input_fasta, databank, stdout_path, stderr_path):
    from subprocess import Popen, PIPE
    # first make the symbolic link
    os.symlink(input_fasta, databank)
    # then execute bwa index
    cmd = [exec_path, "index", "-a", algorithm, databank]
    p = Popen(cmd, stdout=PIPE, stderr=PIPE)
    stdout, stderr = p.communicate()
    # write down the stdout
    stdoh = open(stdout_path, "w")
    stdoh.write(stdout)
    stdoh.close()
    # write down the stderr
    stdeh = open(stderr_path, "w")
    stdeh.write(stderr)
    stdeh.close()
Jerome Mariette's avatar
Jerome Mariette committed
42
43
44
45
        
class BWAIndex (Component):
    
    def define_parameters(self, input_fasta, algorithm="bwtsw"):
Jerome Mariette's avatar
Jerome Mariette committed
46
47
        self.add_input_file("input_fasta", "Which fasta file should be indexed", file_format=Formats.FASTA, default=input_fasta, required=True)
        self.add_parameter("algorithm", "Which algorithm should be used to index the fasta file", default=algorithm, choices=["bwtsw", "div", "is"])
48
49
50
        self.add_output_file("databank", "The indexed databank to use with BWA", filename=os.path.basename(input_fasta))
        self.add_output_file("stdout", "The BWAIndex stdout file", filename="bwaindex.stdout")
        self.add_output_file("stderr", "The BWAIndex stderr file", filename="bwaindex.stderr")
Jerome Mariette's avatar
Jerome Mariette committed
51
        
Jerome Mariette's avatar
Jerome Mariette committed
52
53
54
    def process(self):        
        bwaindex = PythonFunction(bwa_index, cmd_format="{EXE} {ARG} {IN} {OUT}")
        bwaindex(inputs=self.input_fasta, outputs=[self.databank, self.stdout, self.stderr], arguments=[self.get_exec_path("bwa"), self.algorithm])
55
56
57
58
59
60

    def get_version(self):
        cmd = [self.get_exec_path("bwa")]
        p = Popen(cmd, stdout=PIPE, stderr=PIPE)
        stdout, stderr = p.communicate()
        return stderr.split()[7]