annotate export.py @ 5:7610987e0c48 draft

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