Mercurial > repos > eric-rasche > apollo
annotate export.py @ 3:d4ae83dedb14 draft
planemo upload for repository https://github.com/TAMU-CPT/galaxy-webapollo commit 4ac38d0b6dba1183f3e78eb5c224c7051064b4a5
author | eric-rasche |
---|---|
date | Thu, 12 Jan 2017 11:52:28 -0500 |
parents | 6002cc0df04e |
children | 7610987e0c48 |
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 |
0
6002cc0df04e
planemo upload for repository https://github.com/TAMU-CPT/galaxy-webapollo commit 4e5a5af7689f1713c34a6ad9a9594c205e762fdd
eric-rasche
parents:
diff
changeset
|
3 import StringIO |
6002cc0df04e
planemo upload for repository https://github.com/TAMU-CPT/galaxy-webapollo commit 4e5a5af7689f1713c34a6ad9a9594c205e762fdd
eric-rasche
parents:
diff
changeset
|
4 import json |
6002cc0df04e
planemo upload for repository https://github.com/TAMU-CPT/galaxy-webapollo commit 4e5a5af7689f1713c34a6ad9a9594c205e762fdd
eric-rasche
parents:
diff
changeset
|
5 import argparse |
6002cc0df04e
planemo upload for repository https://github.com/TAMU-CPT/galaxy-webapollo commit 4e5a5af7689f1713c34a6ad9a9594c205e762fdd
eric-rasche
parents:
diff
changeset
|
6 from Bio import SeqIO |
6002cc0df04e
planemo upload for repository https://github.com/TAMU-CPT/galaxy-webapollo commit 4e5a5af7689f1713c34a6ad9a9594c205e762fdd
eric-rasche
parents:
diff
changeset
|
7 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
|
8 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
|
9 |
d4ae83dedb14
planemo upload for repository https://github.com/TAMU-CPT/galaxy-webapollo commit 4ac38d0b6dba1183f3e78eb5c224c7051064b4a5
eric-rasche
parents:
0
diff
changeset
|
10 |
d4ae83dedb14
planemo upload for repository https://github.com/TAMU-CPT/galaxy-webapollo commit 4ac38d0b6dba1183f3e78eb5c224c7051064b4a5
eric-rasche
parents:
0
diff
changeset
|
11 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
|
12 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
|
13 |
d4ae83dedb14
planemo upload for repository https://github.com/TAMU-CPT/galaxy-webapollo commit 4ac38d0b6dba1183f3e78eb5c224c7051064b4a5
eric-rasche
parents:
0
diff
changeset
|
14 data = StringIO.StringIO() |
d4ae83dedb14
planemo upload for repository https://github.com/TAMU-CPT/galaxy-webapollo commit 4ac38d0b6dba1183f3e78eb5c224c7051064b4a5
eric-rasche
parents:
0
diff
changeset
|
15 |
d4ae83dedb14
planemo upload for repository https://github.com/TAMU-CPT/galaxy-webapollo commit 4ac38d0b6dba1183f3e78eb5c224c7051064b4a5
eric-rasche
parents:
0
diff
changeset
|
16 kwargs = dict( |
d4ae83dedb14
planemo upload for repository https://github.com/TAMU-CPT/galaxy-webapollo commit 4ac38d0b6dba1183f3e78eb5c224c7051064b4a5
eric-rasche
parents:
0
diff
changeset
|
17 exportType='GFF3', |
d4ae83dedb14
planemo upload for repository https://github.com/TAMU-CPT/galaxy-webapollo commit 4ac38d0b6dba1183f3e78eb5c224c7051064b4a5
eric-rasche
parents:
0
diff
changeset
|
18 seqType='genomic', |
d4ae83dedb14
planemo upload for repository https://github.com/TAMU-CPT/galaxy-webapollo commit 4ac38d0b6dba1183f3e78eb5c224c7051064b4a5
eric-rasche
parents:
0
diff
changeset
|
19 exportGff3Fasta=True, |
d4ae83dedb14
planemo upload for repository https://github.com/TAMU-CPT/galaxy-webapollo commit 4ac38d0b6dba1183f3e78eb5c224c7051064b4a5
eric-rasche
parents:
0
diff
changeset
|
20 output="text", |
d4ae83dedb14
planemo upload for repository https://github.com/TAMU-CPT/galaxy-webapollo commit 4ac38d0b6dba1183f3e78eb5c224c7051064b4a5
eric-rasche
parents:
0
diff
changeset
|
21 exportFormat="text", |
d4ae83dedb14
planemo upload for repository https://github.com/TAMU-CPT/galaxy-webapollo commit 4ac38d0b6dba1183f3e78eb5c224c7051064b4a5
eric-rasche
parents:
0
diff
changeset
|
22 organism=org_cn, |
d4ae83dedb14
planemo upload for repository https://github.com/TAMU-CPT/galaxy-webapollo commit 4ac38d0b6dba1183f3e78eb5c224c7051064b4a5
eric-rasche
parents:
0
diff
changeset
|
23 ) |
d4ae83dedb14
planemo upload for repository https://github.com/TAMU-CPT/galaxy-webapollo commit 4ac38d0b6dba1183f3e78eb5c224c7051064b4a5
eric-rasche
parents:
0
diff
changeset
|
24 |
d4ae83dedb14
planemo upload for repository https://github.com/TAMU-CPT/galaxy-webapollo commit 4ac38d0b6dba1183f3e78eb5c224c7051064b4a5
eric-rasche
parents:
0
diff
changeset
|
25 if len(seqs) > 0: |
d4ae83dedb14
planemo upload for repository https://github.com/TAMU-CPT/galaxy-webapollo commit 4ac38d0b6dba1183f3e78eb5c224c7051064b4a5
eric-rasche
parents:
0
diff
changeset
|
26 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
|
27 exportAllSequences=False, |
d4ae83dedb14
planemo upload for repository https://github.com/TAMU-CPT/galaxy-webapollo commit 4ac38d0b6dba1183f3e78eb5c224c7051064b4a5
eric-rasche
parents:
0
diff
changeset
|
28 sequences=seqs, |
d4ae83dedb14
planemo upload for repository https://github.com/TAMU-CPT/galaxy-webapollo commit 4ac38d0b6dba1183f3e78eb5c224c7051064b4a5
eric-rasche
parents:
0
diff
changeset
|
29 **kwargs |
d4ae83dedb14
planemo upload for repository https://github.com/TAMU-CPT/galaxy-webapollo commit 4ac38d0b6dba1183f3e78eb5c224c7051064b4a5
eric-rasche
parents:
0
diff
changeset
|
30 ).encode('utf-8')) |
d4ae83dedb14
planemo upload for repository https://github.com/TAMU-CPT/galaxy-webapollo commit 4ac38d0b6dba1183f3e78eb5c224c7051064b4a5
eric-rasche
parents:
0
diff
changeset
|
31 else: |
d4ae83dedb14
planemo upload for repository https://github.com/TAMU-CPT/galaxy-webapollo commit 4ac38d0b6dba1183f3e78eb5c224c7051064b4a5
eric-rasche
parents:
0
diff
changeset
|
32 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
|
33 exportAllSequences=True, |
d4ae83dedb14
planemo upload for repository https://github.com/TAMU-CPT/galaxy-webapollo commit 4ac38d0b6dba1183f3e78eb5c224c7051064b4a5
eric-rasche
parents:
0
diff
changeset
|
34 sequences=[], |
d4ae83dedb14
planemo upload for repository https://github.com/TAMU-CPT/galaxy-webapollo commit 4ac38d0b6dba1183f3e78eb5c224c7051064b4a5
eric-rasche
parents:
0
diff
changeset
|
35 **kwargs |
d4ae83dedb14
planemo upload for repository https://github.com/TAMU-CPT/galaxy-webapollo commit 4ac38d0b6dba1183f3e78eb5c224c7051064b4a5
eric-rasche
parents:
0
diff
changeset
|
36 ).encode('utf-8')) |
d4ae83dedb14
planemo upload for repository https://github.com/TAMU-CPT/galaxy-webapollo commit 4ac38d0b6dba1183f3e78eb5c224c7051064b4a5
eric-rasche
parents:
0
diff
changeset
|
37 |
d4ae83dedb14
planemo upload for repository https://github.com/TAMU-CPT/galaxy-webapollo commit 4ac38d0b6dba1183f3e78eb5c224c7051064b4a5
eric-rasche
parents:
0
diff
changeset
|
38 # Seek back to start |
d4ae83dedb14
planemo upload for repository https://github.com/TAMU-CPT/galaxy-webapollo commit 4ac38d0b6dba1183f3e78eb5c224c7051064b4a5
eric-rasche
parents:
0
diff
changeset
|
39 data.seek(0) |
d4ae83dedb14
planemo upload for repository https://github.com/TAMU-CPT/galaxy-webapollo commit 4ac38d0b6dba1183f3e78eb5c224c7051064b4a5
eric-rasche
parents:
0
diff
changeset
|
40 |
d4ae83dedb14
planemo upload for repository https://github.com/TAMU-CPT/galaxy-webapollo commit 4ac38d0b6dba1183f3e78eb5c224c7051064b4a5
eric-rasche
parents:
0
diff
changeset
|
41 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
|
42 if len(records) == 0: |
d4ae83dedb14
planemo upload for repository https://github.com/TAMU-CPT/galaxy-webapollo commit 4ac38d0b6dba1183f3e78eb5c224c7051064b4a5
eric-rasche
parents:
0
diff
changeset
|
43 print "Could not find any sequences or annotations for this organism + reference sequence" |
d4ae83dedb14
planemo upload for repository https://github.com/TAMU-CPT/galaxy-webapollo commit 4ac38d0b6dba1183f3e78eb5c224c7051064b4a5
eric-rasche
parents:
0
diff
changeset
|
44 sys.exit(2) |
d4ae83dedb14
planemo upload for repository https://github.com/TAMU-CPT/galaxy-webapollo commit 4ac38d0b6dba1183f3e78eb5c224c7051064b4a5
eric-rasche
parents:
0
diff
changeset
|
45 else: |
d4ae83dedb14
planemo upload for repository https://github.com/TAMU-CPT/galaxy-webapollo commit 4ac38d0b6dba1183f3e78eb5c224c7051064b4a5
eric-rasche
parents:
0
diff
changeset
|
46 for record in records: |
d4ae83dedb14
planemo upload for repository https://github.com/TAMU-CPT/galaxy-webapollo commit 4ac38d0b6dba1183f3e78eb5c224c7051064b4a5
eric-rasche
parents:
0
diff
changeset
|
47 record.annotations = {} |
d4ae83dedb14
planemo upload for repository https://github.com/TAMU-CPT/galaxy-webapollo commit 4ac38d0b6dba1183f3e78eb5c224c7051064b4a5
eric-rasche
parents:
0
diff
changeset
|
48 if args.gff: |
d4ae83dedb14
planemo upload for repository https://github.com/TAMU-CPT/galaxy-webapollo commit 4ac38d0b6dba1183f3e78eb5c224c7051064b4a5
eric-rasche
parents:
0
diff
changeset
|
49 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
|
50 record.description = "" |
d4ae83dedb14
planemo upload for repository https://github.com/TAMU-CPT/galaxy-webapollo commit 4ac38d0b6dba1183f3e78eb5c224c7051064b4a5
eric-rasche
parents:
0
diff
changeset
|
51 if args.fasta: |
d4ae83dedb14
planemo upload for repository https://github.com/TAMU-CPT/galaxy-webapollo commit 4ac38d0b6dba1183f3e78eb5c224c7051064b4a5
eric-rasche
parents:
0
diff
changeset
|
52 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
|
53 |
d4ae83dedb14
planemo upload for repository https://github.com/TAMU-CPT/galaxy-webapollo commit 4ac38d0b6dba1183f3e78eb5c224c7051064b4a5
eric-rasche
parents:
0
diff
changeset
|
54 return org_data |
0
6002cc0df04e
planemo upload for repository https://github.com/TAMU-CPT/galaxy-webapollo commit 4e5a5af7689f1713c34a6ad9a9594c205e762fdd
eric-rasche
parents:
diff
changeset
|
55 |
6002cc0df04e
planemo upload for repository https://github.com/TAMU-CPT/galaxy-webapollo commit 4e5a5af7689f1713c34a6ad9a9594c205e762fdd
eric-rasche
parents:
diff
changeset
|
56 if __name__ == '__main__': |
6002cc0df04e
planemo upload for repository https://github.com/TAMU-CPT/galaxy-webapollo commit 4e5a5af7689f1713c34a6ad9a9594c205e762fdd
eric-rasche
parents:
diff
changeset
|
57 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
|
58 WAAuth(parser) |
d4ae83dedb14
planemo upload for repository https://github.com/TAMU-CPT/galaxy-webapollo commit 4ac38d0b6dba1183f3e78eb5c224c7051064b4a5
eric-rasche
parents:
0
diff
changeset
|
59 CnOrGuess(parser) |
0
6002cc0df04e
planemo upload for repository https://github.com/TAMU-CPT/galaxy-webapollo commit 4e5a5af7689f1713c34a6ad9a9594c205e762fdd
eric-rasche
parents:
diff
changeset
|
60 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
|
61 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
|
62 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
|
63 |
6002cc0df04e
planemo upload for repository https://github.com/TAMU-CPT/galaxy-webapollo commit 4e5a5af7689f1713c34a6ad9a9594c205e762fdd
eric-rasche
parents:
diff
changeset
|
64 args = parser.parse_args() |
6002cc0df04e
planemo upload for repository https://github.com/TAMU-CPT/galaxy-webapollo commit 4e5a5af7689f1713c34a6ad9a9594c205e762fdd
eric-rasche
parents:
diff
changeset
|
65 |
6002cc0df04e
planemo upload for repository https://github.com/TAMU-CPT/galaxy-webapollo commit 4e5a5af7689f1713c34a6ad9a9594c205e762fdd
eric-rasche
parents:
diff
changeset
|
66 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
|
67 |
3
d4ae83dedb14
planemo upload for repository https://github.com/TAMU-CPT/galaxy-webapollo commit 4ac38d0b6dba1183f3e78eb5c224c7051064b4a5
eric-rasche
parents:
0
diff
changeset
|
68 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
|
69 |
3
d4ae83dedb14
planemo upload for repository https://github.com/TAMU-CPT/galaxy-webapollo commit 4ac38d0b6dba1183f3e78eb5c224c7051064b4a5
eric-rasche
parents:
0
diff
changeset
|
70 org_data = [] |
d4ae83dedb14
planemo upload for repository https://github.com/TAMU-CPT/galaxy-webapollo commit 4ac38d0b6dba1183f3e78eb5c224c7051064b4a5
eric-rasche
parents:
0
diff
changeset
|
71 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
|
72 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
|
73 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
|
74 args.json.write(json.dumps(org_data, indent=2)) |