Mercurial > repos > gga > apollo_create_or_update
comparison export.py @ 10:d72192ec8e39 draft
"planemo upload for repository https://github.com/galaxy-genome-annotation/galaxy-tools/tree/master/tools/apollo commit 08015be1ee8a784e0619f961aaa724857debfd6f"
author | gga |
---|---|
date | Mon, 02 Dec 2019 05:46:45 -0500 |
parents | 696a1962212e |
children | 8edb79e6b141 |
comparison
equal
deleted
inserted
replaced
9:29ce13734a5c | 10:d72192ec8e39 |
---|---|
1 #!/usr/bin/env python | 1 #!/usr/bin/env python |
2 from __future__ import print_function | 2 from __future__ import print_function |
3 | 3 |
4 import argparse | 4 import argparse |
5 import json | 5 import json |
6 import sys | 6 import time |
7 | 7 |
8 from BCBio import GFF | 8 from apollo import accessible_organisms |
9 from apollo.util import CnOrGuess, GuessCn | |
9 | 10 |
10 from Bio import SeqIO | 11 from arrow.apollo import get_apollo_instance |
11 | 12 |
12 from webapollo import CnOrGuess, GuessCn, PermissionCheck, WAAuth, WebApolloInstance | 13 from webapollo import UserObj, handle_credentials |
13 | |
14 try: | |
15 import StringIO as io | |
16 except ImportError: | |
17 import io | |
18 | |
19 | |
20 def export(org_cn, seqs): | |
21 org_data = wa.organisms.findOrganismByCn(org_cn) | |
22 | |
23 data = io.StringIO() | |
24 | |
25 kwargs = dict( | |
26 exportType='GFF3', | |
27 seqType='genomic', | |
28 exportGff3Fasta=True, | |
29 output="text", | |
30 exportFormat="text", | |
31 organism=org_cn, | |
32 ) | |
33 | |
34 if len(seqs) > 0: | |
35 data.write(wa.io.write( | |
36 exportAllSequences=False, | |
37 sequences=seqs, | |
38 **kwargs | |
39 ).encode('utf-8')) | |
40 else: | |
41 data.write(wa.io.write( | |
42 exportAllSequences=True, | |
43 sequences=[], | |
44 **kwargs | |
45 ).encode('utf-8')) | |
46 | |
47 # Seek back to start | |
48 data.seek(0) | |
49 | |
50 records = list(GFF.parse(data)) | |
51 if len(records) == 0: | |
52 print("Could not find any sequences or annotations for this organism + reference sequence") | |
53 sys.exit(2) | |
54 else: | |
55 for record in records: | |
56 record.annotations = {} | |
57 record.features = sorted(record.features, key=lambda x: x.location.start) | |
58 if args.gff: | |
59 GFF.write([record], args.gff) | |
60 record.description = "" | |
61 if args.fasta: | |
62 SeqIO.write([record], args.fasta, 'fasta') | |
63 | |
64 return org_data | |
65 | |
66 | 14 |
67 if __name__ == '__main__': | 15 if __name__ == '__main__': |
68 parser = argparse.ArgumentParser(description='Sample script to add an attribute to a feature via web services') | 16 parser = argparse.ArgumentParser(description='Script to export data from Apollo via web services') |
69 WAAuth(parser) | |
70 CnOrGuess(parser) | 17 CnOrGuess(parser) |
71 parser.add_argument('--gff', type=argparse.FileType('w')) | 18 parser.add_argument('--gff', type=argparse.FileType('w')) |
72 parser.add_argument('--fasta', type=argparse.FileType('w')) | 19 parser.add_argument('--fasta_pep', type=argparse.FileType('w')) |
20 parser.add_argument('--fasta_cds', type=argparse.FileType('w')) | |
21 parser.add_argument('--fasta_cdna', type=argparse.FileType('w')) | |
22 parser.add_argument('--vcf', type=argparse.FileType('w')) | |
73 parser.add_argument('--json', type=argparse.FileType('w')) | 23 parser.add_argument('--json', type=argparse.FileType('w')) |
74 parser.add_argument('email', help='User Email') | 24 parser.add_argument('email', help='User Email') |
75 args = parser.parse_args() | 25 args = parser.parse_args() |
76 | 26 |
77 wa = WebApolloInstance(args.apollo, args.username, args.password) | 27 wa = get_apollo_instance() |
78 | |
79 org_cn_list, seqs = GuessCn(args, wa) | |
80 | 28 |
81 # User must have an apollo account, if not, create it | 29 # User must have an apollo account, if not, create it |
82 gx_user = wa.users.assertOrCreateUser(args.email) | 30 gx_user = UserObj(**wa.users._assert_or_create_user(args.email)) |
31 handle_credentials(gx_user) | |
32 | |
33 org_cns, seqs = GuessCn(args, wa) | |
34 if not isinstance(org_cns, list): | |
35 org_cns = [org_cns] | |
36 | |
37 all_orgs = wa.organisms.get_organisms() | |
38 if 'error' in all_orgs: | |
39 all_orgs = [] | |
40 all_orgs = [org['commonName'] for org in all_orgs] | |
83 | 41 |
84 org_data = [] | 42 org_data = [] |
85 for org_cn in org_cn_list: | 43 for org_cn in org_cns: |
86 # User must have read permission on organism | 44 if org_cn not in all_orgs: |
87 if not PermissionCheck(gx_user, org_cn, "READ"): | 45 raise Exception("Could not find organism %s" % org_cn) |
88 continue | 46 |
89 indiv_org_data = export(org_cn, seqs) | 47 orgs = accessible_organisms(gx_user, [org_cn], 'READ') |
90 org_data.append(indiv_org_data) | 48 if not orgs: |
49 raise Exception("You do not have read permission on organism %s" % org_cn) | |
50 | |
51 org = wa.organisms.show_organism(org_cn) | |
52 | |
53 uuid_gff = wa.io.write_downloadable(org['commonName'], 'GFF3', export_gff3_fasta=True, sequences=seqs) | |
54 if 'error' in uuid_gff or 'uuid' not in uuid_gff: | |
55 raise Exception("Apollo failed to prepare the file for download: %s" % uuid_gff) | |
56 args.gff.write(wa.io.download(uuid_gff['uuid'], output_format="text")) | |
57 | |
58 time.sleep(1) | |
59 | |
60 uuid_vcf = wa.io.write_downloadable(org['commonName'], 'VCF', sequences=seqs) | |
61 if 'error' in uuid_vcf or 'uuid' not in uuid_vcf: | |
62 raise Exception("Apollo failed to prepare the file for download: %s" % uuid_vcf) | |
63 args.vcf.write(wa.io.download(uuid_vcf['uuid'], output_format="text")) | |
64 | |
65 time.sleep(1) | |
66 | |
67 uuid_fa = wa.io.write_downloadable(org['commonName'], 'FASTA', sequences=seqs, seq_type='cdna') | |
68 if 'error' in uuid_fa or 'uuid' not in uuid_fa: | |
69 raise Exception("Apollo failed to prepare the file for download: %s" % uuid_fa) | |
70 args.fasta_cdna.write(wa.io.download(uuid_fa['uuid'], output_format="text")) | |
71 | |
72 time.sleep(1) | |
73 | |
74 uuid_fa = wa.io.write_downloadable(org['commonName'], 'FASTA', sequences=seqs, seq_type='cds') | |
75 if 'error' in uuid_fa or 'uuid' not in uuid_fa: | |
76 raise Exception("Apollo failed to prepare the file for download: %s" % uuid_fa) | |
77 args.fasta_cds.write(wa.io.download(uuid_fa['uuid'], output_format="text")) | |
78 | |
79 time.sleep(1) | |
80 | |
81 uuid_fa = wa.io.write_downloadable(org['commonName'], 'FASTA', sequences=seqs, seq_type='peptide') | |
82 if 'error' in uuid_fa or 'uuid' not in uuid_fa: | |
83 raise Exception("Apollo failed to prepare the file for download: %s" % uuid_fa) | |
84 args.fasta_pep.write(wa.io.download(uuid_fa['uuid'], output_format="text")) | |
85 | |
86 org_data.append(org) | |
87 | |
91 args.json.write(json.dumps(org_data, indent=2)) | 88 args.json.write(json.dumps(org_data, indent=2)) |