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