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