Mercurial > repos > eric-rasche > apollo
comparison 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 |
comparison
equal
deleted
inserted
replaced
2:c8e16c8eff98 | 3:d4ae83dedb14 |
---|---|
1 #!/usr/bin/env python | 1 #!/usr/bin/env python |
2 import sys | |
2 import StringIO | 3 import StringIO |
3 import sys | |
4 import json | 4 import json |
5 import argparse | 5 import argparse |
6 from Bio import SeqIO | 6 from Bio import SeqIO |
7 from BCBio import GFF | 7 from BCBio import GFF |
8 from webapollo import WebApolloInstance | 8 from webapollo import WAAuth, WebApolloInstance, CnOrGuess, GuessCn |
9 | |
10 | |
11 def export(org_cn, seqs): | |
12 org_data = wa.organisms.findOrganismByCn(org_cn) | |
13 | |
14 data = StringIO.StringIO() | |
15 | |
16 kwargs = dict( | |
17 exportType='GFF3', | |
18 seqType='genomic', | |
19 exportGff3Fasta=True, | |
20 output="text", | |
21 exportFormat="text", | |
22 organism=org_cn, | |
23 ) | |
24 | |
25 if len(seqs) > 0: | |
26 data.write(wa.io.write( | |
27 exportAllSequences=False, | |
28 sequences=seqs, | |
29 **kwargs | |
30 ).encode('utf-8')) | |
31 else: | |
32 data.write(wa.io.write( | |
33 exportAllSequences=True, | |
34 sequences=[], | |
35 **kwargs | |
36 ).encode('utf-8')) | |
37 | |
38 # Seek back to start | |
39 data.seek(0) | |
40 | |
41 records = list(GFF.parse(data)) | |
42 if len(records) == 0: | |
43 print "Could not find any sequences or annotations for this organism + reference sequence" | |
44 sys.exit(2) | |
45 else: | |
46 for record in records: | |
47 record.annotations = {} | |
48 if args.gff: | |
49 GFF.write([record], args.gff) | |
50 record.description = "" | |
51 if args.fasta: | |
52 SeqIO.write([record], args.fasta, 'fasta') | |
53 | |
54 return org_data | |
9 | 55 |
10 if __name__ == '__main__': | 56 if __name__ == '__main__': |
11 json | |
12 parser = argparse.ArgumentParser(description='Sample script to add an attribute to a feature via web services') | 57 parser = argparse.ArgumentParser(description='Sample script to add an attribute to a feature via web services') |
13 parser.add_argument('apollo', help='Complete Apollo URL') | 58 WAAuth(parser) |
14 parser.add_argument('username', help='WA Username') | 59 CnOrGuess(parser) |
15 parser.add_argument('password', help='WA Password') | |
16 | |
17 parser.add_argument('commonName', nargs='+', help='Sequence Unique Names') | |
18 | |
19 parser.add_argument('--gff', type=argparse.FileType('w')) | 60 parser.add_argument('--gff', type=argparse.FileType('w')) |
20 parser.add_argument('--fasta', type=argparse.FileType('w')) | 61 parser.add_argument('--fasta', type=argparse.FileType('w')) |
62 parser.add_argument('--json', type=argparse.FileType('w')) | |
21 | 63 |
22 args = parser.parse_args() | 64 args = parser.parse_args() |
23 | 65 |
24 wa = WebApolloInstance(args.apollo, args.username, args.password) | 66 wa = WebApolloInstance(args.apollo, args.username, args.password) |
25 | 67 |
26 data = StringIO.StringIO(wa.io.write( | 68 org_cn_list, seqs = GuessCn(args, wa) |
27 exportType='GFF3', | |
28 seqType='genomic', | |
29 exportAllSequences=False, | |
30 exportGff3Fasta=True, | |
31 output="text", | |
32 exportFormat="text", | |
33 # TODO: CPT specific convention!!!!!!!! | |
34 organism=args.commonName, | |
35 sequences=args.commonName | |
36 )) | |
37 data.seek(0) | |
38 | 69 |
39 for record in GFF.parse(data): | 70 org_data = [] |
40 record.annotations = {} | 71 for org_cn in org_cn_list: |
41 GFF.write([record], args.gff) | 72 indiv_org_data = export(org_cn, seqs) |
42 record.description = "" | 73 org_data.append(indiv_org_data) |
43 SeqIO.write([record], args.fasta, 'fasta') | 74 args.json.write(json.dumps(org_data, indent=2)) |
44 sys.exit() |