Mercurial > repos > iuc > virannot_rps2tsv
annotate rps2tsv.py @ 3:d1fd5579469d draft default tip
planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/virAnnot commit 16701bfbffd605805e847897799251ab748f559f
author | iuc |
---|---|
date | Sun, 08 Sep 2024 14:09:07 +0000 |
parents | fd7104249a3c |
children |
rev | line source |
---|---|
0
bbaa89f070f4
planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/virAnnot commit 3a3b40c15ae5e82334f016e88b1f3c5bbbb3b2cd
iuc
parents:
diff
changeset
|
1 #!/usr/bin/env python3 |
bbaa89f070f4
planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/virAnnot commit 3a3b40c15ae5e82334f016e88b1f3c5bbbb3b2cd
iuc
parents:
diff
changeset
|
2 |
bbaa89f070f4
planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/virAnnot commit 3a3b40c15ae5e82334f016e88b1f3c5bbbb3b2cd
iuc
parents:
diff
changeset
|
3 |
bbaa89f070f4
planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/virAnnot commit 3a3b40c15ae5e82334f016e88b1f3c5bbbb3b2cd
iuc
parents:
diff
changeset
|
4 # Name: rps2ecsv |
bbaa89f070f4
planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/virAnnot commit 3a3b40c15ae5e82334f016e88b1f3c5bbbb3b2cd
iuc
parents:
diff
changeset
|
5 # Author: Marie Lefebvre - INRAE |
bbaa89f070f4
planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/virAnnot commit 3a3b40c15ae5e82334f016e88b1f3c5bbbb3b2cd
iuc
parents:
diff
changeset
|
6 # Aims: Convert rpsblast xml output to csv and add taxonomy |
bbaa89f070f4
planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/virAnnot commit 3a3b40c15ae5e82334f016e88b1f3c5bbbb3b2cd
iuc
parents:
diff
changeset
|
7 |
bbaa89f070f4
planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/virAnnot commit 3a3b40c15ae5e82334f016e88b1f3c5bbbb3b2cd
iuc
parents:
diff
changeset
|
8 |
bbaa89f070f4
planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/virAnnot commit 3a3b40c15ae5e82334f016e88b1f3c5bbbb3b2cd
iuc
parents:
diff
changeset
|
9 import argparse |
bbaa89f070f4
planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/virAnnot commit 3a3b40c15ae5e82334f016e88b1f3c5bbbb3b2cd
iuc
parents:
diff
changeset
|
10 import json |
bbaa89f070f4
planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/virAnnot commit 3a3b40c15ae5e82334f016e88b1f3c5bbbb3b2cd
iuc
parents:
diff
changeset
|
11 import logging as log |
bbaa89f070f4
planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/virAnnot commit 3a3b40c15ae5e82334f016e88b1f3c5bbbb3b2cd
iuc
parents:
diff
changeset
|
12 from urllib import request |
bbaa89f070f4
planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/virAnnot commit 3a3b40c15ae5e82334f016e88b1f3c5bbbb3b2cd
iuc
parents:
diff
changeset
|
13 from urllib.error import HTTPError, URLError |
bbaa89f070f4
planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/virAnnot commit 3a3b40c15ae5e82334f016e88b1f3c5bbbb3b2cd
iuc
parents:
diff
changeset
|
14 |
bbaa89f070f4
planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/virAnnot commit 3a3b40c15ae5e82334f016e88b1f3c5bbbb3b2cd
iuc
parents:
diff
changeset
|
15 from Bio.Blast import NCBIXML |
bbaa89f070f4
planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/virAnnot commit 3a3b40c15ae5e82334f016e88b1f3c5bbbb3b2cd
iuc
parents:
diff
changeset
|
16 from ete3 import NCBITaxa |
bbaa89f070f4
planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/virAnnot commit 3a3b40c15ae5e82334f016e88b1f3c5bbbb3b2cd
iuc
parents:
diff
changeset
|
17 |
bbaa89f070f4
planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/virAnnot commit 3a3b40c15ae5e82334f016e88b1f3c5bbbb3b2cd
iuc
parents:
diff
changeset
|
18 ncbi = NCBITaxa() |
bbaa89f070f4
planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/virAnnot commit 3a3b40c15ae5e82334f016e88b1f3c5bbbb3b2cd
iuc
parents:
diff
changeset
|
19 |
bbaa89f070f4
planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/virAnnot commit 3a3b40c15ae5e82334f016e88b1f3c5bbbb3b2cd
iuc
parents:
diff
changeset
|
20 |
bbaa89f070f4
planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/virAnnot commit 3a3b40c15ae5e82334f016e88b1f3c5bbbb3b2cd
iuc
parents:
diff
changeset
|
21 def main(): |
bbaa89f070f4
planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/virAnnot commit 3a3b40c15ae5e82334f016e88b1f3c5bbbb3b2cd
iuc
parents:
diff
changeset
|
22 options = _set_options() |
bbaa89f070f4
planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/virAnnot commit 3a3b40c15ae5e82334f016e88b1f3c5bbbb3b2cd
iuc
parents:
diff
changeset
|
23 _set_log_level(options.verbosity) |
bbaa89f070f4
planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/virAnnot commit 3a3b40c15ae5e82334f016e88b1f3c5bbbb3b2cd
iuc
parents:
diff
changeset
|
24 hits = _read_xml(options) |
bbaa89f070f4
planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/virAnnot commit 3a3b40c15ae5e82334f016e88b1f3c5bbbb3b2cd
iuc
parents:
diff
changeset
|
25 _write_tsv(options, hits) |
bbaa89f070f4
planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/virAnnot commit 3a3b40c15ae5e82334f016e88b1f3c5bbbb3b2cd
iuc
parents:
diff
changeset
|
26 |
bbaa89f070f4
planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/virAnnot commit 3a3b40c15ae5e82334f016e88b1f3c5bbbb3b2cd
iuc
parents:
diff
changeset
|
27 |
bbaa89f070f4
planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/virAnnot commit 3a3b40c15ae5e82334f016e88b1f3c5bbbb3b2cd
iuc
parents:
diff
changeset
|
28 def _read_xml(options): |
bbaa89f070f4
planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/virAnnot commit 3a3b40c15ae5e82334f016e88b1f3c5bbbb3b2cd
iuc
parents:
diff
changeset
|
29 """ |
bbaa89f070f4
planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/virAnnot commit 3a3b40c15ae5e82334f016e88b1f3c5bbbb3b2cd
iuc
parents:
diff
changeset
|
30 Parse XML RPSblast results file |
bbaa89f070f4
planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/virAnnot commit 3a3b40c15ae5e82334f016e88b1f3c5bbbb3b2cd
iuc
parents:
diff
changeset
|
31 """ |
bbaa89f070f4
planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/virAnnot commit 3a3b40c15ae5e82334f016e88b1f3c5bbbb3b2cd
iuc
parents:
diff
changeset
|
32 log.info("Read XML file " + options.xml_file) |
bbaa89f070f4
planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/virAnnot commit 3a3b40c15ae5e82334f016e88b1f3c5bbbb3b2cd
iuc
parents:
diff
changeset
|
33 xml = open(options.xml_file, 'r') |
bbaa89f070f4
planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/virAnnot commit 3a3b40c15ae5e82334f016e88b1f3c5bbbb3b2cd
iuc
parents:
diff
changeset
|
34 records = NCBIXML.parse(xml) |
bbaa89f070f4
planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/virAnnot commit 3a3b40c15ae5e82334f016e88b1f3c5bbbb3b2cd
iuc
parents:
diff
changeset
|
35 xml_results = {} |
bbaa89f070f4
planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/virAnnot commit 3a3b40c15ae5e82334f016e88b1f3c5bbbb3b2cd
iuc
parents:
diff
changeset
|
36 for blast_record in records: |
bbaa89f070f4
planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/virAnnot commit 3a3b40c15ae5e82334f016e88b1f3c5bbbb3b2cd
iuc
parents:
diff
changeset
|
37 for aln in blast_record.alignments: |
bbaa89f070f4
planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/virAnnot commit 3a3b40c15ae5e82334f016e88b1f3c5bbbb3b2cd
iuc
parents:
diff
changeset
|
38 for hit in aln.hsps: |
bbaa89f070f4
planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/virAnnot commit 3a3b40c15ae5e82334f016e88b1f3c5bbbb3b2cd
iuc
parents:
diff
changeset
|
39 hsp = {} |
bbaa89f070f4
planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/virAnnot commit 3a3b40c15ae5e82334f016e88b1f3c5bbbb3b2cd
iuc
parents:
diff
changeset
|
40 hit_evalue = hit.expect |
bbaa89f070f4
planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/virAnnot commit 3a3b40c15ae5e82334f016e88b1f3c5bbbb3b2cd
iuc
parents:
diff
changeset
|
41 if hit_evalue > options.max_evalue: |
bbaa89f070f4
planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/virAnnot commit 3a3b40c15ae5e82334f016e88b1f3c5bbbb3b2cd
iuc
parents:
diff
changeset
|
42 continue |
bbaa89f070f4
planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/virAnnot commit 3a3b40c15ae5e82334f016e88b1f3c5bbbb3b2cd
iuc
parents:
diff
changeset
|
43 hit_frame = hit.frame[0] # frame |
bbaa89f070f4
planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/virAnnot commit 3a3b40c15ae5e82334f016e88b1f3c5bbbb3b2cd
iuc
parents:
diff
changeset
|
44 hit_evalue = hit.expect # evalue |
bbaa89f070f4
planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/virAnnot commit 3a3b40c15ae5e82334f016e88b1f3c5bbbb3b2cd
iuc
parents:
diff
changeset
|
45 hit_startQ = hit.query_start |
bbaa89f070f4
planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/virAnnot commit 3a3b40c15ae5e82334f016e88b1f3c5bbbb3b2cd
iuc
parents:
diff
changeset
|
46 hit_endQ = hit.query_end |
bbaa89f070f4
planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/virAnnot commit 3a3b40c15ae5e82334f016e88b1f3c5bbbb3b2cd
iuc
parents:
diff
changeset
|
47 hsp["frame"] = hit_frame |
bbaa89f070f4
planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/virAnnot commit 3a3b40c15ae5e82334f016e88b1f3c5bbbb3b2cd
iuc
parents:
diff
changeset
|
48 hsp["evalue"] = hit_evalue |
bbaa89f070f4
planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/virAnnot commit 3a3b40c15ae5e82334f016e88b1f3c5bbbb3b2cd
iuc
parents:
diff
changeset
|
49 hsp["startQ"] = hit_startQ |
bbaa89f070f4
planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/virAnnot commit 3a3b40c15ae5e82334f016e88b1f3c5bbbb3b2cd
iuc
parents:
diff
changeset
|
50 hsp["endQ"] = hit_endQ |
3
d1fd5579469d
planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/virAnnot commit 16701bfbffd605805e847897799251ab748f559f
iuc
parents:
2
diff
changeset
|
51 hsp["query_id"] = blast_record.query |
0
bbaa89f070f4
planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/virAnnot commit 3a3b40c15ae5e82334f016e88b1f3c5bbbb3b2cd
iuc
parents:
diff
changeset
|
52 hsp["cdd_id"] = aln.hit_def.split(",")[0] |
bbaa89f070f4
planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/virAnnot commit 3a3b40c15ae5e82334f016e88b1f3c5bbbb3b2cd
iuc
parents:
diff
changeset
|
53 hsp["hit_id"] = aln.hit_id |
bbaa89f070f4
planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/virAnnot commit 3a3b40c15ae5e82334f016e88b1f3c5bbbb3b2cd
iuc
parents:
diff
changeset
|
54 hsp["query_length"] = blast_record.query_length # length of the query |
bbaa89f070f4
planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/virAnnot commit 3a3b40c15ae5e82334f016e88b1f3c5bbbb3b2cd
iuc
parents:
diff
changeset
|
55 hsp["description"] = aln.hit_def |
bbaa89f070f4
planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/virAnnot commit 3a3b40c15ae5e82334f016e88b1f3c5bbbb3b2cd
iuc
parents:
diff
changeset
|
56 hsp["accession"] = aln.accession |
bbaa89f070f4
planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/virAnnot commit 3a3b40c15ae5e82334f016e88b1f3c5bbbb3b2cd
iuc
parents:
diff
changeset
|
57 hsp["pfam_id"] = hsp["description"].split(",")[0].replace("pfam", "PF") |
bbaa89f070f4
planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/virAnnot commit 3a3b40c15ae5e82334f016e88b1f3c5bbbb3b2cd
iuc
parents:
diff
changeset
|
58 log.info("Requeting Interpro for " + hsp["pfam_id"]) |
2
fd7104249a3c
planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/virAnnot commit ab5e1189217b6ed5f1c5d7c5ff6b79b6a4c18cff
iuc
parents:
0
diff
changeset
|
59 url = "https://www.ebi.ac.uk/interpro/api/taxonomy/uniprot/entry/pfam/" + hsp["pfam_id"] |
0
bbaa89f070f4
planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/virAnnot commit 3a3b40c15ae5e82334f016e88b1f3c5bbbb3b2cd
iuc
parents:
diff
changeset
|
60 req = request.Request(url) |
bbaa89f070f4
planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/virAnnot commit 3a3b40c15ae5e82334f016e88b1f3c5bbbb3b2cd
iuc
parents:
diff
changeset
|
61 try: |
bbaa89f070f4
planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/virAnnot commit 3a3b40c15ae5e82334f016e88b1f3c5bbbb3b2cd
iuc
parents:
diff
changeset
|
62 response = request.urlopen(req) |
bbaa89f070f4
planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/virAnnot commit 3a3b40c15ae5e82334f016e88b1f3c5bbbb3b2cd
iuc
parents:
diff
changeset
|
63 except HTTPError as e: |
bbaa89f070f4
planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/virAnnot commit 3a3b40c15ae5e82334f016e88b1f3c5bbbb3b2cd
iuc
parents:
diff
changeset
|
64 log.debug('Http error for interpro: ', e.code) |
bbaa89f070f4
planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/virAnnot commit 3a3b40c15ae5e82334f016e88b1f3c5bbbb3b2cd
iuc
parents:
diff
changeset
|
65 except URLError as e: |
bbaa89f070f4
planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/virAnnot commit 3a3b40c15ae5e82334f016e88b1f3c5bbbb3b2cd
iuc
parents:
diff
changeset
|
66 log.debug('Url error for interpro: ', e.reason) |
bbaa89f070f4
planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/virAnnot commit 3a3b40c15ae5e82334f016e88b1f3c5bbbb3b2cd
iuc
parents:
diff
changeset
|
67 else: |
bbaa89f070f4
planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/virAnnot commit 3a3b40c15ae5e82334f016e88b1f3c5bbbb3b2cd
iuc
parents:
diff
changeset
|
68 encoded_response = response.read() |
bbaa89f070f4
planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/virAnnot commit 3a3b40c15ae5e82334f016e88b1f3c5bbbb3b2cd
iuc
parents:
diff
changeset
|
69 decoded_response = encoded_response.decode() |
bbaa89f070f4
planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/virAnnot commit 3a3b40c15ae5e82334f016e88b1f3c5bbbb3b2cd
iuc
parents:
diff
changeset
|
70 payload = json.loads(decoded_response) |
bbaa89f070f4
planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/virAnnot commit 3a3b40c15ae5e82334f016e88b1f3c5bbbb3b2cd
iuc
parents:
diff
changeset
|
71 kingdoms = [] |
2
fd7104249a3c
planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/virAnnot commit ab5e1189217b6ed5f1c5d7c5ff6b79b6a4c18cff
iuc
parents:
0
diff
changeset
|
72 for item in payload["results"][:6]: |
fd7104249a3c
planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/virAnnot commit ab5e1189217b6ed5f1c5d7c5ff6b79b6a4c18cff
iuc
parents:
0
diff
changeset
|
73 if item["metadata"]["parent"] is not None: |
fd7104249a3c
planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/virAnnot commit ab5e1189217b6ed5f1c5d7c5ff6b79b6a4c18cff
iuc
parents:
0
diff
changeset
|
74 lineage_parent = item["metadata"]["parent"] |
fd7104249a3c
planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/virAnnot commit ab5e1189217b6ed5f1c5d7c5ff6b79b6a4c18cff
iuc
parents:
0
diff
changeset
|
75 translation = ncbi.get_taxid_translator([int(lineage_parent)]) |
fd7104249a3c
planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/virAnnot commit ab5e1189217b6ed5f1c5d7c5ff6b79b6a4c18cff
iuc
parents:
0
diff
changeset
|
76 names = list(translation.values()) |
fd7104249a3c
planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/virAnnot commit ab5e1189217b6ed5f1c5d7c5ff6b79b6a4c18cff
iuc
parents:
0
diff
changeset
|
77 if len(names) > 0: |
fd7104249a3c
planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/virAnnot commit ab5e1189217b6ed5f1c5d7c5ff6b79b6a4c18cff
iuc
parents:
0
diff
changeset
|
78 if names[0] == "root": |
fd7104249a3c
planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/virAnnot commit ab5e1189217b6ed5f1c5d7c5ff6b79b6a4c18cff
iuc
parents:
0
diff
changeset
|
79 taxonomy = names[1:] # remove 'root' at the begining |
fd7104249a3c
planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/virAnnot commit ab5e1189217b6ed5f1c5d7c5ff6b79b6a4c18cff
iuc
parents:
0
diff
changeset
|
80 else: |
fd7104249a3c
planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/virAnnot commit ab5e1189217b6ed5f1c5d7c5ff6b79b6a4c18cff
iuc
parents:
0
diff
changeset
|
81 taxonomy = names |
fd7104249a3c
planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/virAnnot commit ab5e1189217b6ed5f1c5d7c5ff6b79b6a4c18cff
iuc
parents:
0
diff
changeset
|
82 else: |
fd7104249a3c
planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/virAnnot commit ab5e1189217b6ed5f1c5d7c5ff6b79b6a4c18cff
iuc
parents:
0
diff
changeset
|
83 taxonomy = names |
fd7104249a3c
planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/virAnnot commit ab5e1189217b6ed5f1c5d7c5ff6b79b6a4c18cff
iuc
parents:
0
diff
changeset
|
84 if len(taxonomy) != 0: |
fd7104249a3c
planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/virAnnot commit ab5e1189217b6ed5f1c5d7c5ff6b79b6a4c18cff
iuc
parents:
0
diff
changeset
|
85 kingdoms.append(taxonomy[0]) |
0
bbaa89f070f4
planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/virAnnot commit 3a3b40c15ae5e82334f016e88b1f3c5bbbb3b2cd
iuc
parents:
diff
changeset
|
86 frequency = {kingdom: kingdoms.count(kingdom) for kingdom in kingdoms} # {'Pseudomonadota': 9, 'cellular organisms': 4} |
bbaa89f070f4
planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/virAnnot commit 3a3b40c15ae5e82334f016e88b1f3c5bbbb3b2cd
iuc
parents:
diff
changeset
|
87 sorted_freq = dict(sorted(frequency.items(), key=lambda x: x[1], reverse=True)) |
bbaa89f070f4
planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/virAnnot commit 3a3b40c15ae5e82334f016e88b1f3c5bbbb3b2cd
iuc
parents:
diff
changeset
|
88 concat_freq = ";".join("{}({})".format(k, v) for k, v in sorted_freq.items()) |
bbaa89f070f4
planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/virAnnot commit 3a3b40c15ae5e82334f016e88b1f3c5bbbb3b2cd
iuc
parents:
diff
changeset
|
89 hsp["taxonomy"] = concat_freq |
bbaa89f070f4
planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/virAnnot commit 3a3b40c15ae5e82334f016e88b1f3c5bbbb3b2cd
iuc
parents:
diff
changeset
|
90 xml_results[hsp["query_id"]] = hsp |
bbaa89f070f4
planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/virAnnot commit 3a3b40c15ae5e82334f016e88b1f3c5bbbb3b2cd
iuc
parents:
diff
changeset
|
91 return xml_results |
bbaa89f070f4
planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/virAnnot commit 3a3b40c15ae5e82334f016e88b1f3c5bbbb3b2cd
iuc
parents:
diff
changeset
|
92 |
bbaa89f070f4
planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/virAnnot commit 3a3b40c15ae5e82334f016e88b1f3c5bbbb3b2cd
iuc
parents:
diff
changeset
|
93 |
bbaa89f070f4
planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/virAnnot commit 3a3b40c15ae5e82334f016e88b1f3c5bbbb3b2cd
iuc
parents:
diff
changeset
|
94 def _write_tsv(options, hits): |
bbaa89f070f4
planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/virAnnot commit 3a3b40c15ae5e82334f016e88b1f3c5bbbb3b2cd
iuc
parents:
diff
changeset
|
95 """ |
bbaa89f070f4
planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/virAnnot commit 3a3b40c15ae5e82334f016e88b1f3c5bbbb3b2cd
iuc
parents:
diff
changeset
|
96 Write output |
bbaa89f070f4
planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/virAnnot commit 3a3b40c15ae5e82334f016e88b1f3c5bbbb3b2cd
iuc
parents:
diff
changeset
|
97 """ |
bbaa89f070f4
planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/virAnnot commit 3a3b40c15ae5e82334f016e88b1f3c5bbbb3b2cd
iuc
parents:
diff
changeset
|
98 log.info("Write output file " + options.output) |
bbaa89f070f4
planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/virAnnot commit 3a3b40c15ae5e82334f016e88b1f3c5bbbb3b2cd
iuc
parents:
diff
changeset
|
99 headers = "#query_id\tquery_length\tcdd_id\thit_id\tevalue\tstartQ\tendQ\tframe\tdescription\tsuperkingdom\n" |
bbaa89f070f4
planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/virAnnot commit 3a3b40c15ae5e82334f016e88b1f3c5bbbb3b2cd
iuc
parents:
diff
changeset
|
100 f = open(options.output, "w+") |
bbaa89f070f4
planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/virAnnot commit 3a3b40c15ae5e82334f016e88b1f3c5bbbb3b2cd
iuc
parents:
diff
changeset
|
101 f.write(headers) |
bbaa89f070f4
planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/virAnnot commit 3a3b40c15ae5e82334f016e88b1f3c5bbbb3b2cd
iuc
parents:
diff
changeset
|
102 for h in hits: |
bbaa89f070f4
planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/virAnnot commit 3a3b40c15ae5e82334f016e88b1f3c5bbbb3b2cd
iuc
parents:
diff
changeset
|
103 f.write(h + "\t" + str(hits[h]["query_length"]) + "\t") |
bbaa89f070f4
planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/virAnnot commit 3a3b40c15ae5e82334f016e88b1f3c5bbbb3b2cd
iuc
parents:
diff
changeset
|
104 f.write(hits[h]["cdd_id"] + "\t" + hits[h]["hit_id"] + "\t" + str(hits[h]["evalue"]) + "\t") |
bbaa89f070f4
planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/virAnnot commit 3a3b40c15ae5e82334f016e88b1f3c5bbbb3b2cd
iuc
parents:
diff
changeset
|
105 f.write(str(hits[h]["startQ"]) + "\t" + str(hits[h]["endQ"]) + "\t" + str(hits[h]["frame"]) + "\t") |
bbaa89f070f4
planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/virAnnot commit 3a3b40c15ae5e82334f016e88b1f3c5bbbb3b2cd
iuc
parents:
diff
changeset
|
106 f.write(hits[h]["description"] + "\t" + hits[h]["taxonomy"]) |
bbaa89f070f4
planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/virAnnot commit 3a3b40c15ae5e82334f016e88b1f3c5bbbb3b2cd
iuc
parents:
diff
changeset
|
107 f.write("\n") |
bbaa89f070f4
planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/virAnnot commit 3a3b40c15ae5e82334f016e88b1f3c5bbbb3b2cd
iuc
parents:
diff
changeset
|
108 f.close() |
bbaa89f070f4
planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/virAnnot commit 3a3b40c15ae5e82334f016e88b1f3c5bbbb3b2cd
iuc
parents:
diff
changeset
|
109 |
bbaa89f070f4
planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/virAnnot commit 3a3b40c15ae5e82334f016e88b1f3c5bbbb3b2cd
iuc
parents:
diff
changeset
|
110 |
bbaa89f070f4
planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/virAnnot commit 3a3b40c15ae5e82334f016e88b1f3c5bbbb3b2cd
iuc
parents:
diff
changeset
|
111 def _set_options(): |
bbaa89f070f4
planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/virAnnot commit 3a3b40c15ae5e82334f016e88b1f3c5bbbb3b2cd
iuc
parents:
diff
changeset
|
112 parser = argparse.ArgumentParser() |
bbaa89f070f4
planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/virAnnot commit 3a3b40c15ae5e82334f016e88b1f3c5bbbb3b2cd
iuc
parents:
diff
changeset
|
113 parser.add_argument('-x', '--xml', help='XML files with results of blast', action='store', required=True, dest='xml_file') |
bbaa89f070f4
planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/virAnnot commit 3a3b40c15ae5e82334f016e88b1f3c5bbbb3b2cd
iuc
parents:
diff
changeset
|
114 parser.add_argument('-e', '--max_evalue', help='Max evalue', action='store', type=float, default=0.0001, dest='max_evalue') |
bbaa89f070f4
planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/virAnnot commit 3a3b40c15ae5e82334f016e88b1f3c5bbbb3b2cd
iuc
parents:
diff
changeset
|
115 parser.add_argument('-o', '--out', help='The output file (.tab).', action='store', type=str, default='./rps2tsv_output.tab', dest='output') |
bbaa89f070f4
planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/virAnnot commit 3a3b40c15ae5e82334f016e88b1f3c5bbbb3b2cd
iuc
parents:
diff
changeset
|
116 parser.add_argument('-v', '--verbosity', help='Verbose level', action='store', type=int, choices=[1, 2, 3, 4], default=1) |
bbaa89f070f4
planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/virAnnot commit 3a3b40c15ae5e82334f016e88b1f3c5bbbb3b2cd
iuc
parents:
diff
changeset
|
117 args = parser.parse_args() |
bbaa89f070f4
planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/virAnnot commit 3a3b40c15ae5e82334f016e88b1f3c5bbbb3b2cd
iuc
parents:
diff
changeset
|
118 return args |
bbaa89f070f4
planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/virAnnot commit 3a3b40c15ae5e82334f016e88b1f3c5bbbb3b2cd
iuc
parents:
diff
changeset
|
119 |
bbaa89f070f4
planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/virAnnot commit 3a3b40c15ae5e82334f016e88b1f3c5bbbb3b2cd
iuc
parents:
diff
changeset
|
120 |
bbaa89f070f4
planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/virAnnot commit 3a3b40c15ae5e82334f016e88b1f3c5bbbb3b2cd
iuc
parents:
diff
changeset
|
121 def _set_log_level(verbosity): |
bbaa89f070f4
planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/virAnnot commit 3a3b40c15ae5e82334f016e88b1f3c5bbbb3b2cd
iuc
parents:
diff
changeset
|
122 if verbosity == 1: |
bbaa89f070f4
planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/virAnnot commit 3a3b40c15ae5e82334f016e88b1f3c5bbbb3b2cd
iuc
parents:
diff
changeset
|
123 log_format = '%(asctime)s %(levelname)-8s %(message)s' |
bbaa89f070f4
planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/virAnnot commit 3a3b40c15ae5e82334f016e88b1f3c5bbbb3b2cd
iuc
parents:
diff
changeset
|
124 log.basicConfig(level=log.INFO, format=log_format) |
bbaa89f070f4
planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/virAnnot commit 3a3b40c15ae5e82334f016e88b1f3c5bbbb3b2cd
iuc
parents:
diff
changeset
|
125 elif verbosity == 3: |
bbaa89f070f4
planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/virAnnot commit 3a3b40c15ae5e82334f016e88b1f3c5bbbb3b2cd
iuc
parents:
diff
changeset
|
126 log_format = '%(filename)s:%(lineno)s - %(asctime)s %(levelname)-8s %(message)s' |
bbaa89f070f4
planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/virAnnot commit 3a3b40c15ae5e82334f016e88b1f3c5bbbb3b2cd
iuc
parents:
diff
changeset
|
127 log.basicConfig(level=log.DEBUG, format=log_format) |
bbaa89f070f4
planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/virAnnot commit 3a3b40c15ae5e82334f016e88b1f3c5bbbb3b2cd
iuc
parents:
diff
changeset
|
128 |
bbaa89f070f4
planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/virAnnot commit 3a3b40c15ae5e82334f016e88b1f3c5bbbb3b2cd
iuc
parents:
diff
changeset
|
129 |
bbaa89f070f4
planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/virAnnot commit 3a3b40c15ae5e82334f016e88b1f3c5bbbb3b2cd
iuc
parents:
diff
changeset
|
130 if __name__ == "__main__": |
bbaa89f070f4
planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/virAnnot commit 3a3b40c15ae5e82334f016e88b1f3c5bbbb3b2cd
iuc
parents:
diff
changeset
|
131 main() |