comparison create_or_update_organism.py @ 0:99b348a1d355 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:47:45 -0400
parents
children 951ef9b7bafc
comparison
equal deleted inserted replaced
-1:000000000000 0:99b348a1d355
1 #!/usr/bin/env python
2 from __future__ import print_function
3
4 import argparse
5 import json
6 import logging
7 import shutil
8 import sys
9 import time
10
11 from webapollo import AssertUser, GuessOrg, OrgOrGuess, WAAuth, WebApolloInstance
12 logging.basicConfig(level=logging.INFO)
13 log = logging.getLogger(__name__)
14
15
16 if __name__ == '__main__':
17 parser = argparse.ArgumentParser(description='Create or update an organism in an Apollo instance')
18 WAAuth(parser)
19
20 parser.add_argument('jbrowse', help='JBrowse Data Directory')
21 parser.add_argument('email', help='User Email')
22 OrgOrGuess(parser)
23 parser.add_argument('--genus', help='Organism Genus')
24 parser.add_argument('--species', help='Organism Species')
25 parser.add_argument('--public', action='store_true', help='Make organism public')
26 parser.add_argument('--group', help='Give access to a user group')
27 parser.add_argument('--remove_old_directory', action='store_true', help='Remove old directory')
28
29 args = parser.parse_args()
30 wa = WebApolloInstance(args.apollo, args.username, args.password)
31
32 org_cn = GuessOrg(args, wa)
33 if isinstance(org_cn, list):
34 org_cn = org_cn[0]
35
36 # User must have an account
37 gx_user = AssertUser(wa.users.loadUsers(email=args.email))
38
39 log.info("Determining if add or update required")
40 try:
41 org = wa.organisms.findOrganismByCn(org_cn)
42 except Exception:
43 org = None
44
45 if org:
46 has_perms = False
47 old_directory = org.directory
48 for user_owned_organism in gx_user.organismPermissions:
49 if 'WRITE' in user_owned_organism['permissions']:
50 has_perms = True
51 break
52
53 if not has_perms:
54 print("Naming Conflict. You do not have permissions to access this organism. Either request permission from the owner, or choose a different name for your organism.")
55 sys.exit(2)
56
57 log.info("\tUpdating Organism")
58 data = wa.organisms.updateOrganismInfo(
59 org['id'],
60 org_cn,
61 args.jbrowse,
62 # mandatory
63 genus=args.genus,
64 species=args.species,
65 public=args.public
66 )
67 time.sleep(2)
68 if(args.remove_old_directory):
69 shutil.rmtree(old_directory)
70
71 data = [wa.organisms.findOrganismById(org['id'])]
72
73 else:
74 # New organism
75 log.info("\tAdding Organism")
76 data = wa.organisms.addOrganism(
77 org_cn,
78 args.jbrowse,
79 genus=args.genus,
80 species=args.species,
81 public=args.public
82 )
83
84 # Must sleep before we're ready to handle
85 time.sleep(2)
86 log.info("Updating permissions for %s on %s", gx_user, org_cn)
87 wa.users.updateOrganismPermission(
88 gx_user, org_cn,
89 write=True,
90 export=True,
91 read=True,
92 )
93
94 # Group access
95 if args.group:
96 group = wa.groups.loadGroupByName(name=args.group)
97 res = wa.groups.updateOrganismPermission(group, org_cn,
98 administrate=False, write=True, read=True,
99 export=True)
100
101 data = [o for o in data if o['commonName'] == org_cn]
102 print(json.dumps(data, indent=2))