Mercurial > repos > yating-l > multi_fasta_glimmer_hmm
annotate multi_glimmer.py @ 3:87f64f4b52e4 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:07:38 -0400 | 
| parents | 36f4ba558f9f | 
| children | 
| rev | line source | 
|---|---|
| 0 
36f4ba558f9f
planemo upload for repository https://github.com/remimarenco/multi_fasta_glimmerhmm.git commit 1245031a7d52c922c86f33ebfd0f20eb9ddf84ac-dirty
 yating-l parents: diff
changeset | 1 #!/usr/bin/python | 
| 
36f4ba558f9f
planemo upload for repository https://github.com/remimarenco/multi_fasta_glimmerhmm.git commit 1245031a7d52c922c86f33ebfd0f20eb9ddf84ac-dirty
 yating-l parents: diff
changeset | 2 # -*- coding: utf8 -*- | 
| 
36f4ba558f9f
planemo upload for repository https://github.com/remimarenco/multi_fasta_glimmerhmm.git commit 1245031a7d52c922c86f33ebfd0f20eb9ddf84ac-dirty
 yating-l parents: diff
changeset | 3 | 
| 
36f4ba558f9f
planemo upload for repository https://github.com/remimarenco/multi_fasta_glimmerhmm.git commit 1245031a7d52c922c86f33ebfd0f20eb9ddf84ac-dirty
 yating-l parents: diff
changeset | 4 import argparse | 
| 
36f4ba558f9f
planemo upload for repository https://github.com/remimarenco/multi_fasta_glimmerhmm.git commit 1245031a7d52c922c86f33ebfd0f20eb9ddf84ac-dirty
 yating-l parents: diff
changeset | 5 import os | 
| 
36f4ba558f9f
planemo upload for repository https://github.com/remimarenco/multi_fasta_glimmerhmm.git commit 1245031a7d52c922c86f33ebfd0f20eb9ddf84ac-dirty
 yating-l parents: diff
changeset | 6 import subprocess | 
| 
36f4ba558f9f
planemo upload for repository https://github.com/remimarenco/multi_fasta_glimmerhmm.git commit 1245031a7d52c922c86f33ebfd0f20eb9ddf84ac-dirty
 yating-l parents: diff
changeset | 7 import sys | 
| 
36f4ba558f9f
planemo upload for repository https://github.com/remimarenco/multi_fasta_glimmerhmm.git commit 1245031a7d52c922c86f33ebfd0f20eb9ddf84ac-dirty
 yating-l parents: diff
changeset | 8 | 
| 
36f4ba558f9f
planemo upload for repository https://github.com/remimarenco/multi_fasta_glimmerhmm.git commit 1245031a7d52c922c86f33ebfd0f20eb9ddf84ac-dirty
 yating-l parents: diff
changeset | 9 | 
| 
36f4ba558f9f
planemo upload for repository https://github.com/remimarenco/multi_fasta_glimmerhmm.git commit 1245031a7d52c922c86f33ebfd0f20eb9ddf84ac-dirty
 yating-l parents: diff
changeset | 10 def main(): | 
| 
36f4ba558f9f
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, ' | 
| 
36f4ba558f9f
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') | 
| 
36f4ba558f9f
planemo upload for repository https://github.com/remimarenco/multi_fasta_glimmerhmm.git commit 1245031a7d52c922c86f33ebfd0f20eb9ddf84ac-dirty
 yating-l parents: diff
changeset | 13 | 
| 
36f4ba558f9f
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) | 
| 
36f4ba558f9f
planemo upload for repository https://github.com/remimarenco/multi_fasta_glimmerhmm.git commit 1245031a7d52c922c86f33ebfd0f20eb9ddf84ac-dirty
 yating-l parents: diff
changeset | 15 | 
| 
36f4ba558f9f
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) | 
| 
36f4ba558f9f
planemo upload for repository https://github.com/remimarenco/multi_fasta_glimmerhmm.git commit 1245031a7d52c922c86f33ebfd0f20eb9ddf84ac-dirty
 yating-l parents: diff
changeset | 17 | 
| 
36f4ba558f9f
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) | 
| 
36f4ba558f9f
planemo upload for repository https://github.com/remimarenco/multi_fasta_glimmerhmm.git commit 1245031a7d52c922c86f33ebfd0f20eb9ddf84ac-dirty
 yating-l parents: diff
changeset | 19 | 
| 
36f4ba558f9f
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() | 
| 
36f4ba558f9f
planemo upload for repository https://github.com/remimarenco/multi_fasta_glimmerhmm.git commit 1245031a7d52c922c86f33ebfd0f20eb9ddf84ac-dirty
 yating-l parents: diff
changeset | 21 | 
| 
36f4ba558f9f
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 | 
| 
36f4ba558f9f
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 | 
| 
36f4ba558f9f
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 | 
| 
36f4ba558f9f
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": | 
| 
36f4ba558f9f
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") | 
| 
36f4ba558f9f
planemo upload for repository https://github.com/remimarenco/multi_fasta_glimmerhmm.git commit 1245031a7d52c922c86f33ebfd0f20eb9ddf84ac-dirty
 yating-l parents: diff
changeset | 27 | 
| 
36f4ba558f9f
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 | 
| 
36f4ba558f9f
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" | 
| 
36f4ba558f9f
planemo upload for repository https://github.com/remimarenco/multi_fasta_glimmerhmm.git commit 1245031a7d52c922c86f33ebfd0f20eb9ddf84ac-dirty
 yating-l parents: diff
changeset | 30 | 
| 
36f4ba558f9f
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): | 
| 
36f4ba558f9f
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"], | 
| 
36f4ba558f9f
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) | 
| 
36f4ba558f9f
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() | 
| 
36f4ba558f9f
planemo upload for repository https://github.com/remimarenco/multi_fasta_glimmerhmm.git commit 1245031a7d52c922c86f33ebfd0f20eb9ddf84ac-dirty
 yating-l parents: diff
changeset | 35 | 
| 
36f4ba558f9f
planemo upload for repository https://github.com/remimarenco/multi_fasta_glimmerhmm.git commit 1245031a7d52c922c86f33ebfd0f20eb9ddf84ac-dirty
 yating-l parents: diff
changeset | 36 p.wait() | 
| 
36f4ba558f9f
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" | 
| 
36f4ba558f9f
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"): | 
| 
36f4ba558f9f
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)) | 
| 
36f4ba558f9f
planemo upload for repository https://github.com/remimarenco/multi_fasta_glimmerhmm.git commit 1245031a7d52c922c86f33ebfd0f20eb9ddf84ac-dirty
 yating-l parents: diff
changeset | 40 else: | 
| 
36f4ba558f9f
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) | 
| 
36f4ba558f9f
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 | 
| 
36f4ba558f9f
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: | 
| 
36f4ba558f9f
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:]) | 
| 
36f4ba558f9f
planemo upload for repository https://github.com/remimarenco/multi_fasta_glimmerhmm.git commit 1245031a7d52c922c86f33ebfd0f20eb9ddf84ac-dirty
 yating-l parents: diff
changeset | 45 | 
| 
36f4ba558f9f
planemo upload for repository https://github.com/remimarenco/multi_fasta_glimmerhmm.git commit 1245031a7d52c922c86f33ebfd0f20eb9ddf84ac-dirty
 yating-l parents: diff
changeset | 46 return output | 
| 
36f4ba558f9f
planemo upload for repository https://github.com/remimarenco/multi_fasta_glimmerhmm.git commit 1245031a7d52c922c86f33ebfd0f20eb9ddf84ac-dirty
 yating-l parents: diff
changeset | 47 | 
| 
36f4ba558f9f
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: | 
| 
36f4ba558f9f
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: | 
| 
36f4ba558f9f
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 | 
| 
36f4ba558f9f
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): | 
| 
36f4ba558f9f
planemo upload for repository https://github.com/remimarenco/multi_fasta_glimmerhmm.git commit 1245031a7d52c922c86f33ebfd0f20eb9ddf84ac-dirty
 yating-l parents: diff
changeset | 52 if line[0] == '>': | 
| 
36f4ba558f9f
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 | 
| 
36f4ba558f9f
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 | 
| 
36f4ba558f9f
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: | 
| 
36f4ba558f9f
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)) | 
| 
36f4ba558f9f
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 | 
| 
36f4ba558f9f
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) | 
| 
36f4ba558f9f
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 | 
| 
36f4ba558f9f
planemo upload for repository https://github.com/remimarenco/multi_fasta_glimmerhmm.git commit 1245031a7d52c922c86f33ebfd0f20eb9ddf84ac-dirty
 yating-l parents: diff
changeset | 60 elif i > 0: | 
| 
36f4ba558f9f
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)) | 
| 
36f4ba558f9f
planemo upload for repository https://github.com/remimarenco/multi_fasta_glimmerhmm.git commit 1245031a7d52c922c86f33ebfd0f20eb9ddf84ac-dirty
 yating-l parents: diff
changeset | 62 | 
| 
36f4ba558f9f
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 | 
| 
36f4ba558f9f
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 | 
| 
36f4ba558f9f
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: | 
| 
36f4ba558f9f
planemo upload for repository https://github.com/remimarenco/multi_fasta_glimmerhmm.git commit 1245031a7d52c922c86f33ebfd0f20eb9ddf84ac-dirty
 yating-l parents: diff
changeset | 66 tc.write(line) | 
| 
36f4ba558f9f
planemo upload for repository https://github.com/remimarenco/multi_fasta_glimmerhmm.git commit 1245031a7d52c922c86f33ebfd0f20eb9ddf84ac-dirty
 yating-l parents: diff
changeset | 67 else: | 
| 
36f4ba558f9f
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 | 
| 
36f4ba558f9f
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: | 
| 
36f4ba558f9f
planemo upload for repository https://github.com/remimarenco/multi_fasta_glimmerhmm.git commit 1245031a7d52c922c86f33ebfd0f20eb9ddf84ac-dirty
 yating-l parents: diff
changeset | 70 tc.write(line) | 
| 
36f4ba558f9f
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 | 
| 
36f4ba558f9f
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)) | 
| 
36f4ba558f9f
planemo upload for repository https://github.com/remimarenco/multi_fasta_glimmerhmm.git commit 1245031a7d52c922c86f33ebfd0f20eb9ddf84ac-dirty
 yating-l parents: diff
changeset | 73 | 
| 
36f4ba558f9f
planemo upload for repository https://github.com/remimarenco/multi_fasta_glimmerhmm.git commit 1245031a7d52c922c86f33ebfd0f20eb9ddf84ac-dirty
 yating-l parents: diff
changeset | 74 if __name__ == "__main__": | 
| 
36f4ba558f9f
planemo upload for repository https://github.com/remimarenco/multi_fasta_glimmerhmm.git commit 1245031a7d52c922c86f33ebfd0f20eb9ddf84ac-dirty
 yating-l parents: diff
changeset | 75 main() | 
