comparison multi_glimmer.py @ 0:0ddb5ee32ff6 draft default tip

planemo upload for repository https://github.com/remimarenco/multi_fasta_glimmerhmm.git commit 28bd73b26b50165eded1d9ba995979acdf005ad1-dirty
author rmarenco
date Thu, 18 Aug 2016 18:50:00 -0400
parents
children
comparison
equal deleted inserted replaced
-1:000000000000 0:0ddb5ee32ff6
1 #!/usr/bin/python
2 # -*- coding: utf8 -*-
3
4 import argparse
5 import os
6 import subprocess
7 import sys
8
9
10 def main():
11 parser = argparse.ArgumentParser(description='Get a multi-fasta, the trained_dir and the output file as inputs, '
12 'to generate GlimmerHMM gene prediction over all contigs')
13
14 parser.add_argument('--multi_fasta', help='Multi fasta file to run GlimmerHMM on', required=True)
15
16 parser.add_argument('--trained_dir', help='Path to the GlimmerHMM trained_dir', required=True)
17
18 parser.add_argument('--output', help='file to output the result into', required=True)
19
20 args = parser.parse_args()
21
22 multi_fasta = args.multi_fasta
23 trained_dir = args.trained_dir
24 # TODO: Temporary fix for the issue with config.file in human/. Next: GC Content to select the appropriate folder
25 if trained_dir.split('/')[-1] == "human":
26 trained_dir = os.path.join(trained_dir, "Train0-43")
27
28 output_file = args.output
29 temp_contig = "temp_contig"
30
31 def exec_glimmer(contig_file, first_time=False):
32 p = subprocess.Popen(["glimmerhmm", contig_file, trained_dir, "-g"],
33 stdout=subprocess.PIPE, stderr=subprocess.PIPE)
34 output, errors = p.communicate()
35
36 p.wait()
37 # Process the error if != "Done"
38 if not errors or (errors.split()[0] != "Done"):
39 raise Exception("Error in glimmer: {0}".format(errors))
40 else:
41 sys.stdout.write(errors)
42 # If not first time, we need to remove the first comments
43 if not first_time:
44 output = "\n".join(output.split("\n")[1:])
45
46 return output
47
48 with open(output_file, 'w+') as o:
49 with open(multi_fasta, 'r') as mf:
50 is_first_time = True
51 for i, line in enumerate(mf):
52 if line[0] == '>':
53 # If it is the first time we finish to read a contig, we let glimmer add the full comments
54 # and write into the output the result
55 if is_first_time is True and i != 0:
56 o.write(exec_glimmer(temp_contig, first_time=is_first_time))
57 is_first_time = False
58 # Else we call glimmer and say this is not the first time (so remove the first comment)
59 # and dump into the output file the result
60 elif i > 0:
61 o.write(exec_glimmer(temp_contig))
62
63 # Because we are on an indication of a beginning of a sequence, we need to create an empty file
64 # to dump the line into
65 with open(temp_contig, 'w+') as tc:
66 tc.write(line)
67 else:
68 # We are in the sequence of a contig, so we append the line in the file
69 with open(temp_contig, 'a+') as tc:
70 tc.write(line)
71 # The file is terminate, we did read another contig so we need to save this last one
72 o.write(exec_glimmer(temp_contig, first_time=is_first_time))
73
74 if __name__ == "__main__":
75 main()