Mercurial > repos > devteam > cuffmerge
annotate cuffmerge_wrapper.py @ 12:1707a530e598 draft
planemo upload for repository https://github.com/galaxyproject/tools-devteam/tree/master/tool_collections/cufflinks/cuffmerge commit eb18f691975ef9539b5ebd4f118343c8ad967a1f
author | devteam |
---|---|
date | Tue, 07 Feb 2017 18:39:42 -0500 |
parents | b6e3849293b1 |
children | cf747d1bd79a |
rev | line source |
---|---|
0 | 1 #!/usr/bin/env python |
2 | |
12
1707a530e598
planemo upload for repository https://github.com/galaxyproject/tools-devteam/tree/master/tool_collections/cufflinks/cuffmerge commit eb18f691975ef9539b5ebd4f118343c8ad967a1f
devteam
parents:
10
diff
changeset
|
3 import optparse |
1707a530e598
planemo upload for repository https://github.com/galaxyproject/tools-devteam/tree/master/tool_collections/cufflinks/cuffmerge commit eb18f691975ef9539b5ebd4f118343c8ad967a1f
devteam
parents:
10
diff
changeset
|
4 import os |
1707a530e598
planemo upload for repository https://github.com/galaxyproject/tools-devteam/tree/master/tool_collections/cufflinks/cuffmerge commit eb18f691975ef9539b5ebd4f118343c8ad967a1f
devteam
parents:
10
diff
changeset
|
5 import shutil |
1707a530e598
planemo upload for repository https://github.com/galaxyproject/tools-devteam/tree/master/tool_collections/cufflinks/cuffmerge commit eb18f691975ef9539b5ebd4f118343c8ad967a1f
devteam
parents:
10
diff
changeset
|
6 import subprocess |
1707a530e598
planemo upload for repository https://github.com/galaxyproject/tools-devteam/tree/master/tool_collections/cufflinks/cuffmerge commit eb18f691975ef9539b5ebd4f118343c8ad967a1f
devteam
parents:
10
diff
changeset
|
7 import sys |
1707a530e598
planemo upload for repository https://github.com/galaxyproject/tools-devteam/tree/master/tool_collections/cufflinks/cuffmerge commit eb18f691975ef9539b5ebd4f118343c8ad967a1f
devteam
parents:
10
diff
changeset
|
8 import tempfile |
0 | 9 |
12
1707a530e598
planemo upload for repository https://github.com/galaxyproject/tools-devteam/tree/master/tool_collections/cufflinks/cuffmerge commit eb18f691975ef9539b5ebd4f118343c8ad967a1f
devteam
parents:
10
diff
changeset
|
10 |
1707a530e598
planemo upload for repository https://github.com/galaxyproject/tools-devteam/tree/master/tool_collections/cufflinks/cuffmerge commit eb18f691975ef9539b5ebd4f118343c8ad967a1f
devteam
parents:
10
diff
changeset
|
11 def stop_err(msg): |
1707a530e598
planemo upload for repository https://github.com/galaxyproject/tools-devteam/tree/master/tool_collections/cufflinks/cuffmerge commit eb18f691975ef9539b5ebd4f118343c8ad967a1f
devteam
parents:
10
diff
changeset
|
12 sys.stderr.write('%s\n' % msg) |
0 | 13 sys.exit() |
14 | |
12
1707a530e598
planemo upload for repository https://github.com/galaxyproject/tools-devteam/tree/master/tool_collections/cufflinks/cuffmerge commit eb18f691975ef9539b5ebd4f118343c8ad967a1f
devteam
parents:
10
diff
changeset
|
15 |
0 | 16 def __main__(): |
17 parser = optparse.OptionParser() | |
12
1707a530e598
planemo upload for repository https://github.com/galaxyproject/tools-devteam/tree/master/tool_collections/cufflinks/cuffmerge commit eb18f691975ef9539b5ebd4f118343c8ad967a1f
devteam
parents:
10
diff
changeset
|
18 parser.add_option('-g', dest='ref_annotation', help='An optional "reference" annotation GTF. Each sample is matched against this file, and sample isoforms are tagged as overlapping, matching, or novel where appropriate. See the refmap and tmap output file descriptions below.') |
1707a530e598
planemo upload for repository https://github.com/galaxyproject/tools-devteam/tree/master/tool_collections/cufflinks/cuffmerge commit eb18f691975ef9539b5ebd4f118343c8ad967a1f
devteam
parents:
10
diff
changeset
|
19 parser.add_option('-s', dest='use_seq_data', action="store_true", help='Causes cuffmerge to look into for fasta files with the underlying genomic sequences (one file per contig) against which your reads were aligned for some optional classification functions. For example, Cufflinks transcripts consisting mostly of lower-case bases are classified as repeats. Note that <seq_dir> must contain one fasta file per reference chromosome, and each file must be named after the chromosome, and have a .fa or .fasta extension.') |
1707a530e598
planemo upload for repository https://github.com/galaxyproject/tools-devteam/tree/master/tool_collections/cufflinks/cuffmerge commit eb18f691975ef9539b5ebd4f118343c8ad967a1f
devteam
parents:
10
diff
changeset
|
20 parser.add_option('-p', '--num-threads', dest='num_threads', help='Use this many threads to align reads. The default is 1.') |
1707a530e598
planemo upload for repository https://github.com/galaxyproject/tools-devteam/tree/master/tool_collections/cufflinks/cuffmerge commit eb18f691975ef9539b5ebd4f118343c8ad967a1f
devteam
parents:
10
diff
changeset
|
21 |
0 | 22 # Wrapper / Galaxy options. |
12
1707a530e598
planemo upload for repository https://github.com/galaxyproject/tools-devteam/tree/master/tool_collections/cufflinks/cuffmerge commit eb18f691975ef9539b5ebd4f118343c8ad967a1f
devteam
parents:
10
diff
changeset
|
23 parser.add_option('', '--index', dest='index', help='The path of the reference genome') |
1707a530e598
planemo upload for repository https://github.com/galaxyproject/tools-devteam/tree/master/tool_collections/cufflinks/cuffmerge commit eb18f691975ef9539b5ebd4f118343c8ad967a1f
devteam
parents:
10
diff
changeset
|
24 parser.add_option('', '--ref_file', dest='ref_file', help='The reference dataset from the history') |
1707a530e598
planemo upload for repository https://github.com/galaxyproject/tools-devteam/tree/master/tool_collections/cufflinks/cuffmerge commit eb18f691975ef9539b5ebd4f118343c8ad967a1f
devteam
parents:
10
diff
changeset
|
25 |
0 | 26 # Outputs. |
12
1707a530e598
planemo upload for repository https://github.com/galaxyproject/tools-devteam/tree/master/tool_collections/cufflinks/cuffmerge commit eb18f691975ef9539b5ebd4f118343c8ad967a1f
devteam
parents:
10
diff
changeset
|
27 parser.add_option('', '--merged-transcripts', dest='merged_transcripts') |
1707a530e598
planemo upload for repository https://github.com/galaxyproject/tools-devteam/tree/master/tool_collections/cufflinks/cuffmerge commit eb18f691975ef9539b5ebd4f118343c8ad967a1f
devteam
parents:
10
diff
changeset
|
28 parser.add_option('--min-isoform-fraction', dest='min_isoform_fraction') |
1707a530e598
planemo upload for repository https://github.com/galaxyproject/tools-devteam/tree/master/tool_collections/cufflinks/cuffmerge commit eb18f691975ef9539b5ebd4f118343c8ad967a1f
devteam
parents:
10
diff
changeset
|
29 |
0 | 30 (options, args) = parser.parse_args() |
12
1707a530e598
planemo upload for repository https://github.com/galaxyproject/tools-devteam/tree/master/tool_collections/cufflinks/cuffmerge commit eb18f691975ef9539b5ebd4f118343c8ad967a1f
devteam
parents:
10
diff
changeset
|
31 |
0 | 32 # output version # of tool |
33 try: | |
12
1707a530e598
planemo upload for repository https://github.com/galaxyproject/tools-devteam/tree/master/tool_collections/cufflinks/cuffmerge commit eb18f691975ef9539b5ebd4f118343c8ad967a1f
devteam
parents:
10
diff
changeset
|
34 with tempfile.NamedTemporaryFile() as tmp_stdout: |
1707a530e598
planemo upload for repository https://github.com/galaxyproject/tools-devteam/tree/master/tool_collections/cufflinks/cuffmerge commit eb18f691975ef9539b5ebd4f118343c8ad967a1f
devteam
parents:
10
diff
changeset
|
35 returncode = subprocess.call(args='cuffmerge -v 2>&1', stdout=tmp_stdout, shell=True) |
1707a530e598
planemo upload for repository https://github.com/galaxyproject/tools-devteam/tree/master/tool_collections/cufflinks/cuffmerge commit eb18f691975ef9539b5ebd4f118343c8ad967a1f
devteam
parents:
10
diff
changeset
|
36 stdout = None |
1707a530e598
planemo upload for repository https://github.com/galaxyproject/tools-devteam/tree/master/tool_collections/cufflinks/cuffmerge commit eb18f691975ef9539b5ebd4f118343c8ad967a1f
devteam
parents:
10
diff
changeset
|
37 with open(tmp_stdout.name) as tmp_stdout2: |
1707a530e598
planemo upload for repository https://github.com/galaxyproject/tools-devteam/tree/master/tool_collections/cufflinks/cuffmerge commit eb18f691975ef9539b5ebd4f118343c8ad967a1f
devteam
parents:
10
diff
changeset
|
38 for line in tmp_stdout2: |
1707a530e598
planemo upload for repository https://github.com/galaxyproject/tools-devteam/tree/master/tool_collections/cufflinks/cuffmerge commit eb18f691975ef9539b5ebd4f118343c8ad967a1f
devteam
parents:
10
diff
changeset
|
39 if line.lower().find('merge_cuff_asms v') >= 0: |
1707a530e598
planemo upload for repository https://github.com/galaxyproject/tools-devteam/tree/master/tool_collections/cufflinks/cuffmerge commit eb18f691975ef9539b5ebd4f118343c8ad967a1f
devteam
parents:
10
diff
changeset
|
40 stdout = line.strip() |
1707a530e598
planemo upload for repository https://github.com/galaxyproject/tools-devteam/tree/master/tool_collections/cufflinks/cuffmerge commit eb18f691975ef9539b5ebd4f118343c8ad967a1f
devteam
parents:
10
diff
changeset
|
41 break |
0 | 42 if stdout: |
12
1707a530e598
planemo upload for repository https://github.com/galaxyproject/tools-devteam/tree/master/tool_collections/cufflinks/cuffmerge commit eb18f691975ef9539b5ebd4f118343c8ad967a1f
devteam
parents:
10
diff
changeset
|
43 sys.stdout.write('%s\n' % stdout) |
0 | 44 else: |
45 raise Exception | |
46 except: | |
12
1707a530e598
planemo upload for repository https://github.com/galaxyproject/tools-devteam/tree/master/tool_collections/cufflinks/cuffmerge commit eb18f691975ef9539b5ebd4f118343c8ad967a1f
devteam
parents:
10
diff
changeset
|
47 sys.stdout.write('Could not determine Cuffmerge version\n') |
1707a530e598
planemo upload for repository https://github.com/galaxyproject/tools-devteam/tree/master/tool_collections/cufflinks/cuffmerge commit eb18f691975ef9539b5ebd4f118343c8ad967a1f
devteam
parents:
10
diff
changeset
|
48 |
0 | 49 # Set/link to sequence file. |
50 if options.use_seq_data: | |
2
5b285b6e4ee3
Update to the new data table specification.
Dave Bouvier <dave@bx.psu.edu>
parents:
0
diff
changeset
|
51 if options.ref_file: |
0 | 52 # Sequence data from history. |
53 # Create symbolic link to ref_file so that index will be created in working directory. | |
54 seq_path = "ref.fa" | |
12
1707a530e598
planemo upload for repository https://github.com/galaxyproject/tools-devteam/tree/master/tool_collections/cufflinks/cuffmerge commit eb18f691975ef9539b5ebd4f118343c8ad967a1f
devteam
parents:
10
diff
changeset
|
55 os.symlink(options.ref_file, seq_path) |
0 | 56 else: |
12
1707a530e598
planemo upload for repository https://github.com/galaxyproject/tools-devteam/tree/master/tool_collections/cufflinks/cuffmerge commit eb18f691975ef9539b5ebd4f118343c8ad967a1f
devteam
parents:
10
diff
changeset
|
57 if not os.path.exists(options.index): |
1707a530e598
planemo upload for repository https://github.com/galaxyproject/tools-devteam/tree/master/tool_collections/cufflinks/cuffmerge commit eb18f691975ef9539b5ebd4f118343c8ad967a1f
devteam
parents:
10
diff
changeset
|
58 stop_err('Reference genome %s not present, request it by reporting this error.' % options.index) |
2
5b285b6e4ee3
Update to the new data table specification.
Dave Bouvier <dave@bx.psu.edu>
parents:
0
diff
changeset
|
59 seq_path = options.index |
12
1707a530e598
planemo upload for repository https://github.com/galaxyproject/tools-devteam/tree/master/tool_collections/cufflinks/cuffmerge commit eb18f691975ef9539b5ebd4f118343c8ad967a1f
devteam
parents:
10
diff
changeset
|
60 |
0 | 61 # Build command. |
12
1707a530e598
planemo upload for repository https://github.com/galaxyproject/tools-devteam/tree/master/tool_collections/cufflinks/cuffmerge commit eb18f691975ef9539b5ebd4f118343c8ad967a1f
devteam
parents:
10
diff
changeset
|
62 |
0 | 63 # Base. |
64 cmd = "cuffmerge -o cm_output " | |
12
1707a530e598
planemo upload for repository https://github.com/galaxyproject/tools-devteam/tree/master/tool_collections/cufflinks/cuffmerge commit eb18f691975ef9539b5ebd4f118343c8ad967a1f
devteam
parents:
10
diff
changeset
|
65 |
0 | 66 # Add options. |
67 if options.num_threads: | |
12
1707a530e598
planemo upload for repository https://github.com/galaxyproject/tools-devteam/tree/master/tool_collections/cufflinks/cuffmerge commit eb18f691975ef9539b5ebd4f118343c8ad967a1f
devteam
parents:
10
diff
changeset
|
68 cmd += (" -p %i " % int(options.num_threads)) |
0 | 69 if options.ref_annotation: |
70 cmd += " -g %s " % options.ref_annotation | |
71 if options.use_seq_data: | |
72 cmd += " -s %s " % seq_path | |
10 | 73 if options.min_isoform_fraction: |
74 cmd += " --min-isoform-fraction %s " % (options.min_isoform_fraction) | |
0 | 75 # Add input files to a file. |
12
1707a530e598
planemo upload for repository https://github.com/galaxyproject/tools-devteam/tree/master/tool_collections/cufflinks/cuffmerge commit eb18f691975ef9539b5ebd4f118343c8ad967a1f
devteam
parents:
10
diff
changeset
|
76 with tempfile.NamedTemporaryFile(mode='w', dir=".", delete=False) as inputs_file: |
1707a530e598
planemo upload for repository https://github.com/galaxyproject/tools-devteam/tree/master/tool_collections/cufflinks/cuffmerge commit eb18f691975ef9539b5ebd4f118343c8ad967a1f
devteam
parents:
10
diff
changeset
|
77 for arg in args: |
1707a530e598
planemo upload for repository https://github.com/galaxyproject/tools-devteam/tree/master/tool_collections/cufflinks/cuffmerge commit eb18f691975ef9539b5ebd4f118343c8ad967a1f
devteam
parents:
10
diff
changeset
|
78 inputs_file.write(arg + "\n") |
1707a530e598
planemo upload for repository https://github.com/galaxyproject/tools-devteam/tree/master/tool_collections/cufflinks/cuffmerge commit eb18f691975ef9539b5ebd4f118343c8ad967a1f
devteam
parents:
10
diff
changeset
|
79 cmd += inputs_file.name |
0 | 80 |
81 # Run command. | |
12
1707a530e598
planemo upload for repository https://github.com/galaxyproject/tools-devteam/tree/master/tool_collections/cufflinks/cuffmerge commit eb18f691975ef9539b5ebd4f118343c8ad967a1f
devteam
parents:
10
diff
changeset
|
82 try: |
1707a530e598
planemo upload for repository https://github.com/galaxyproject/tools-devteam/tree/master/tool_collections/cufflinks/cuffmerge commit eb18f691975ef9539b5ebd4f118343c8ad967a1f
devteam
parents:
10
diff
changeset
|
83 with tempfile.NamedTemporaryFile(dir=".") as tmp_stderr: |
1707a530e598
planemo upload for repository https://github.com/galaxyproject/tools-devteam/tree/master/tool_collections/cufflinks/cuffmerge commit eb18f691975ef9539b5ebd4f118343c8ad967a1f
devteam
parents:
10
diff
changeset
|
84 returncode = subprocess.call(args=cmd, stderr=tmp_stderr, shell=True) |
1707a530e598
planemo upload for repository https://github.com/galaxyproject/tools-devteam/tree/master/tool_collections/cufflinks/cuffmerge commit eb18f691975ef9539b5ebd4f118343c8ad967a1f
devteam
parents:
10
diff
changeset
|
85 |
1707a530e598
planemo upload for repository https://github.com/galaxyproject/tools-devteam/tree/master/tool_collections/cufflinks/cuffmerge commit eb18f691975ef9539b5ebd4f118343c8ad967a1f
devteam
parents:
10
diff
changeset
|
86 # Error checking. |
1707a530e598
planemo upload for repository https://github.com/galaxyproject/tools-devteam/tree/master/tool_collections/cufflinks/cuffmerge commit eb18f691975ef9539b5ebd4f118343c8ad967a1f
devteam
parents:
10
diff
changeset
|
87 if returncode != 0: |
1707a530e598
planemo upload for repository https://github.com/galaxyproject/tools-devteam/tree/master/tool_collections/cufflinks/cuffmerge commit eb18f691975ef9539b5ebd4f118343c8ad967a1f
devteam
parents:
10
diff
changeset
|
88 # Get stderr, allowing for case where it's very large. |
1707a530e598
planemo upload for repository https://github.com/galaxyproject/tools-devteam/tree/master/tool_collections/cufflinks/cuffmerge commit eb18f691975ef9539b5ebd4f118343c8ad967a1f
devteam
parents:
10
diff
changeset
|
89 buffsize = 1048576 |
1707a530e598
planemo upload for repository https://github.com/galaxyproject/tools-devteam/tree/master/tool_collections/cufflinks/cuffmerge commit eb18f691975ef9539b5ebd4f118343c8ad967a1f
devteam
parents:
10
diff
changeset
|
90 stderr = '' |
1707a530e598
planemo upload for repository https://github.com/galaxyproject/tools-devteam/tree/master/tool_collections/cufflinks/cuffmerge commit eb18f691975ef9539b5ebd4f118343c8ad967a1f
devteam
parents:
10
diff
changeset
|
91 with open(tmp_stderr.name, 'r') as tmp_stderr2: |
1707a530e598
planemo upload for repository https://github.com/galaxyproject/tools-devteam/tree/master/tool_collections/cufflinks/cuffmerge commit eb18f691975ef9539b5ebd4f118343c8ad967a1f
devteam
parents:
10
diff
changeset
|
92 try: |
1707a530e598
planemo upload for repository https://github.com/galaxyproject/tools-devteam/tree/master/tool_collections/cufflinks/cuffmerge commit eb18f691975ef9539b5ebd4f118343c8ad967a1f
devteam
parents:
10
diff
changeset
|
93 while True: |
1707a530e598
planemo upload for repository https://github.com/galaxyproject/tools-devteam/tree/master/tool_collections/cufflinks/cuffmerge commit eb18f691975ef9539b5ebd4f118343c8ad967a1f
devteam
parents:
10
diff
changeset
|
94 stderr += tmp_stderr2.read(buffsize) |
1707a530e598
planemo upload for repository https://github.com/galaxyproject/tools-devteam/tree/master/tool_collections/cufflinks/cuffmerge commit eb18f691975ef9539b5ebd4f118343c8ad967a1f
devteam
parents:
10
diff
changeset
|
95 if not stderr or len(stderr) % buffsize != 0: |
1707a530e598
planemo upload for repository https://github.com/galaxyproject/tools-devteam/tree/master/tool_collections/cufflinks/cuffmerge commit eb18f691975ef9539b5ebd4f118343c8ad967a1f
devteam
parents:
10
diff
changeset
|
96 break |
1707a530e598
planemo upload for repository https://github.com/galaxyproject/tools-devteam/tree/master/tool_collections/cufflinks/cuffmerge commit eb18f691975ef9539b5ebd4f118343c8ad967a1f
devteam
parents:
10
diff
changeset
|
97 except OverflowError: |
1707a530e598
planemo upload for repository https://github.com/galaxyproject/tools-devteam/tree/master/tool_collections/cufflinks/cuffmerge commit eb18f691975ef9539b5ebd4f118343c8ad967a1f
devteam
parents:
10
diff
changeset
|
98 pass |
1707a530e598
planemo upload for repository https://github.com/galaxyproject/tools-devteam/tree/master/tool_collections/cufflinks/cuffmerge commit eb18f691975ef9539b5ebd4f118343c8ad967a1f
devteam
parents:
10
diff
changeset
|
99 raise Exception(stderr) |
1707a530e598
planemo upload for repository https://github.com/galaxyproject/tools-devteam/tree/master/tool_collections/cufflinks/cuffmerge commit eb18f691975ef9539b5ebd4f118343c8ad967a1f
devteam
parents:
10
diff
changeset
|
100 |
1707a530e598
planemo upload for repository https://github.com/galaxyproject/tools-devteam/tree/master/tool_collections/cufflinks/cuffmerge commit eb18f691975ef9539b5ebd4f118343c8ad967a1f
devteam
parents:
10
diff
changeset
|
101 if len(open("cm_output/merged.gtf", 'r').read().strip()) == 0: |
1707a530e598
planemo upload for repository https://github.com/galaxyproject/tools-devteam/tree/master/tool_collections/cufflinks/cuffmerge commit eb18f691975ef9539b5ebd4f118343c8ad967a1f
devteam
parents:
10
diff
changeset
|
102 raise Exception('The output file is empty, there may be an error with your input file or settings.') |
1707a530e598
planemo upload for repository https://github.com/galaxyproject/tools-devteam/tree/master/tool_collections/cufflinks/cuffmerge commit eb18f691975ef9539b5ebd4f118343c8ad967a1f
devteam
parents:
10
diff
changeset
|
103 |
0 | 104 # Copy outputs. |
12
1707a530e598
planemo upload for repository https://github.com/galaxyproject/tools-devteam/tree/master/tool_collections/cufflinks/cuffmerge commit eb18f691975ef9539b5ebd4f118343c8ad967a1f
devteam
parents:
10
diff
changeset
|
105 shutil.move("cm_output/merged.gtf", options.merged_transcripts) |
1707a530e598
planemo upload for repository https://github.com/galaxyproject/tools-devteam/tree/master/tool_collections/cufflinks/cuffmerge commit eb18f691975ef9539b5ebd4f118343c8ad967a1f
devteam
parents:
10
diff
changeset
|
106 except Exception as e: |
1707a530e598
planemo upload for repository https://github.com/galaxyproject/tools-devteam/tree/master/tool_collections/cufflinks/cuffmerge commit eb18f691975ef9539b5ebd4f118343c8ad967a1f
devteam
parents:
10
diff
changeset
|
107 stop_err('Error running cuffmerge: %s' % e) |
1707a530e598
planemo upload for repository https://github.com/galaxyproject/tools-devteam/tree/master/tool_collections/cufflinks/cuffmerge commit eb18f691975ef9539b5ebd4f118343c8ad967a1f
devteam
parents:
10
diff
changeset
|
108 |
1707a530e598
planemo upload for repository https://github.com/galaxyproject/tools-devteam/tree/master/tool_collections/cufflinks/cuffmerge commit eb18f691975ef9539b5ebd4f118343c8ad967a1f
devteam
parents:
10
diff
changeset
|
109 |
1707a530e598
planemo upload for repository https://github.com/galaxyproject/tools-devteam/tree/master/tool_collections/cufflinks/cuffmerge commit eb18f691975ef9539b5ebd4f118343c8ad967a1f
devteam
parents:
10
diff
changeset
|
110 if __name__ == "__main__": |
1707a530e598
planemo upload for repository https://github.com/galaxyproject/tools-devteam/tree/master/tool_collections/cufflinks/cuffmerge commit eb18f691975ef9539b5ebd4f118343c8ad967a1f
devteam
parents:
10
diff
changeset
|
111 __main__() |