Mercurial > repos > bgruening > glimmer_hmm
annotate glimmerHMM/BCBio/GFF/GFFOutput.py @ 1:4da91bb244dc draft
planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/glimmer_hmm commit 2effed877a778e455c63a76e994a0f2bb8f4dba0
author | rmarenco |
---|---|
date | Thu, 14 Jul 2016 15:11:33 -0400 |
parents | c9699375fcf6 |
children |
rev | line source |
---|---|
0
c9699375fcf6
planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/glimmer_hmm commit 0dc67759bcbdf5a8a285ded9ba751340d741fe63
bgruening
parents:
diff
changeset
|
1 """Output Biopython SeqRecords and SeqFeatures to GFF3 format. |
c9699375fcf6
planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/glimmer_hmm commit 0dc67759bcbdf5a8a285ded9ba751340d741fe63
bgruening
parents:
diff
changeset
|
2 |
c9699375fcf6
planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/glimmer_hmm commit 0dc67759bcbdf5a8a285ded9ba751340d741fe63
bgruening
parents:
diff
changeset
|
3 The target format is GFF3, the current GFF standard: |
c9699375fcf6
planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/glimmer_hmm commit 0dc67759bcbdf5a8a285ded9ba751340d741fe63
bgruening
parents:
diff
changeset
|
4 http://www.sequenceontology.org/gff3.shtml |
c9699375fcf6
planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/glimmer_hmm commit 0dc67759bcbdf5a8a285ded9ba751340d741fe63
bgruening
parents:
diff
changeset
|
5 """ |
c9699375fcf6
planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/glimmer_hmm commit 0dc67759bcbdf5a8a285ded9ba751340d741fe63
bgruening
parents:
diff
changeset
|
6 import urllib |
c9699375fcf6
planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/glimmer_hmm commit 0dc67759bcbdf5a8a285ded9ba751340d741fe63
bgruening
parents:
diff
changeset
|
7 |
c9699375fcf6
planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/glimmer_hmm commit 0dc67759bcbdf5a8a285ded9ba751340d741fe63
bgruening
parents:
diff
changeset
|
8 class _IdHandler: |
c9699375fcf6
planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/glimmer_hmm commit 0dc67759bcbdf5a8a285ded9ba751340d741fe63
bgruening
parents:
diff
changeset
|
9 """Generate IDs for GFF3 Parent/Child relationships where they don't exist. |
c9699375fcf6
planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/glimmer_hmm commit 0dc67759bcbdf5a8a285ded9ba751340d741fe63
bgruening
parents:
diff
changeset
|
10 """ |
c9699375fcf6
planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/glimmer_hmm commit 0dc67759bcbdf5a8a285ded9ba751340d741fe63
bgruening
parents:
diff
changeset
|
11 def __init__(self): |
c9699375fcf6
planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/glimmer_hmm commit 0dc67759bcbdf5a8a285ded9ba751340d741fe63
bgruening
parents:
diff
changeset
|
12 self._prefix = "biopygen" |
c9699375fcf6
planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/glimmer_hmm commit 0dc67759bcbdf5a8a285ded9ba751340d741fe63
bgruening
parents:
diff
changeset
|
13 self._counter = 1 |
c9699375fcf6
planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/glimmer_hmm commit 0dc67759bcbdf5a8a285ded9ba751340d741fe63
bgruening
parents:
diff
changeset
|
14 self._seen_ids = [] |
c9699375fcf6
planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/glimmer_hmm commit 0dc67759bcbdf5a8a285ded9ba751340d741fe63
bgruening
parents:
diff
changeset
|
15 |
c9699375fcf6
planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/glimmer_hmm commit 0dc67759bcbdf5a8a285ded9ba751340d741fe63
bgruening
parents:
diff
changeset
|
16 def _generate_id(self, quals): |
c9699375fcf6
planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/glimmer_hmm commit 0dc67759bcbdf5a8a285ded9ba751340d741fe63
bgruening
parents:
diff
changeset
|
17 """Generate a unique ID not present in our existing IDs. |
c9699375fcf6
planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/glimmer_hmm commit 0dc67759bcbdf5a8a285ded9ba751340d741fe63
bgruening
parents:
diff
changeset
|
18 """ |
c9699375fcf6
planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/glimmer_hmm commit 0dc67759bcbdf5a8a285ded9ba751340d741fe63
bgruening
parents:
diff
changeset
|
19 gen_id = self._get_standard_id(quals) |
c9699375fcf6
planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/glimmer_hmm commit 0dc67759bcbdf5a8a285ded9ba751340d741fe63
bgruening
parents:
diff
changeset
|
20 if gen_id is None: |
c9699375fcf6
planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/glimmer_hmm commit 0dc67759bcbdf5a8a285ded9ba751340d741fe63
bgruening
parents:
diff
changeset
|
21 while 1: |
c9699375fcf6
planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/glimmer_hmm commit 0dc67759bcbdf5a8a285ded9ba751340d741fe63
bgruening
parents:
diff
changeset
|
22 gen_id = "%s%s" % (self._prefix, self._counter) |
c9699375fcf6
planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/glimmer_hmm commit 0dc67759bcbdf5a8a285ded9ba751340d741fe63
bgruening
parents:
diff
changeset
|
23 if gen_id not in self._seen_ids: |
c9699375fcf6
planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/glimmer_hmm commit 0dc67759bcbdf5a8a285ded9ba751340d741fe63
bgruening
parents:
diff
changeset
|
24 break |
c9699375fcf6
planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/glimmer_hmm commit 0dc67759bcbdf5a8a285ded9ba751340d741fe63
bgruening
parents:
diff
changeset
|
25 self._counter += 1 |
c9699375fcf6
planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/glimmer_hmm commit 0dc67759bcbdf5a8a285ded9ba751340d741fe63
bgruening
parents:
diff
changeset
|
26 return gen_id |
c9699375fcf6
planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/glimmer_hmm commit 0dc67759bcbdf5a8a285ded9ba751340d741fe63
bgruening
parents:
diff
changeset
|
27 |
c9699375fcf6
planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/glimmer_hmm commit 0dc67759bcbdf5a8a285ded9ba751340d741fe63
bgruening
parents:
diff
changeset
|
28 def _get_standard_id(self, quals): |
c9699375fcf6
planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/glimmer_hmm commit 0dc67759bcbdf5a8a285ded9ba751340d741fe63
bgruening
parents:
diff
changeset
|
29 """Retrieve standardized IDs from other sources like NCBI GenBank. |
c9699375fcf6
planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/glimmer_hmm commit 0dc67759bcbdf5a8a285ded9ba751340d741fe63
bgruening
parents:
diff
changeset
|
30 |
c9699375fcf6
planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/glimmer_hmm commit 0dc67759bcbdf5a8a285ded9ba751340d741fe63
bgruening
parents:
diff
changeset
|
31 This tries to find IDs from known key/values when stored differently |
c9699375fcf6
planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/glimmer_hmm commit 0dc67759bcbdf5a8a285ded9ba751340d741fe63
bgruening
parents:
diff
changeset
|
32 than GFF3 specifications. |
c9699375fcf6
planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/glimmer_hmm commit 0dc67759bcbdf5a8a285ded9ba751340d741fe63
bgruening
parents:
diff
changeset
|
33 """ |
c9699375fcf6
planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/glimmer_hmm commit 0dc67759bcbdf5a8a285ded9ba751340d741fe63
bgruening
parents:
diff
changeset
|
34 possible_keys = ["transcript_id", "protein_id"] |
c9699375fcf6
planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/glimmer_hmm commit 0dc67759bcbdf5a8a285ded9ba751340d741fe63
bgruening
parents:
diff
changeset
|
35 for test_key in possible_keys: |
c9699375fcf6
planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/glimmer_hmm commit 0dc67759bcbdf5a8a285ded9ba751340d741fe63
bgruening
parents:
diff
changeset
|
36 if quals.has_key(test_key): |
c9699375fcf6
planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/glimmer_hmm commit 0dc67759bcbdf5a8a285ded9ba751340d741fe63
bgruening
parents:
diff
changeset
|
37 cur_id = quals[test_key] |
c9699375fcf6
planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/glimmer_hmm commit 0dc67759bcbdf5a8a285ded9ba751340d741fe63
bgruening
parents:
diff
changeset
|
38 if isinstance(cur_id, tuple) or isinstance(cur_id, list): |
c9699375fcf6
planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/glimmer_hmm commit 0dc67759bcbdf5a8a285ded9ba751340d741fe63
bgruening
parents:
diff
changeset
|
39 return cur_id[0] |
c9699375fcf6
planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/glimmer_hmm commit 0dc67759bcbdf5a8a285ded9ba751340d741fe63
bgruening
parents:
diff
changeset
|
40 else: |
c9699375fcf6
planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/glimmer_hmm commit 0dc67759bcbdf5a8a285ded9ba751340d741fe63
bgruening
parents:
diff
changeset
|
41 return cur_id |
c9699375fcf6
planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/glimmer_hmm commit 0dc67759bcbdf5a8a285ded9ba751340d741fe63
bgruening
parents:
diff
changeset
|
42 return None |
c9699375fcf6
planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/glimmer_hmm commit 0dc67759bcbdf5a8a285ded9ba751340d741fe63
bgruening
parents:
diff
changeset
|
43 |
c9699375fcf6
planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/glimmer_hmm commit 0dc67759bcbdf5a8a285ded9ba751340d741fe63
bgruening
parents:
diff
changeset
|
44 def update_quals(self, quals, has_children): |
c9699375fcf6
planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/glimmer_hmm commit 0dc67759bcbdf5a8a285ded9ba751340d741fe63
bgruening
parents:
diff
changeset
|
45 """Update a set of qualifiers, adding an ID if necessary. |
c9699375fcf6
planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/glimmer_hmm commit 0dc67759bcbdf5a8a285ded9ba751340d741fe63
bgruening
parents:
diff
changeset
|
46 """ |
c9699375fcf6
planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/glimmer_hmm commit 0dc67759bcbdf5a8a285ded9ba751340d741fe63
bgruening
parents:
diff
changeset
|
47 cur_id = quals.get("ID", None) |
c9699375fcf6
planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/glimmer_hmm commit 0dc67759bcbdf5a8a285ded9ba751340d741fe63
bgruening
parents:
diff
changeset
|
48 # if we have an ID, record it |
c9699375fcf6
planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/glimmer_hmm commit 0dc67759bcbdf5a8a285ded9ba751340d741fe63
bgruening
parents:
diff
changeset
|
49 if cur_id: |
c9699375fcf6
planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/glimmer_hmm commit 0dc67759bcbdf5a8a285ded9ba751340d741fe63
bgruening
parents:
diff
changeset
|
50 if not isinstance(cur_id, list) and not isinstance(cur_id, tuple): |
c9699375fcf6
planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/glimmer_hmm commit 0dc67759bcbdf5a8a285ded9ba751340d741fe63
bgruening
parents:
diff
changeset
|
51 cur_id = [cur_id] |
c9699375fcf6
planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/glimmer_hmm commit 0dc67759bcbdf5a8a285ded9ba751340d741fe63
bgruening
parents:
diff
changeset
|
52 for add_id in cur_id: |
c9699375fcf6
planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/glimmer_hmm commit 0dc67759bcbdf5a8a285ded9ba751340d741fe63
bgruening
parents:
diff
changeset
|
53 self._seen_ids.append(add_id) |
c9699375fcf6
planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/glimmer_hmm commit 0dc67759bcbdf5a8a285ded9ba751340d741fe63
bgruening
parents:
diff
changeset
|
54 # if we need one and don't have it, create a new one |
c9699375fcf6
planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/glimmer_hmm commit 0dc67759bcbdf5a8a285ded9ba751340d741fe63
bgruening
parents:
diff
changeset
|
55 elif has_children: |
c9699375fcf6
planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/glimmer_hmm commit 0dc67759bcbdf5a8a285ded9ba751340d741fe63
bgruening
parents:
diff
changeset
|
56 new_id = self._generate_id(quals) |
c9699375fcf6
planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/glimmer_hmm commit 0dc67759bcbdf5a8a285ded9ba751340d741fe63
bgruening
parents:
diff
changeset
|
57 self._seen_ids.append(new_id) |
c9699375fcf6
planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/glimmer_hmm commit 0dc67759bcbdf5a8a285ded9ba751340d741fe63
bgruening
parents:
diff
changeset
|
58 quals["ID"] = [new_id] |
c9699375fcf6
planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/glimmer_hmm commit 0dc67759bcbdf5a8a285ded9ba751340d741fe63
bgruening
parents:
diff
changeset
|
59 return quals |
c9699375fcf6
planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/glimmer_hmm commit 0dc67759bcbdf5a8a285ded9ba751340d741fe63
bgruening
parents:
diff
changeset
|
60 |
c9699375fcf6
planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/glimmer_hmm commit 0dc67759bcbdf5a8a285ded9ba751340d741fe63
bgruening
parents:
diff
changeset
|
61 class GFF3Writer: |
c9699375fcf6
planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/glimmer_hmm commit 0dc67759bcbdf5a8a285ded9ba751340d741fe63
bgruening
parents:
diff
changeset
|
62 """Write GFF3 files starting with standard Biopython objects. |
c9699375fcf6
planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/glimmer_hmm commit 0dc67759bcbdf5a8a285ded9ba751340d741fe63
bgruening
parents:
diff
changeset
|
63 """ |
c9699375fcf6
planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/glimmer_hmm commit 0dc67759bcbdf5a8a285ded9ba751340d741fe63
bgruening
parents:
diff
changeset
|
64 def __init__(self): |
c9699375fcf6
planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/glimmer_hmm commit 0dc67759bcbdf5a8a285ded9ba751340d741fe63
bgruening
parents:
diff
changeset
|
65 pass |
c9699375fcf6
planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/glimmer_hmm commit 0dc67759bcbdf5a8a285ded9ba751340d741fe63
bgruening
parents:
diff
changeset
|
66 |
c9699375fcf6
planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/glimmer_hmm commit 0dc67759bcbdf5a8a285ded9ba751340d741fe63
bgruening
parents:
diff
changeset
|
67 def write(self, recs, out_handle): |
c9699375fcf6
planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/glimmer_hmm commit 0dc67759bcbdf5a8a285ded9ba751340d741fe63
bgruening
parents:
diff
changeset
|
68 """Write the provided records to the given handle in GFF3 format. |
c9699375fcf6
planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/glimmer_hmm commit 0dc67759bcbdf5a8a285ded9ba751340d741fe63
bgruening
parents:
diff
changeset
|
69 """ |
c9699375fcf6
planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/glimmer_hmm commit 0dc67759bcbdf5a8a285ded9ba751340d741fe63
bgruening
parents:
diff
changeset
|
70 id_handler = _IdHandler() |
c9699375fcf6
planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/glimmer_hmm commit 0dc67759bcbdf5a8a285ded9ba751340d741fe63
bgruening
parents:
diff
changeset
|
71 self._write_header(out_handle) |
c9699375fcf6
planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/glimmer_hmm commit 0dc67759bcbdf5a8a285ded9ba751340d741fe63
bgruening
parents:
diff
changeset
|
72 for rec in recs: |
c9699375fcf6
planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/glimmer_hmm commit 0dc67759bcbdf5a8a285ded9ba751340d741fe63
bgruening
parents:
diff
changeset
|
73 self._write_rec(rec, out_handle) |
c9699375fcf6
planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/glimmer_hmm commit 0dc67759bcbdf5a8a285ded9ba751340d741fe63
bgruening
parents:
diff
changeset
|
74 self._write_annotations(rec.annotations, rec.id, out_handle) |
c9699375fcf6
planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/glimmer_hmm commit 0dc67759bcbdf5a8a285ded9ba751340d741fe63
bgruening
parents:
diff
changeset
|
75 for sf in rec.features: |
c9699375fcf6
planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/glimmer_hmm commit 0dc67759bcbdf5a8a285ded9ba751340d741fe63
bgruening
parents:
diff
changeset
|
76 sf = self._clean_feature(sf) |
c9699375fcf6
planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/glimmer_hmm commit 0dc67759bcbdf5a8a285ded9ba751340d741fe63
bgruening
parents:
diff
changeset
|
77 id_handler = self._write_feature(sf, rec.id, out_handle, |
c9699375fcf6
planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/glimmer_hmm commit 0dc67759bcbdf5a8a285ded9ba751340d741fe63
bgruening
parents:
diff
changeset
|
78 id_handler) |
c9699375fcf6
planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/glimmer_hmm commit 0dc67759bcbdf5a8a285ded9ba751340d741fe63
bgruening
parents:
diff
changeset
|
79 |
c9699375fcf6
planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/glimmer_hmm commit 0dc67759bcbdf5a8a285ded9ba751340d741fe63
bgruening
parents:
diff
changeset
|
80 def _clean_feature(self, feature): |
c9699375fcf6
planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/glimmer_hmm commit 0dc67759bcbdf5a8a285ded9ba751340d741fe63
bgruening
parents:
diff
changeset
|
81 quals = {} |
c9699375fcf6
planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/glimmer_hmm commit 0dc67759bcbdf5a8a285ded9ba751340d741fe63
bgruening
parents:
diff
changeset
|
82 for key, val in feature.qualifiers.items(): |
c9699375fcf6
planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/glimmer_hmm commit 0dc67759bcbdf5a8a285ded9ba751340d741fe63
bgruening
parents:
diff
changeset
|
83 if not isinstance(val, (list, tuple)): |
c9699375fcf6
planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/glimmer_hmm commit 0dc67759bcbdf5a8a285ded9ba751340d741fe63
bgruening
parents:
diff
changeset
|
84 val = [val] |
c9699375fcf6
planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/glimmer_hmm commit 0dc67759bcbdf5a8a285ded9ba751340d741fe63
bgruening
parents:
diff
changeset
|
85 val = [str(x) for x in val] |
c9699375fcf6
planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/glimmer_hmm commit 0dc67759bcbdf5a8a285ded9ba751340d741fe63
bgruening
parents:
diff
changeset
|
86 quals[key] = val |
c9699375fcf6
planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/glimmer_hmm commit 0dc67759bcbdf5a8a285ded9ba751340d741fe63
bgruening
parents:
diff
changeset
|
87 feature.qualifiers = quals |
c9699375fcf6
planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/glimmer_hmm commit 0dc67759bcbdf5a8a285ded9ba751340d741fe63
bgruening
parents:
diff
changeset
|
88 clean_sub = [self._clean_feature(f) for f in feature.sub_features] |
c9699375fcf6
planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/glimmer_hmm commit 0dc67759bcbdf5a8a285ded9ba751340d741fe63
bgruening
parents:
diff
changeset
|
89 feature.sub_features = clean_sub |
c9699375fcf6
planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/glimmer_hmm commit 0dc67759bcbdf5a8a285ded9ba751340d741fe63
bgruening
parents:
diff
changeset
|
90 return feature |
c9699375fcf6
planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/glimmer_hmm commit 0dc67759bcbdf5a8a285ded9ba751340d741fe63
bgruening
parents:
diff
changeset
|
91 |
c9699375fcf6
planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/glimmer_hmm commit 0dc67759bcbdf5a8a285ded9ba751340d741fe63
bgruening
parents:
diff
changeset
|
92 def _write_rec(self, rec, out_handle): |
c9699375fcf6
planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/glimmer_hmm commit 0dc67759bcbdf5a8a285ded9ba751340d741fe63
bgruening
parents:
diff
changeset
|
93 # if we have a SeqRecord, write out optional directive |
c9699375fcf6
planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/glimmer_hmm commit 0dc67759bcbdf5a8a285ded9ba751340d741fe63
bgruening
parents:
diff
changeset
|
94 if len(rec.seq) > 0: |
c9699375fcf6
planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/glimmer_hmm commit 0dc67759bcbdf5a8a285ded9ba751340d741fe63
bgruening
parents:
diff
changeset
|
95 out_handle.write("##sequence-region %s 1 %s\n" % (rec.id, len(rec.seq))) |
c9699375fcf6
planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/glimmer_hmm commit 0dc67759bcbdf5a8a285ded9ba751340d741fe63
bgruening
parents:
diff
changeset
|
96 |
c9699375fcf6
planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/glimmer_hmm commit 0dc67759bcbdf5a8a285ded9ba751340d741fe63
bgruening
parents:
diff
changeset
|
97 def _write_feature(self, feature, rec_id, out_handle, id_handler, |
c9699375fcf6
planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/glimmer_hmm commit 0dc67759bcbdf5a8a285ded9ba751340d741fe63
bgruening
parents:
diff
changeset
|
98 parent_id=None): |
c9699375fcf6
planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/glimmer_hmm commit 0dc67759bcbdf5a8a285ded9ba751340d741fe63
bgruening
parents:
diff
changeset
|
99 """Write a feature with location information. |
c9699375fcf6
planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/glimmer_hmm commit 0dc67759bcbdf5a8a285ded9ba751340d741fe63
bgruening
parents:
diff
changeset
|
100 """ |
c9699375fcf6
planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/glimmer_hmm commit 0dc67759bcbdf5a8a285ded9ba751340d741fe63
bgruening
parents:
diff
changeset
|
101 if feature.strand == 1: |
c9699375fcf6
planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/glimmer_hmm commit 0dc67759bcbdf5a8a285ded9ba751340d741fe63
bgruening
parents:
diff
changeset
|
102 strand = '+' |
c9699375fcf6
planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/glimmer_hmm commit 0dc67759bcbdf5a8a285ded9ba751340d741fe63
bgruening
parents:
diff
changeset
|
103 elif feature.strand == -1: |
c9699375fcf6
planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/glimmer_hmm commit 0dc67759bcbdf5a8a285ded9ba751340d741fe63
bgruening
parents:
diff
changeset
|
104 strand = '-' |
c9699375fcf6
planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/glimmer_hmm commit 0dc67759bcbdf5a8a285ded9ba751340d741fe63
bgruening
parents:
diff
changeset
|
105 else: |
c9699375fcf6
planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/glimmer_hmm commit 0dc67759bcbdf5a8a285ded9ba751340d741fe63
bgruening
parents:
diff
changeset
|
106 strand = '.' |
c9699375fcf6
planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/glimmer_hmm commit 0dc67759bcbdf5a8a285ded9ba751340d741fe63
bgruening
parents:
diff
changeset
|
107 # remove any standard features from the qualifiers |
c9699375fcf6
planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/glimmer_hmm commit 0dc67759bcbdf5a8a285ded9ba751340d741fe63
bgruening
parents:
diff
changeset
|
108 quals = feature.qualifiers.copy() |
c9699375fcf6
planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/glimmer_hmm commit 0dc67759bcbdf5a8a285ded9ba751340d741fe63
bgruening
parents:
diff
changeset
|
109 for std_qual in ["source", "score", "phase"]: |
c9699375fcf6
planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/glimmer_hmm commit 0dc67759bcbdf5a8a285ded9ba751340d741fe63
bgruening
parents:
diff
changeset
|
110 if quals.has_key(std_qual) and len(quals[std_qual]) == 1: |
c9699375fcf6
planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/glimmer_hmm commit 0dc67759bcbdf5a8a285ded9ba751340d741fe63
bgruening
parents:
diff
changeset
|
111 del quals[std_qual] |
c9699375fcf6
planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/glimmer_hmm commit 0dc67759bcbdf5a8a285ded9ba751340d741fe63
bgruening
parents:
diff
changeset
|
112 # add a link to a parent identifier if it exists |
c9699375fcf6
planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/glimmer_hmm commit 0dc67759bcbdf5a8a285ded9ba751340d741fe63
bgruening
parents:
diff
changeset
|
113 if parent_id: |
c9699375fcf6
planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/glimmer_hmm commit 0dc67759bcbdf5a8a285ded9ba751340d741fe63
bgruening
parents:
diff
changeset
|
114 if not quals.has_key("Parent"): |
c9699375fcf6
planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/glimmer_hmm commit 0dc67759bcbdf5a8a285ded9ba751340d741fe63
bgruening
parents:
diff
changeset
|
115 quals["Parent"] = [] |
c9699375fcf6
planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/glimmer_hmm commit 0dc67759bcbdf5a8a285ded9ba751340d741fe63
bgruening
parents:
diff
changeset
|
116 quals["Parent"].append(parent_id) |
c9699375fcf6
planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/glimmer_hmm commit 0dc67759bcbdf5a8a285ded9ba751340d741fe63
bgruening
parents:
diff
changeset
|
117 quals = id_handler.update_quals(quals, len(feature.sub_features) > 0) |
c9699375fcf6
planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/glimmer_hmm commit 0dc67759bcbdf5a8a285ded9ba751340d741fe63
bgruening
parents:
diff
changeset
|
118 if feature.type: |
c9699375fcf6
planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/glimmer_hmm commit 0dc67759bcbdf5a8a285ded9ba751340d741fe63
bgruening
parents:
diff
changeset
|
119 ftype = feature.type |
c9699375fcf6
planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/glimmer_hmm commit 0dc67759bcbdf5a8a285ded9ba751340d741fe63
bgruening
parents:
diff
changeset
|
120 else: |
c9699375fcf6
planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/glimmer_hmm commit 0dc67759bcbdf5a8a285ded9ba751340d741fe63
bgruening
parents:
diff
changeset
|
121 ftype = "sequence_feature" |
c9699375fcf6
planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/glimmer_hmm commit 0dc67759bcbdf5a8a285ded9ba751340d741fe63
bgruening
parents:
diff
changeset
|
122 parts = [str(rec_id), |
c9699375fcf6
planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/glimmer_hmm commit 0dc67759bcbdf5a8a285ded9ba751340d741fe63
bgruening
parents:
diff
changeset
|
123 feature.qualifiers.get("source", ["feature"])[0], |
c9699375fcf6
planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/glimmer_hmm commit 0dc67759bcbdf5a8a285ded9ba751340d741fe63
bgruening
parents:
diff
changeset
|
124 ftype, |
c9699375fcf6
planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/glimmer_hmm commit 0dc67759bcbdf5a8a285ded9ba751340d741fe63
bgruening
parents:
diff
changeset
|
125 str(feature.location.nofuzzy_start + 1), # 1-based indexing |
c9699375fcf6
planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/glimmer_hmm commit 0dc67759bcbdf5a8a285ded9ba751340d741fe63
bgruening
parents:
diff
changeset
|
126 str(feature.location.nofuzzy_end), |
c9699375fcf6
planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/glimmer_hmm commit 0dc67759bcbdf5a8a285ded9ba751340d741fe63
bgruening
parents:
diff
changeset
|
127 feature.qualifiers.get("score", ["."])[0], |
c9699375fcf6
planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/glimmer_hmm commit 0dc67759bcbdf5a8a285ded9ba751340d741fe63
bgruening
parents:
diff
changeset
|
128 strand, |
c9699375fcf6
planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/glimmer_hmm commit 0dc67759bcbdf5a8a285ded9ba751340d741fe63
bgruening
parents:
diff
changeset
|
129 str(feature.qualifiers.get("phase", ["."])[0]), |
c9699375fcf6
planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/glimmer_hmm commit 0dc67759bcbdf5a8a285ded9ba751340d741fe63
bgruening
parents:
diff
changeset
|
130 self._format_keyvals(quals)] |
c9699375fcf6
planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/glimmer_hmm commit 0dc67759bcbdf5a8a285ded9ba751340d741fe63
bgruening
parents:
diff
changeset
|
131 out_handle.write("\t".join(parts) + "\n") |
c9699375fcf6
planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/glimmer_hmm commit 0dc67759bcbdf5a8a285ded9ba751340d741fe63
bgruening
parents:
diff
changeset
|
132 for sub_feature in feature.sub_features: |
c9699375fcf6
planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/glimmer_hmm commit 0dc67759bcbdf5a8a285ded9ba751340d741fe63
bgruening
parents:
diff
changeset
|
133 id_handler = self._write_feature(sub_feature, rec_id, out_handle, |
c9699375fcf6
planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/glimmer_hmm commit 0dc67759bcbdf5a8a285ded9ba751340d741fe63
bgruening
parents:
diff
changeset
|
134 id_handler, quals["ID"][0]) |
c9699375fcf6
planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/glimmer_hmm commit 0dc67759bcbdf5a8a285ded9ba751340d741fe63
bgruening
parents:
diff
changeset
|
135 return id_handler |
c9699375fcf6
planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/glimmer_hmm commit 0dc67759bcbdf5a8a285ded9ba751340d741fe63
bgruening
parents:
diff
changeset
|
136 |
c9699375fcf6
planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/glimmer_hmm commit 0dc67759bcbdf5a8a285ded9ba751340d741fe63
bgruening
parents:
diff
changeset
|
137 def _format_keyvals(self, keyvals): |
c9699375fcf6
planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/glimmer_hmm commit 0dc67759bcbdf5a8a285ded9ba751340d741fe63
bgruening
parents:
diff
changeset
|
138 format_kvs = [] |
c9699375fcf6
planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/glimmer_hmm commit 0dc67759bcbdf5a8a285ded9ba751340d741fe63
bgruening
parents:
diff
changeset
|
139 for key, values in keyvals.items(): |
c9699375fcf6
planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/glimmer_hmm commit 0dc67759bcbdf5a8a285ded9ba751340d741fe63
bgruening
parents:
diff
changeset
|
140 key = key.strip() |
c9699375fcf6
planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/glimmer_hmm commit 0dc67759bcbdf5a8a285ded9ba751340d741fe63
bgruening
parents:
diff
changeset
|
141 format_vals = [] |
c9699375fcf6
planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/glimmer_hmm commit 0dc67759bcbdf5a8a285ded9ba751340d741fe63
bgruening
parents:
diff
changeset
|
142 if not isinstance(values, list) or isinstance(values, tuple): |
c9699375fcf6
planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/glimmer_hmm commit 0dc67759bcbdf5a8a285ded9ba751340d741fe63
bgruening
parents:
diff
changeset
|
143 values = [values] |
c9699375fcf6
planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/glimmer_hmm commit 0dc67759bcbdf5a8a285ded9ba751340d741fe63
bgruening
parents:
diff
changeset
|
144 for val in values: |
c9699375fcf6
planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/glimmer_hmm commit 0dc67759bcbdf5a8a285ded9ba751340d741fe63
bgruening
parents:
diff
changeset
|
145 val = urllib.quote(str(val).strip()) |
c9699375fcf6
planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/glimmer_hmm commit 0dc67759bcbdf5a8a285ded9ba751340d741fe63
bgruening
parents:
diff
changeset
|
146 if ((key and val) and val not in format_vals): |
c9699375fcf6
planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/glimmer_hmm commit 0dc67759bcbdf5a8a285ded9ba751340d741fe63
bgruening
parents:
diff
changeset
|
147 format_vals.append(val) |
c9699375fcf6
planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/glimmer_hmm commit 0dc67759bcbdf5a8a285ded9ba751340d741fe63
bgruening
parents:
diff
changeset
|
148 format_kvs.append("%s=%s" % (key, ",".join(format_vals))) |
c9699375fcf6
planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/glimmer_hmm commit 0dc67759bcbdf5a8a285ded9ba751340d741fe63
bgruening
parents:
diff
changeset
|
149 return ";".join(format_kvs) |
c9699375fcf6
planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/glimmer_hmm commit 0dc67759bcbdf5a8a285ded9ba751340d741fe63
bgruening
parents:
diff
changeset
|
150 |
c9699375fcf6
planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/glimmer_hmm commit 0dc67759bcbdf5a8a285ded9ba751340d741fe63
bgruening
parents:
diff
changeset
|
151 def _write_annotations(self, anns, rec_id, out_handle): |
c9699375fcf6
planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/glimmer_hmm commit 0dc67759bcbdf5a8a285ded9ba751340d741fe63
bgruening
parents:
diff
changeset
|
152 """Add annotations which refer to an entire sequence. |
c9699375fcf6
planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/glimmer_hmm commit 0dc67759bcbdf5a8a285ded9ba751340d741fe63
bgruening
parents:
diff
changeset
|
153 """ |
c9699375fcf6
planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/glimmer_hmm commit 0dc67759bcbdf5a8a285ded9ba751340d741fe63
bgruening
parents:
diff
changeset
|
154 format_anns = self._format_keyvals(anns) |
c9699375fcf6
planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/glimmer_hmm commit 0dc67759bcbdf5a8a285ded9ba751340d741fe63
bgruening
parents:
diff
changeset
|
155 if format_anns: |
c9699375fcf6
planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/glimmer_hmm commit 0dc67759bcbdf5a8a285ded9ba751340d741fe63
bgruening
parents:
diff
changeset
|
156 parts = [rec_id, "annotation", "remark", ".", ".", ".", ".", ".", |
c9699375fcf6
planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/glimmer_hmm commit 0dc67759bcbdf5a8a285ded9ba751340d741fe63
bgruening
parents:
diff
changeset
|
157 format_anns] |
c9699375fcf6
planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/glimmer_hmm commit 0dc67759bcbdf5a8a285ded9ba751340d741fe63
bgruening
parents:
diff
changeset
|
158 out_handle.write("\t".join(parts) + "\n") |
c9699375fcf6
planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/glimmer_hmm commit 0dc67759bcbdf5a8a285ded9ba751340d741fe63
bgruening
parents:
diff
changeset
|
159 |
c9699375fcf6
planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/glimmer_hmm commit 0dc67759bcbdf5a8a285ded9ba751340d741fe63
bgruening
parents:
diff
changeset
|
160 def _write_header(self, out_handle): |
c9699375fcf6
planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/glimmer_hmm commit 0dc67759bcbdf5a8a285ded9ba751340d741fe63
bgruening
parents:
diff
changeset
|
161 """Write out standard header directives. |
c9699375fcf6
planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/glimmer_hmm commit 0dc67759bcbdf5a8a285ded9ba751340d741fe63
bgruening
parents:
diff
changeset
|
162 """ |
c9699375fcf6
planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/glimmer_hmm commit 0dc67759bcbdf5a8a285ded9ba751340d741fe63
bgruening
parents:
diff
changeset
|
163 out_handle.write("##gff-version 3\n") |
c9699375fcf6
planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/glimmer_hmm commit 0dc67759bcbdf5a8a285ded9ba751340d741fe63
bgruening
parents:
diff
changeset
|
164 |
c9699375fcf6
planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/glimmer_hmm commit 0dc67759bcbdf5a8a285ded9ba751340d741fe63
bgruening
parents:
diff
changeset
|
165 def write(recs, out_handle): |
c9699375fcf6
planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/glimmer_hmm commit 0dc67759bcbdf5a8a285ded9ba751340d741fe63
bgruening
parents:
diff
changeset
|
166 """High level interface to write GFF3 files from SeqRecords and SeqFeatures. |
c9699375fcf6
planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/glimmer_hmm commit 0dc67759bcbdf5a8a285ded9ba751340d741fe63
bgruening
parents:
diff
changeset
|
167 """ |
c9699375fcf6
planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/glimmer_hmm commit 0dc67759bcbdf5a8a285ded9ba751340d741fe63
bgruening
parents:
diff
changeset
|
168 writer = GFF3Writer() |
c9699375fcf6
planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/glimmer_hmm commit 0dc67759bcbdf5a8a285ded9ba751340d741fe63
bgruening
parents:
diff
changeset
|
169 return writer.write(recs, out_handle) |