Mercurial > repos > devteam > cuffmerge
annotate cuffmerge_wrapper.py @ 13:cf747d1bd79a draft
planemo upload for repository https://github.com/galaxyproject/tools-devteam/tree/master/tool_collections/cufflinks/cuffmerge commit 82ee6fc860c52c531b7a57bbb346ab1a67a434a5
author | devteam |
---|---|
date | Sun, 19 Feb 2017 12:12:38 -0500 |
parents | 1707a530e598 |
children |
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 # Set/link to sequence file. |
33 if options.use_seq_data: | |
2
5b285b6e4ee3
Update to the new data table specification.
Dave Bouvier <dave@bx.psu.edu>
parents:
0
diff
changeset
|
34 if options.ref_file: |
0 | 35 # Sequence data from history. |
36 # Create symbolic link to ref_file so that index will be created in working directory. | |
37 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
|
38 os.symlink(options.ref_file, seq_path) |
0 | 39 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
|
40 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
|
41 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
|
42 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
|
43 |
0 | 44 # 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
|
45 |
0 | 46 # Base. |
47 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
|
48 |
0 | 49 # Add options. |
50 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
|
51 cmd += (" -p %i " % int(options.num_threads)) |
0 | 52 if options.ref_annotation: |
53 cmd += " -g %s " % options.ref_annotation | |
54 if options.use_seq_data: | |
55 cmd += " -s %s " % seq_path | |
10 | 56 if options.min_isoform_fraction: |
57 cmd += " --min-isoform-fraction %s " % (options.min_isoform_fraction) | |
0 | 58 # 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
|
59 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
|
60 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
|
61 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
|
62 cmd += inputs_file.name |
0 | 63 |
64 # 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
|
65 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
|
66 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
|
67 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
|
68 |
1707a530e598
planemo upload for repository https://github.com/galaxyproject/tools-devteam/tree/master/tool_collections/cufflinks/cuffmerge commit eb18f691975ef9539b5ebd4f118343c8ad967a1f
devteam
parents:
10
diff
changeset
|
69 # 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
|
70 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
|
71 # 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
|
72 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
|
73 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
|
74 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
|
75 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
|
76 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
|
77 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
|
78 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
|
79 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
|
80 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
|
81 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
|
82 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
|
83 |
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 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
|
85 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
|
86 |
0 | 87 # 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
|
88 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
|
89 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
|
90 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
|
91 |
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 |
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 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
|
94 __main__() |