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