annotate rnafold_SHAPE.py @ 7:2d0d76f6a8aa draft default tip

planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/rna_tools/vienna_rna commit 36681a08c6e44c663169caaefd964781c43d0d29
author rnateam
date Wed, 20 Dec 2017 08:35:52 -0500
parents 9ef5cff7b5e9
children
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
2
9ef5cff7b5e9 planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/rna_tools/vienna_rna commit b'bd13ffd1c3e126a6dc59dd3c478347ec1b5824a3\n'
rnateam
parents:
diff changeset
1 ### overcoming the problem of SHAPE data working with a single line.
9ef5cff7b5e9 planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/rna_tools/vienna_rna commit b'bd13ffd1c3e126a6dc59dd3c478347ec1b5824a3\n'
rnateam
parents:
diff changeset
2 ### creating multiple multiple files containg SHAPE data for a single sequence and running RNAfold for every
9ef5cff7b5e9 planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/rna_tools/vienna_rna commit b'bd13ffd1c3e126a6dc59dd3c478347ec1b5824a3\n'
rnateam
parents:
diff changeset
3 ### single sequence.
9ef5cff7b5e9 planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/rna_tools/vienna_rna commit b'bd13ffd1c3e126a6dc59dd3c478347ec1b5824a3\n'
rnateam
parents:
diff changeset
4
9ef5cff7b5e9 planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/rna_tools/vienna_rna commit b'bd13ffd1c3e126a6dc59dd3c478347ec1b5824a3\n'
rnateam
parents:
diff changeset
5 import os
9ef5cff7b5e9 planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/rna_tools/vienna_rna commit b'bd13ffd1c3e126a6dc59dd3c478347ec1b5824a3\n'
rnateam
parents:
diff changeset
6 import sys
9ef5cff7b5e9 planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/rna_tools/vienna_rna commit b'bd13ffd1c3e126a6dc59dd3c478347ec1b5824a3\n'
rnateam
parents:
diff changeset
7 from os import system
9ef5cff7b5e9 planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/rna_tools/vienna_rna commit b'bd13ffd1c3e126a6dc59dd3c478347ec1b5824a3\n'
rnateam
parents:
diff changeset
8 from Bio import SeqIO
9ef5cff7b5e9 planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/rna_tools/vienna_rna commit b'bd13ffd1c3e126a6dc59dd3c478347ec1b5824a3\n'
rnateam
parents:
diff changeset
9 import re
9ef5cff7b5e9 planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/rna_tools/vienna_rna commit b'bd13ffd1c3e126a6dc59dd3c478347ec1b5824a3\n'
rnateam
parents:
diff changeset
10 from subprocess import Popen, PIPE
9ef5cff7b5e9 planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/rna_tools/vienna_rna commit b'bd13ffd1c3e126a6dc59dd3c478347ec1b5824a3\n'
rnateam
parents:
diff changeset
11
9ef5cff7b5e9 planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/rna_tools/vienna_rna commit b'bd13ffd1c3e126a6dc59dd3c478347ec1b5824a3\n'
rnateam
parents:
diff changeset
12 params_list = sys.argv[1:]
9ef5cff7b5e9 planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/rna_tools/vienna_rna commit b'bd13ffd1c3e126a6dc59dd3c478347ec1b5824a3\n'
rnateam
parents:
diff changeset
13 param_list_no_shape = [s for s in params_list if not "--shape=" in s ]
9ef5cff7b5e9 planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/rna_tools/vienna_rna commit b'bd13ffd1c3e126a6dc59dd3c478347ec1b5824a3\n'
rnateam
parents:
diff changeset
14 shape_file = [s for s in params_list if "--shape=" in s ]
9ef5cff7b5e9 planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/rna_tools/vienna_rna commit b'bd13ffd1c3e126a6dc59dd3c478347ec1b5824a3\n'
rnateam
parents:
diff changeset
15 assert (len(shape_file) == 1)
9ef5cff7b5e9 planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/rna_tools/vienna_rna commit b'bd13ffd1c3e126a6dc59dd3c478347ec1b5824a3\n'
rnateam
parents:
diff changeset
16
9ef5cff7b5e9 planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/rna_tools/vienna_rna commit b'bd13ffd1c3e126a6dc59dd3c478347ec1b5824a3\n'
rnateam
parents:
diff changeset
17 shape_file = shape_file[0]
9ef5cff7b5e9 planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/rna_tools/vienna_rna commit b'bd13ffd1c3e126a6dc59dd3c478347ec1b5824a3\n'
rnateam
parents:
diff changeset
18 shape_file = shape_file.replace('--shape=', '')
9ef5cff7b5e9 planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/rna_tools/vienna_rna commit b'bd13ffd1c3e126a6dc59dd3c478347ec1b5824a3\n'
rnateam
parents:
diff changeset
19
9ef5cff7b5e9 planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/rna_tools/vienna_rna commit b'bd13ffd1c3e126a6dc59dd3c478347ec1b5824a3\n'
rnateam
parents:
diff changeset
20 params_no_shape = " ".join(str(x) for x in param_list_no_shape)
9ef5cff7b5e9 planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/rna_tools/vienna_rna commit b'bd13ffd1c3e126a6dc59dd3c478347ec1b5824a3\n'
rnateam
parents:
diff changeset
21
9ef5cff7b5e9 planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/rna_tools/vienna_rna commit b'bd13ffd1c3e126a6dc59dd3c478347ec1b5824a3\n'
rnateam
parents:
diff changeset
22 pattern = re.compile("^>.*$")
9ef5cff7b5e9 planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/rna_tools/vienna_rna commit b'bd13ffd1c3e126a6dc59dd3c478347ec1b5824a3\n'
rnateam
parents:
diff changeset
23 id_line = ""
9ef5cff7b5e9 planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/rna_tools/vienna_rna commit b'bd13ffd1c3e126a6dc59dd3c478347ec1b5824a3\n'
rnateam
parents:
diff changeset
24 with open(shape_file, 'r') as f:
9ef5cff7b5e9 planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/rna_tools/vienna_rna commit b'bd13ffd1c3e126a6dc59dd3c478347ec1b5824a3\n'
rnateam
parents:
diff changeset
25 content = f.read()
9ef5cff7b5e9 planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/rna_tools/vienna_rna commit b'bd13ffd1c3e126a6dc59dd3c478347ec1b5824a3\n'
rnateam
parents:
diff changeset
26 lines = content.split('\n')
9ef5cff7b5e9 planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/rna_tools/vienna_rna commit b'bd13ffd1c3e126a6dc59dd3c478347ec1b5824a3\n'
rnateam
parents:
diff changeset
27 for line in lines:
9ef5cff7b5e9 planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/rna_tools/vienna_rna commit b'bd13ffd1c3e126a6dc59dd3c478347ec1b5824a3\n'
rnateam
parents:
diff changeset
28 if pattern.match(line):
9ef5cff7b5e9 planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/rna_tools/vienna_rna commit b'bd13ffd1c3e126a6dc59dd3c478347ec1b5824a3\n'
rnateam
parents:
diff changeset
29 id_line = line.split()[0]
9ef5cff7b5e9 planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/rna_tools/vienna_rna commit b'bd13ffd1c3e126a6dc59dd3c478347ec1b5824a3\n'
rnateam
parents:
diff changeset
30 id_line = id_line[1:]
9ef5cff7b5e9 planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/rna_tools/vienna_rna commit b'bd13ffd1c3e126a6dc59dd3c478347ec1b5824a3\n'
rnateam
parents:
diff changeset
31 continue
9ef5cff7b5e9 planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/rna_tools/vienna_rna commit b'bd13ffd1c3e126a6dc59dd3c478347ec1b5824a3\n'
rnateam
parents:
diff changeset
32 else:
9ef5cff7b5e9 planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/rna_tools/vienna_rna commit b'bd13ffd1c3e126a6dc59dd3c478347ec1b5824a3\n'
rnateam
parents:
diff changeset
33 with open(id_line +'.tmp', "a") as clFile:
9ef5cff7b5e9 planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/rna_tools/vienna_rna commit b'bd13ffd1c3e126a6dc59dd3c478347ec1b5824a3\n'
rnateam
parents:
diff changeset
34 clFile.write(line + "\n")
9ef5cff7b5e9 planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/rna_tools/vienna_rna commit b'bd13ffd1c3e126a6dc59dd3c478347ec1b5824a3\n'
rnateam
parents:
diff changeset
35
9ef5cff7b5e9 planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/rna_tools/vienna_rna commit b'bd13ffd1c3e126a6dc59dd3c478347ec1b5824a3\n'
rnateam
parents:
diff changeset
36 input_file = sys.stdin
9ef5cff7b5e9 planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/rna_tools/vienna_rna commit b'bd13ffd1c3e126a6dc59dd3c478347ec1b5824a3\n'
rnateam
parents:
diff changeset
37
9ef5cff7b5e9 planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/rna_tools/vienna_rna commit b'bd13ffd1c3e126a6dc59dd3c478347ec1b5824a3\n'
rnateam
parents:
diff changeset
38 for record in SeqIO.parse(input_file, "fasta"):
9ef5cff7b5e9 planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/rna_tools/vienna_rna commit b'bd13ffd1c3e126a6dc59dd3c478347ec1b5824a3\n'
rnateam
parents:
diff changeset
39 seq = ">{}\n{}".format(record.id,record.seq)
9ef5cff7b5e9 planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/rna_tools/vienna_rna commit b'bd13ffd1c3e126a6dc59dd3c478347ec1b5824a3\n'
rnateam
parents:
diff changeset
40 cmd = " RNAfold --shape=" + record.id + '.tmp ' + params_no_shape
9ef5cff7b5e9 planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/rna_tools/vienna_rna commit b'bd13ffd1c3e126a6dc59dd3c478347ec1b5824a3\n'
rnateam
parents:
diff changeset
41 p = Popen(cmd , stdin=PIPE, shell=True, stdout=PIPE, stderr=PIPE)
9ef5cff7b5e9 planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/rna_tools/vienna_rna commit b'bd13ffd1c3e126a6dc59dd3c478347ec1b5824a3\n'
rnateam
parents:
diff changeset
42 out,err = p.communicate(seq.encode())
9ef5cff7b5e9 planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/rna_tools/vienna_rna commit b'bd13ffd1c3e126a6dc59dd3c478347ec1b5824a3\n'
rnateam
parents:
diff changeset
43 if err:
9ef5cff7b5e9 planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/rna_tools/vienna_rna commit b'bd13ffd1c3e126a6dc59dd3c478347ec1b5824a3\n'
rnateam
parents:
diff changeset
44 raise RuntimeError("Error in calling RNAfold\n{}\n{}\n".format(out, err))
9ef5cff7b5e9 planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/rna_tools/vienna_rna commit b'bd13ffd1c3e126a6dc59dd3c478347ec1b5824a3\n'
rnateam
parents:
diff changeset
45 print (out.decode('utf-8').strip())