annotate export.py @ 0:1f2e360f7554 draft

planemo upload for repository https://github.com/galaxy-genome-annotation/galaxy-tools/tree/master/tools/apollo commit f745b23c84a615bf434d717c8c0e553a012f0268
author gga
date Mon, 11 Sep 2017 05:48:05 -0400
parents
children b69804a685e2
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
0
1f2e360f7554 planemo upload for repository https://github.com/galaxy-genome-annotation/galaxy-tools/tree/master/tools/apollo commit f745b23c84a615bf434d717c8c0e553a012f0268
gga
parents:
diff changeset
1 #!/usr/bin/env python
1f2e360f7554 planemo upload for repository https://github.com/galaxy-genome-annotation/galaxy-tools/tree/master/tools/apollo commit f745b23c84a615bf434d717c8c0e553a012f0268
gga
parents:
diff changeset
2 from __future__ import print_function
1f2e360f7554 planemo upload for repository https://github.com/galaxy-genome-annotation/galaxy-tools/tree/master/tools/apollo commit f745b23c84a615bf434d717c8c0e553a012f0268
gga
parents:
diff changeset
3
1f2e360f7554 planemo upload for repository https://github.com/galaxy-genome-annotation/galaxy-tools/tree/master/tools/apollo commit f745b23c84a615bf434d717c8c0e553a012f0268
gga
parents:
diff changeset
4 import argparse
1f2e360f7554 planemo upload for repository https://github.com/galaxy-genome-annotation/galaxy-tools/tree/master/tools/apollo commit f745b23c84a615bf434d717c8c0e553a012f0268
gga
parents:
diff changeset
5 import json
1f2e360f7554 planemo upload for repository https://github.com/galaxy-genome-annotation/galaxy-tools/tree/master/tools/apollo commit f745b23c84a615bf434d717c8c0e553a012f0268
gga
parents:
diff changeset
6 import sys
1f2e360f7554 planemo upload for repository https://github.com/galaxy-genome-annotation/galaxy-tools/tree/master/tools/apollo commit f745b23c84a615bf434d717c8c0e553a012f0268
gga
parents:
diff changeset
7
1f2e360f7554 planemo upload for repository https://github.com/galaxy-genome-annotation/galaxy-tools/tree/master/tools/apollo commit f745b23c84a615bf434d717c8c0e553a012f0268
gga
parents:
diff changeset
8 from BCBio import GFF
1f2e360f7554 planemo upload for repository https://github.com/galaxy-genome-annotation/galaxy-tools/tree/master/tools/apollo commit f745b23c84a615bf434d717c8c0e553a012f0268
gga
parents:
diff changeset
9
1f2e360f7554 planemo upload for repository https://github.com/galaxy-genome-annotation/galaxy-tools/tree/master/tools/apollo commit f745b23c84a615bf434d717c8c0e553a012f0268
gga
parents:
diff changeset
10 from Bio import SeqIO
1f2e360f7554 planemo upload for repository https://github.com/galaxy-genome-annotation/galaxy-tools/tree/master/tools/apollo commit f745b23c84a615bf434d717c8c0e553a012f0268
gga
parents:
diff changeset
11
1f2e360f7554 planemo upload for repository https://github.com/galaxy-genome-annotation/galaxy-tools/tree/master/tools/apollo commit f745b23c84a615bf434d717c8c0e553a012f0268
gga
parents:
diff changeset
12 from future import standard_library
1f2e360f7554 planemo upload for repository https://github.com/galaxy-genome-annotation/galaxy-tools/tree/master/tools/apollo commit f745b23c84a615bf434d717c8c0e553a012f0268
gga
parents:
diff changeset
13
1f2e360f7554 planemo upload for repository https://github.com/galaxy-genome-annotation/galaxy-tools/tree/master/tools/apollo commit f745b23c84a615bf434d717c8c0e553a012f0268
gga
parents:
diff changeset
14 from webapollo import CnOrGuess, GuessCn, WAAuth, WebApolloInstance
1f2e360f7554 planemo upload for repository https://github.com/galaxy-genome-annotation/galaxy-tools/tree/master/tools/apollo commit f745b23c84a615bf434d717c8c0e553a012f0268
gga
parents:
diff changeset
15
1f2e360f7554 planemo upload for repository https://github.com/galaxy-genome-annotation/galaxy-tools/tree/master/tools/apollo commit f745b23c84a615bf434d717c8c0e553a012f0268
gga
parents:
diff changeset
16 standard_library.install_aliases()
1f2e360f7554 planemo upload for repository https://github.com/galaxy-genome-annotation/galaxy-tools/tree/master/tools/apollo commit f745b23c84a615bf434d717c8c0e553a012f0268
gga
parents:
diff changeset
17 try:
1f2e360f7554 planemo upload for repository https://github.com/galaxy-genome-annotation/galaxy-tools/tree/master/tools/apollo commit f745b23c84a615bf434d717c8c0e553a012f0268
gga
parents:
diff changeset
18 import StringIO as io
1f2e360f7554 planemo upload for repository https://github.com/galaxy-genome-annotation/galaxy-tools/tree/master/tools/apollo commit f745b23c84a615bf434d717c8c0e553a012f0268
gga
parents:
diff changeset
19 except ImportError:
1f2e360f7554 planemo upload for repository https://github.com/galaxy-genome-annotation/galaxy-tools/tree/master/tools/apollo commit f745b23c84a615bf434d717c8c0e553a012f0268
gga
parents:
diff changeset
20 import io
1f2e360f7554 planemo upload for repository https://github.com/galaxy-genome-annotation/galaxy-tools/tree/master/tools/apollo commit f745b23c84a615bf434d717c8c0e553a012f0268
gga
parents:
diff changeset
21
1f2e360f7554 planemo upload for repository https://github.com/galaxy-genome-annotation/galaxy-tools/tree/master/tools/apollo commit f745b23c84a615bf434d717c8c0e553a012f0268
gga
parents:
diff changeset
22
1f2e360f7554 planemo upload for repository https://github.com/galaxy-genome-annotation/galaxy-tools/tree/master/tools/apollo commit f745b23c84a615bf434d717c8c0e553a012f0268
gga
parents:
diff changeset
23 def export(org_cn, seqs):
1f2e360f7554 planemo upload for repository https://github.com/galaxy-genome-annotation/galaxy-tools/tree/master/tools/apollo commit f745b23c84a615bf434d717c8c0e553a012f0268
gga
parents:
diff changeset
24 org_data = wa.organisms.findOrganismByCn(org_cn)
1f2e360f7554 planemo upload for repository https://github.com/galaxy-genome-annotation/galaxy-tools/tree/master/tools/apollo commit f745b23c84a615bf434d717c8c0e553a012f0268
gga
parents:
diff changeset
25
1f2e360f7554 planemo upload for repository https://github.com/galaxy-genome-annotation/galaxy-tools/tree/master/tools/apollo commit f745b23c84a615bf434d717c8c0e553a012f0268
gga
parents:
diff changeset
26 data = io.StringIO()
1f2e360f7554 planemo upload for repository https://github.com/galaxy-genome-annotation/galaxy-tools/tree/master/tools/apollo commit f745b23c84a615bf434d717c8c0e553a012f0268
gga
parents:
diff changeset
27
1f2e360f7554 planemo upload for repository https://github.com/galaxy-genome-annotation/galaxy-tools/tree/master/tools/apollo commit f745b23c84a615bf434d717c8c0e553a012f0268
gga
parents:
diff changeset
28 kwargs = dict(
1f2e360f7554 planemo upload for repository https://github.com/galaxy-genome-annotation/galaxy-tools/tree/master/tools/apollo commit f745b23c84a615bf434d717c8c0e553a012f0268
gga
parents:
diff changeset
29 exportType='GFF3',
1f2e360f7554 planemo upload for repository https://github.com/galaxy-genome-annotation/galaxy-tools/tree/master/tools/apollo commit f745b23c84a615bf434d717c8c0e553a012f0268
gga
parents:
diff changeset
30 seqType='genomic',
1f2e360f7554 planemo upload for repository https://github.com/galaxy-genome-annotation/galaxy-tools/tree/master/tools/apollo commit f745b23c84a615bf434d717c8c0e553a012f0268
gga
parents:
diff changeset
31 exportGff3Fasta=True,
1f2e360f7554 planemo upload for repository https://github.com/galaxy-genome-annotation/galaxy-tools/tree/master/tools/apollo commit f745b23c84a615bf434d717c8c0e553a012f0268
gga
parents:
diff changeset
32 output="text",
1f2e360f7554 planemo upload for repository https://github.com/galaxy-genome-annotation/galaxy-tools/tree/master/tools/apollo commit f745b23c84a615bf434d717c8c0e553a012f0268
gga
parents:
diff changeset
33 exportFormat="text",
1f2e360f7554 planemo upload for repository https://github.com/galaxy-genome-annotation/galaxy-tools/tree/master/tools/apollo commit f745b23c84a615bf434d717c8c0e553a012f0268
gga
parents:
diff changeset
34 organism=org_cn,
1f2e360f7554 planemo upload for repository https://github.com/galaxy-genome-annotation/galaxy-tools/tree/master/tools/apollo commit f745b23c84a615bf434d717c8c0e553a012f0268
gga
parents:
diff changeset
35 )
1f2e360f7554 planemo upload for repository https://github.com/galaxy-genome-annotation/galaxy-tools/tree/master/tools/apollo commit f745b23c84a615bf434d717c8c0e553a012f0268
gga
parents:
diff changeset
36
1f2e360f7554 planemo upload for repository https://github.com/galaxy-genome-annotation/galaxy-tools/tree/master/tools/apollo commit f745b23c84a615bf434d717c8c0e553a012f0268
gga
parents:
diff changeset
37 if len(seqs) > 0:
1f2e360f7554 planemo upload for repository https://github.com/galaxy-genome-annotation/galaxy-tools/tree/master/tools/apollo commit f745b23c84a615bf434d717c8c0e553a012f0268
gga
parents:
diff changeset
38 data.write(wa.io.write(
1f2e360f7554 planemo upload for repository https://github.com/galaxy-genome-annotation/galaxy-tools/tree/master/tools/apollo commit f745b23c84a615bf434d717c8c0e553a012f0268
gga
parents:
diff changeset
39 exportAllSequences=False,
1f2e360f7554 planemo upload for repository https://github.com/galaxy-genome-annotation/galaxy-tools/tree/master/tools/apollo commit f745b23c84a615bf434d717c8c0e553a012f0268
gga
parents:
diff changeset
40 sequences=seqs,
1f2e360f7554 planemo upload for repository https://github.com/galaxy-genome-annotation/galaxy-tools/tree/master/tools/apollo commit f745b23c84a615bf434d717c8c0e553a012f0268
gga
parents:
diff changeset
41 **kwargs
1f2e360f7554 planemo upload for repository https://github.com/galaxy-genome-annotation/galaxy-tools/tree/master/tools/apollo commit f745b23c84a615bf434d717c8c0e553a012f0268
gga
parents:
diff changeset
42 ).encode('utf-8'))
1f2e360f7554 planemo upload for repository https://github.com/galaxy-genome-annotation/galaxy-tools/tree/master/tools/apollo commit f745b23c84a615bf434d717c8c0e553a012f0268
gga
parents:
diff changeset
43 else:
1f2e360f7554 planemo upload for repository https://github.com/galaxy-genome-annotation/galaxy-tools/tree/master/tools/apollo commit f745b23c84a615bf434d717c8c0e553a012f0268
gga
parents:
diff changeset
44 data.write(wa.io.write(
1f2e360f7554 planemo upload for repository https://github.com/galaxy-genome-annotation/galaxy-tools/tree/master/tools/apollo commit f745b23c84a615bf434d717c8c0e553a012f0268
gga
parents:
diff changeset
45 exportAllSequences=True,
1f2e360f7554 planemo upload for repository https://github.com/galaxy-genome-annotation/galaxy-tools/tree/master/tools/apollo commit f745b23c84a615bf434d717c8c0e553a012f0268
gga
parents:
diff changeset
46 sequences=[],
1f2e360f7554 planemo upload for repository https://github.com/galaxy-genome-annotation/galaxy-tools/tree/master/tools/apollo commit f745b23c84a615bf434d717c8c0e553a012f0268
gga
parents:
diff changeset
47 **kwargs
1f2e360f7554 planemo upload for repository https://github.com/galaxy-genome-annotation/galaxy-tools/tree/master/tools/apollo commit f745b23c84a615bf434d717c8c0e553a012f0268
gga
parents:
diff changeset
48 ).encode('utf-8'))
1f2e360f7554 planemo upload for repository https://github.com/galaxy-genome-annotation/galaxy-tools/tree/master/tools/apollo commit f745b23c84a615bf434d717c8c0e553a012f0268
gga
parents:
diff changeset
49
1f2e360f7554 planemo upload for repository https://github.com/galaxy-genome-annotation/galaxy-tools/tree/master/tools/apollo commit f745b23c84a615bf434d717c8c0e553a012f0268
gga
parents:
diff changeset
50 # Seek back to start
1f2e360f7554 planemo upload for repository https://github.com/galaxy-genome-annotation/galaxy-tools/tree/master/tools/apollo commit f745b23c84a615bf434d717c8c0e553a012f0268
gga
parents:
diff changeset
51 data.seek(0)
1f2e360f7554 planemo upload for repository https://github.com/galaxy-genome-annotation/galaxy-tools/tree/master/tools/apollo commit f745b23c84a615bf434d717c8c0e553a012f0268
gga
parents:
diff changeset
52
1f2e360f7554 planemo upload for repository https://github.com/galaxy-genome-annotation/galaxy-tools/tree/master/tools/apollo commit f745b23c84a615bf434d717c8c0e553a012f0268
gga
parents:
diff changeset
53 records = list(GFF.parse(data))
1f2e360f7554 planemo upload for repository https://github.com/galaxy-genome-annotation/galaxy-tools/tree/master/tools/apollo commit f745b23c84a615bf434d717c8c0e553a012f0268
gga
parents:
diff changeset
54 if len(records) == 0:
1f2e360f7554 planemo upload for repository https://github.com/galaxy-genome-annotation/galaxy-tools/tree/master/tools/apollo commit f745b23c84a615bf434d717c8c0e553a012f0268
gga
parents:
diff changeset
55 print("Could not find any sequences or annotations for this organism + reference sequence")
1f2e360f7554 planemo upload for repository https://github.com/galaxy-genome-annotation/galaxy-tools/tree/master/tools/apollo commit f745b23c84a615bf434d717c8c0e553a012f0268
gga
parents:
diff changeset
56 sys.exit(2)
1f2e360f7554 planemo upload for repository https://github.com/galaxy-genome-annotation/galaxy-tools/tree/master/tools/apollo commit f745b23c84a615bf434d717c8c0e553a012f0268
gga
parents:
diff changeset
57 else:
1f2e360f7554 planemo upload for repository https://github.com/galaxy-genome-annotation/galaxy-tools/tree/master/tools/apollo commit f745b23c84a615bf434d717c8c0e553a012f0268
gga
parents:
diff changeset
58 for record in records:
1f2e360f7554 planemo upload for repository https://github.com/galaxy-genome-annotation/galaxy-tools/tree/master/tools/apollo commit f745b23c84a615bf434d717c8c0e553a012f0268
gga
parents:
diff changeset
59 record.annotations = {}
1f2e360f7554 planemo upload for repository https://github.com/galaxy-genome-annotation/galaxy-tools/tree/master/tools/apollo commit f745b23c84a615bf434d717c8c0e553a012f0268
gga
parents:
diff changeset
60 record.features = sorted(record.features, key=lambda x: x.location.start)
1f2e360f7554 planemo upload for repository https://github.com/galaxy-genome-annotation/galaxy-tools/tree/master/tools/apollo commit f745b23c84a615bf434d717c8c0e553a012f0268
gga
parents:
diff changeset
61 if args.gff:
1f2e360f7554 planemo upload for repository https://github.com/galaxy-genome-annotation/galaxy-tools/tree/master/tools/apollo commit f745b23c84a615bf434d717c8c0e553a012f0268
gga
parents:
diff changeset
62 GFF.write([record], args.gff)
1f2e360f7554 planemo upload for repository https://github.com/galaxy-genome-annotation/galaxy-tools/tree/master/tools/apollo commit f745b23c84a615bf434d717c8c0e553a012f0268
gga
parents:
diff changeset
63 record.description = ""
1f2e360f7554 planemo upload for repository https://github.com/galaxy-genome-annotation/galaxy-tools/tree/master/tools/apollo commit f745b23c84a615bf434d717c8c0e553a012f0268
gga
parents:
diff changeset
64 if args.fasta:
1f2e360f7554 planemo upload for repository https://github.com/galaxy-genome-annotation/galaxy-tools/tree/master/tools/apollo commit f745b23c84a615bf434d717c8c0e553a012f0268
gga
parents:
diff changeset
65 SeqIO.write([record], args.fasta, 'fasta')
1f2e360f7554 planemo upload for repository https://github.com/galaxy-genome-annotation/galaxy-tools/tree/master/tools/apollo commit f745b23c84a615bf434d717c8c0e553a012f0268
gga
parents:
diff changeset
66
1f2e360f7554 planemo upload for repository https://github.com/galaxy-genome-annotation/galaxy-tools/tree/master/tools/apollo commit f745b23c84a615bf434d717c8c0e553a012f0268
gga
parents:
diff changeset
67 return org_data
1f2e360f7554 planemo upload for repository https://github.com/galaxy-genome-annotation/galaxy-tools/tree/master/tools/apollo commit f745b23c84a615bf434d717c8c0e553a012f0268
gga
parents:
diff changeset
68
1f2e360f7554 planemo upload for repository https://github.com/galaxy-genome-annotation/galaxy-tools/tree/master/tools/apollo commit f745b23c84a615bf434d717c8c0e553a012f0268
gga
parents:
diff changeset
69
1f2e360f7554 planemo upload for repository https://github.com/galaxy-genome-annotation/galaxy-tools/tree/master/tools/apollo commit f745b23c84a615bf434d717c8c0e553a012f0268
gga
parents:
diff changeset
70 if __name__ == '__main__':
1f2e360f7554 planemo upload for repository https://github.com/galaxy-genome-annotation/galaxy-tools/tree/master/tools/apollo commit f745b23c84a615bf434d717c8c0e553a012f0268
gga
parents:
diff changeset
71 parser = argparse.ArgumentParser(description='Sample script to add an attribute to a feature via web services')
1f2e360f7554 planemo upload for repository https://github.com/galaxy-genome-annotation/galaxy-tools/tree/master/tools/apollo commit f745b23c84a615bf434d717c8c0e553a012f0268
gga
parents:
diff changeset
72 WAAuth(parser)
1f2e360f7554 planemo upload for repository https://github.com/galaxy-genome-annotation/galaxy-tools/tree/master/tools/apollo commit f745b23c84a615bf434d717c8c0e553a012f0268
gga
parents:
diff changeset
73 CnOrGuess(parser)
1f2e360f7554 planemo upload for repository https://github.com/galaxy-genome-annotation/galaxy-tools/tree/master/tools/apollo commit f745b23c84a615bf434d717c8c0e553a012f0268
gga
parents:
diff changeset
74 parser.add_argument('--gff', type=argparse.FileType('w'))
1f2e360f7554 planemo upload for repository https://github.com/galaxy-genome-annotation/galaxy-tools/tree/master/tools/apollo commit f745b23c84a615bf434d717c8c0e553a012f0268
gga
parents:
diff changeset
75 parser.add_argument('--fasta', type=argparse.FileType('w'))
1f2e360f7554 planemo upload for repository https://github.com/galaxy-genome-annotation/galaxy-tools/tree/master/tools/apollo commit f745b23c84a615bf434d717c8c0e553a012f0268
gga
parents:
diff changeset
76 parser.add_argument('--json', type=argparse.FileType('w'))
1f2e360f7554 planemo upload for repository https://github.com/galaxy-genome-annotation/galaxy-tools/tree/master/tools/apollo commit f745b23c84a615bf434d717c8c0e553a012f0268
gga
parents:
diff changeset
77
1f2e360f7554 planemo upload for repository https://github.com/galaxy-genome-annotation/galaxy-tools/tree/master/tools/apollo commit f745b23c84a615bf434d717c8c0e553a012f0268
gga
parents:
diff changeset
78 args = parser.parse_args()
1f2e360f7554 planemo upload for repository https://github.com/galaxy-genome-annotation/galaxy-tools/tree/master/tools/apollo commit f745b23c84a615bf434d717c8c0e553a012f0268
gga
parents:
diff changeset
79
1f2e360f7554 planemo upload for repository https://github.com/galaxy-genome-annotation/galaxy-tools/tree/master/tools/apollo commit f745b23c84a615bf434d717c8c0e553a012f0268
gga
parents:
diff changeset
80 wa = WebApolloInstance(args.apollo, args.username, args.password)
1f2e360f7554 planemo upload for repository https://github.com/galaxy-genome-annotation/galaxy-tools/tree/master/tools/apollo commit f745b23c84a615bf434d717c8c0e553a012f0268
gga
parents:
diff changeset
81
1f2e360f7554 planemo upload for repository https://github.com/galaxy-genome-annotation/galaxy-tools/tree/master/tools/apollo commit f745b23c84a615bf434d717c8c0e553a012f0268
gga
parents:
diff changeset
82 org_cn_list, seqs = GuessCn(args, wa)
1f2e360f7554 planemo upload for repository https://github.com/galaxy-genome-annotation/galaxy-tools/tree/master/tools/apollo commit f745b23c84a615bf434d717c8c0e553a012f0268
gga
parents:
diff changeset
83
1f2e360f7554 planemo upload for repository https://github.com/galaxy-genome-annotation/galaxy-tools/tree/master/tools/apollo commit f745b23c84a615bf434d717c8c0e553a012f0268
gga
parents:
diff changeset
84 org_data = []
1f2e360f7554 planemo upload for repository https://github.com/galaxy-genome-annotation/galaxy-tools/tree/master/tools/apollo commit f745b23c84a615bf434d717c8c0e553a012f0268
gga
parents:
diff changeset
85 for org_cn in org_cn_list:
1f2e360f7554 planemo upload for repository https://github.com/galaxy-genome-annotation/galaxy-tools/tree/master/tools/apollo commit f745b23c84a615bf434d717c8c0e553a012f0268
gga
parents:
diff changeset
86 indiv_org_data = export(org_cn, seqs)
1f2e360f7554 planemo upload for repository https://github.com/galaxy-genome-annotation/galaxy-tools/tree/master/tools/apollo commit f745b23c84a615bf434d717c8c0e553a012f0268
gga
parents:
diff changeset
87 org_data.append(indiv_org_data)
1f2e360f7554 planemo upload for repository https://github.com/galaxy-genome-annotation/galaxy-tools/tree/master/tools/apollo commit f745b23c84a615bf434d717c8c0e553a012f0268
gga
parents:
diff changeset
88 args.json.write(json.dumps(org_data, indent=2))