Mercurial > repos > eric-rasche > apollo
annotate create_or_update_organism.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 |
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 |
5
7610987e0c48
planemo upload for repository https://github.com/TAMU-CPT/galaxy-webapollo commit 29795b77c0d5c7894219b018a92c5ee7818096c3
eric-rasche
parents:
3
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 json |
6002cc0df04e
planemo upload for repository https://github.com/TAMU-CPT/galaxy-webapollo commit 4e5a5af7689f1713c34a6ad9a9594c205e762fdd
eric-rasche
parents:
diff
changeset
|
4 import argparse |
6002cc0df04e
planemo upload for repository https://github.com/TAMU-CPT/galaxy-webapollo commit 4e5a5af7689f1713c34a6ad9a9594c205e762fdd
eric-rasche
parents:
diff
changeset
|
5 import time |
3
d4ae83dedb14
planemo upload for repository https://github.com/TAMU-CPT/galaxy-webapollo commit 4ac38d0b6dba1183f3e78eb5c224c7051064b4a5
eric-rasche
parents:
0
diff
changeset
|
6 from webapollo import WAAuth, WebApolloInstance, OrgOrGuess, GuessOrg, AssertUser |
0
6002cc0df04e
planemo upload for repository https://github.com/TAMU-CPT/galaxy-webapollo commit 4e5a5af7689f1713c34a6ad9a9594c205e762fdd
eric-rasche
parents:
diff
changeset
|
7 import logging |
6002cc0df04e
planemo upload for repository https://github.com/TAMU-CPT/galaxy-webapollo commit 4e5a5af7689f1713c34a6ad9a9594c205e762fdd
eric-rasche
parents:
diff
changeset
|
8 logging.basicConfig(level=logging.INFO) |
6002cc0df04e
planemo upload for repository https://github.com/TAMU-CPT/galaxy-webapollo commit 4e5a5af7689f1713c34a6ad9a9594c205e762fdd
eric-rasche
parents:
diff
changeset
|
9 log = logging.getLogger(__name__) |
6002cc0df04e
planemo upload for repository https://github.com/TAMU-CPT/galaxy-webapollo commit 4e5a5af7689f1713c34a6ad9a9594c205e762fdd
eric-rasche
parents:
diff
changeset
|
10 |
6002cc0df04e
planemo upload for repository https://github.com/TAMU-CPT/galaxy-webapollo commit 4e5a5af7689f1713c34a6ad9a9594c205e762fdd
eric-rasche
parents:
diff
changeset
|
11 |
6002cc0df04e
planemo upload for repository https://github.com/TAMU-CPT/galaxy-webapollo commit 4e5a5af7689f1713c34a6ad9a9594c205e762fdd
eric-rasche
parents:
diff
changeset
|
12 if __name__ == '__main__': |
5
7610987e0c48
planemo upload for repository https://github.com/TAMU-CPT/galaxy-webapollo commit 29795b77c0d5c7894219b018a92c5ee7818096c3
eric-rasche
parents:
3
diff
changeset
|
13 parser = argparse.ArgumentParser(description='Create or update an organism in an Apollo instance') |
3
d4ae83dedb14
planemo upload for repository https://github.com/TAMU-CPT/galaxy-webapollo commit 4ac38d0b6dba1183f3e78eb5c224c7051064b4a5
eric-rasche
parents:
0
diff
changeset
|
14 WAAuth(parser) |
0
6002cc0df04e
planemo upload for repository https://github.com/TAMU-CPT/galaxy-webapollo commit 4e5a5af7689f1713c34a6ad9a9594c205e762fdd
eric-rasche
parents:
diff
changeset
|
15 |
6002cc0df04e
planemo upload for repository https://github.com/TAMU-CPT/galaxy-webapollo commit 4e5a5af7689f1713c34a6ad9a9594c205e762fdd
eric-rasche
parents:
diff
changeset
|
16 parser.add_argument('jbrowse', help='JBrowse Data Directory') |
6002cc0df04e
planemo upload for repository https://github.com/TAMU-CPT/galaxy-webapollo commit 4e5a5af7689f1713c34a6ad9a9594c205e762fdd
eric-rasche
parents:
diff
changeset
|
17 parser.add_argument('email', help='User Email') |
3
d4ae83dedb14
planemo upload for repository https://github.com/TAMU-CPT/galaxy-webapollo commit 4ac38d0b6dba1183f3e78eb5c224c7051064b4a5
eric-rasche
parents:
0
diff
changeset
|
18 OrgOrGuess(parser) |
0
6002cc0df04e
planemo upload for repository https://github.com/TAMU-CPT/galaxy-webapollo commit 4e5a5af7689f1713c34a6ad9a9594c205e762fdd
eric-rasche
parents:
diff
changeset
|
19 parser.add_argument('--genus', help='Organism Genus') |
6002cc0df04e
planemo upload for repository https://github.com/TAMU-CPT/galaxy-webapollo commit 4e5a5af7689f1713c34a6ad9a9594c205e762fdd
eric-rasche
parents:
diff
changeset
|
20 parser.add_argument('--species', help='Organism Species') |
6002cc0df04e
planemo upload for repository https://github.com/TAMU-CPT/galaxy-webapollo commit 4e5a5af7689f1713c34a6ad9a9594c205e762fdd
eric-rasche
parents:
diff
changeset
|
21 parser.add_argument('--public', action='store_true', help='Make organism public') |
5
7610987e0c48
planemo upload for repository https://github.com/TAMU-CPT/galaxy-webapollo commit 29795b77c0d5c7894219b018a92c5ee7818096c3
eric-rasche
parents:
3
diff
changeset
|
22 parser.add_argument('--group', help='Give access to a user group') |
0
6002cc0df04e
planemo upload for repository https://github.com/TAMU-CPT/galaxy-webapollo commit 4e5a5af7689f1713c34a6ad9a9594c205e762fdd
eric-rasche
parents:
diff
changeset
|
23 |
6002cc0df04e
planemo upload for repository https://github.com/TAMU-CPT/galaxy-webapollo commit 4e5a5af7689f1713c34a6ad9a9594c205e762fdd
eric-rasche
parents:
diff
changeset
|
24 args = parser.parse_args() |
3
d4ae83dedb14
planemo upload for repository https://github.com/TAMU-CPT/galaxy-webapollo commit 4ac38d0b6dba1183f3e78eb5c224c7051064b4a5
eric-rasche
parents:
0
diff
changeset
|
25 wa = WebApolloInstance(args.apollo, args.username, args.password) |
0
6002cc0df04e
planemo upload for repository https://github.com/TAMU-CPT/galaxy-webapollo commit 4e5a5af7689f1713c34a6ad9a9594c205e762fdd
eric-rasche
parents:
diff
changeset
|
26 |
3
d4ae83dedb14
planemo upload for repository https://github.com/TAMU-CPT/galaxy-webapollo commit 4ac38d0b6dba1183f3e78eb5c224c7051064b4a5
eric-rasche
parents:
0
diff
changeset
|
27 org_cn = GuessOrg(args, wa) |
d4ae83dedb14
planemo upload for repository https://github.com/TAMU-CPT/galaxy-webapollo commit 4ac38d0b6dba1183f3e78eb5c224c7051064b4a5
eric-rasche
parents:
0
diff
changeset
|
28 if isinstance(org_cn, list): |
d4ae83dedb14
planemo upload for repository https://github.com/TAMU-CPT/galaxy-webapollo commit 4ac38d0b6dba1183f3e78eb5c224c7051064b4a5
eric-rasche
parents:
0
diff
changeset
|
29 org_cn = org_cn[0] |
d4ae83dedb14
planemo upload for repository https://github.com/TAMU-CPT/galaxy-webapollo commit 4ac38d0b6dba1183f3e78eb5c224c7051064b4a5
eric-rasche
parents:
0
diff
changeset
|
30 |
0
6002cc0df04e
planemo upload for repository https://github.com/TAMU-CPT/galaxy-webapollo commit 4e5a5af7689f1713c34a6ad9a9594c205e762fdd
eric-rasche
parents:
diff
changeset
|
31 # User must have an account |
3
d4ae83dedb14
planemo upload for repository https://github.com/TAMU-CPT/galaxy-webapollo commit 4ac38d0b6dba1183f3e78eb5c224c7051064b4a5
eric-rasche
parents:
0
diff
changeset
|
32 gx_user = AssertUser(wa.users.loadUsers(email=args.email)) |
0
6002cc0df04e
planemo upload for repository https://github.com/TAMU-CPT/galaxy-webapollo commit 4e5a5af7689f1713c34a6ad9a9594c205e762fdd
eric-rasche
parents:
diff
changeset
|
33 |
6002cc0df04e
planemo upload for repository https://github.com/TAMU-CPT/galaxy-webapollo commit 4e5a5af7689f1713c34a6ad9a9594c205e762fdd
eric-rasche
parents:
diff
changeset
|
34 log.info("Determining if add or update required") |
6002cc0df04e
planemo upload for repository https://github.com/TAMU-CPT/galaxy-webapollo commit 4e5a5af7689f1713c34a6ad9a9594c205e762fdd
eric-rasche
parents:
diff
changeset
|
35 try: |
3
d4ae83dedb14
planemo upload for repository https://github.com/TAMU-CPT/galaxy-webapollo commit 4ac38d0b6dba1183f3e78eb5c224c7051064b4a5
eric-rasche
parents:
0
diff
changeset
|
36 org = wa.organisms.findOrganismByCn(org_cn) |
0
6002cc0df04e
planemo upload for repository https://github.com/TAMU-CPT/galaxy-webapollo commit 4e5a5af7689f1713c34a6ad9a9594c205e762fdd
eric-rasche
parents:
diff
changeset
|
37 except Exception: |
6002cc0df04e
planemo upload for repository https://github.com/TAMU-CPT/galaxy-webapollo commit 4e5a5af7689f1713c34a6ad9a9594c205e762fdd
eric-rasche
parents:
diff
changeset
|
38 org = None |
6002cc0df04e
planemo upload for repository https://github.com/TAMU-CPT/galaxy-webapollo commit 4e5a5af7689f1713c34a6ad9a9594c205e762fdd
eric-rasche
parents:
diff
changeset
|
39 |
6002cc0df04e
planemo upload for repository https://github.com/TAMU-CPT/galaxy-webapollo commit 4e5a5af7689f1713c34a6ad9a9594c205e762fdd
eric-rasche
parents:
diff
changeset
|
40 if org: |
5
7610987e0c48
planemo upload for repository https://github.com/TAMU-CPT/galaxy-webapollo commit 29795b77c0d5c7894219b018a92c5ee7818096c3
eric-rasche
parents:
3
diff
changeset
|
41 has_perms = False |
7610987e0c48
planemo upload for repository https://github.com/TAMU-CPT/galaxy-webapollo commit 29795b77c0d5c7894219b018a92c5ee7818096c3
eric-rasche
parents:
3
diff
changeset
|
42 for user_owned_organism in gx_user.organismPermissions: |
7610987e0c48
planemo upload for repository https://github.com/TAMU-CPT/galaxy-webapollo commit 29795b77c0d5c7894219b018a92c5ee7818096c3
eric-rasche
parents:
3
diff
changeset
|
43 if 'WRITE' in user_owned_organism['permissions']: |
7610987e0c48
planemo upload for repository https://github.com/TAMU-CPT/galaxy-webapollo commit 29795b77c0d5c7894219b018a92c5ee7818096c3
eric-rasche
parents:
3
diff
changeset
|
44 has_perms = True |
7610987e0c48
planemo upload for repository https://github.com/TAMU-CPT/galaxy-webapollo commit 29795b77c0d5c7894219b018a92c5ee7818096c3
eric-rasche
parents:
3
diff
changeset
|
45 break |
7610987e0c48
planemo upload for repository https://github.com/TAMU-CPT/galaxy-webapollo commit 29795b77c0d5c7894219b018a92c5ee7818096c3
eric-rasche
parents:
3
diff
changeset
|
46 |
7610987e0c48
planemo upload for repository https://github.com/TAMU-CPT/galaxy-webapollo commit 29795b77c0d5c7894219b018a92c5ee7818096c3
eric-rasche
parents:
3
diff
changeset
|
47 if not has_perms: |
7610987e0c48
planemo upload for repository https://github.com/TAMU-CPT/galaxy-webapollo commit 29795b77c0d5c7894219b018a92c5ee7818096c3
eric-rasche
parents:
3
diff
changeset
|
48 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.") |
7610987e0c48
planemo upload for repository https://github.com/TAMU-CPT/galaxy-webapollo commit 29795b77c0d5c7894219b018a92c5ee7818096c3
eric-rasche
parents:
3
diff
changeset
|
49 sys.exit(2) |
7610987e0c48
planemo upload for repository https://github.com/TAMU-CPT/galaxy-webapollo commit 29795b77c0d5c7894219b018a92c5ee7818096c3
eric-rasche
parents:
3
diff
changeset
|
50 |
0
6002cc0df04e
planemo upload for repository https://github.com/TAMU-CPT/galaxy-webapollo commit 4e5a5af7689f1713c34a6ad9a9594c205e762fdd
eric-rasche
parents:
diff
changeset
|
51 log.info("\tUpdating Organism") |
6002cc0df04e
planemo upload for repository https://github.com/TAMU-CPT/galaxy-webapollo commit 4e5a5af7689f1713c34a6ad9a9594c205e762fdd
eric-rasche
parents:
diff
changeset
|
52 data = wa.organisms.updateOrganismInfo( |
6002cc0df04e
planemo upload for repository https://github.com/TAMU-CPT/galaxy-webapollo commit 4e5a5af7689f1713c34a6ad9a9594c205e762fdd
eric-rasche
parents:
diff
changeset
|
53 org['id'], |
3
d4ae83dedb14
planemo upload for repository https://github.com/TAMU-CPT/galaxy-webapollo commit 4ac38d0b6dba1183f3e78eb5c224c7051064b4a5
eric-rasche
parents:
0
diff
changeset
|
54 org_cn, |
0
6002cc0df04e
planemo upload for repository https://github.com/TAMU-CPT/galaxy-webapollo commit 4e5a5af7689f1713c34a6ad9a9594c205e762fdd
eric-rasche
parents:
diff
changeset
|
55 args.jbrowse, |
6002cc0df04e
planemo upload for repository https://github.com/TAMU-CPT/galaxy-webapollo commit 4e5a5af7689f1713c34a6ad9a9594c205e762fdd
eric-rasche
parents:
diff
changeset
|
56 # mandatory |
6002cc0df04e
planemo upload for repository https://github.com/TAMU-CPT/galaxy-webapollo commit 4e5a5af7689f1713c34a6ad9a9594c205e762fdd
eric-rasche
parents:
diff
changeset
|
57 genus=args.genus, |
6002cc0df04e
planemo upload for repository https://github.com/TAMU-CPT/galaxy-webapollo commit 4e5a5af7689f1713c34a6ad9a9594c205e762fdd
eric-rasche
parents:
diff
changeset
|
58 species=args.species, |
6002cc0df04e
planemo upload for repository https://github.com/TAMU-CPT/galaxy-webapollo commit 4e5a5af7689f1713c34a6ad9a9594c205e762fdd
eric-rasche
parents:
diff
changeset
|
59 public=args.public |
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 time.sleep(2) |
7610987e0c48
planemo upload for repository https://github.com/TAMU-CPT/galaxy-webapollo commit 29795b77c0d5c7894219b018a92c5ee7818096c3
eric-rasche
parents:
3
diff
changeset
|
62 data = [wa.organisms.findOrganismById(org['id'])] |
0
6002cc0df04e
planemo upload for repository https://github.com/TAMU-CPT/galaxy-webapollo commit 4e5a5af7689f1713c34a6ad9a9594c205e762fdd
eric-rasche
parents:
diff
changeset
|
63 else: |
6002cc0df04e
planemo upload for repository https://github.com/TAMU-CPT/galaxy-webapollo commit 4e5a5af7689f1713c34a6ad9a9594c205e762fdd
eric-rasche
parents:
diff
changeset
|
64 # New organism |
6002cc0df04e
planemo upload for repository https://github.com/TAMU-CPT/galaxy-webapollo commit 4e5a5af7689f1713c34a6ad9a9594c205e762fdd
eric-rasche
parents:
diff
changeset
|
65 log.info("\tAdding Organism") |
6002cc0df04e
planemo upload for repository https://github.com/TAMU-CPT/galaxy-webapollo commit 4e5a5af7689f1713c34a6ad9a9594c205e762fdd
eric-rasche
parents:
diff
changeset
|
66 data = wa.organisms.addOrganism( |
3
d4ae83dedb14
planemo upload for repository https://github.com/TAMU-CPT/galaxy-webapollo commit 4ac38d0b6dba1183f3e78eb5c224c7051064b4a5
eric-rasche
parents:
0
diff
changeset
|
67 org_cn, |
0
6002cc0df04e
planemo upload for repository https://github.com/TAMU-CPT/galaxy-webapollo commit 4e5a5af7689f1713c34a6ad9a9594c205e762fdd
eric-rasche
parents:
diff
changeset
|
68 args.jbrowse, |
6002cc0df04e
planemo upload for repository https://github.com/TAMU-CPT/galaxy-webapollo commit 4e5a5af7689f1713c34a6ad9a9594c205e762fdd
eric-rasche
parents:
diff
changeset
|
69 genus=args.genus, |
6002cc0df04e
planemo upload for repository https://github.com/TAMU-CPT/galaxy-webapollo commit 4e5a5af7689f1713c34a6ad9a9594c205e762fdd
eric-rasche
parents:
diff
changeset
|
70 species=args.species, |
6002cc0df04e
planemo upload for repository https://github.com/TAMU-CPT/galaxy-webapollo commit 4e5a5af7689f1713c34a6ad9a9594c205e762fdd
eric-rasche
parents:
diff
changeset
|
71 public=args.public |
6002cc0df04e
planemo upload for repository https://github.com/TAMU-CPT/galaxy-webapollo commit 4e5a5af7689f1713c34a6ad9a9594c205e762fdd
eric-rasche
parents:
diff
changeset
|
72 ) |
6002cc0df04e
planemo upload for repository https://github.com/TAMU-CPT/galaxy-webapollo commit 4e5a5af7689f1713c34a6ad9a9594c205e762fdd
eric-rasche
parents:
diff
changeset
|
73 |
6002cc0df04e
planemo upload for repository https://github.com/TAMU-CPT/galaxy-webapollo commit 4e5a5af7689f1713c34a6ad9a9594c205e762fdd
eric-rasche
parents:
diff
changeset
|
74 # Must sleep before we're ready to handle |
3
d4ae83dedb14
planemo upload for repository https://github.com/TAMU-CPT/galaxy-webapollo commit 4ac38d0b6dba1183f3e78eb5c224c7051064b4a5
eric-rasche
parents:
0
diff
changeset
|
75 time.sleep(2) |
d4ae83dedb14
planemo upload for repository https://github.com/TAMU-CPT/galaxy-webapollo commit 4ac38d0b6dba1183f3e78eb5c224c7051064b4a5
eric-rasche
parents:
0
diff
changeset
|
76 log.info("Updating permissions for %s on %s", gx_user, org_cn) |
0
6002cc0df04e
planemo upload for repository https://github.com/TAMU-CPT/galaxy-webapollo commit 4e5a5af7689f1713c34a6ad9a9594c205e762fdd
eric-rasche
parents:
diff
changeset
|
77 wa.users.updateOrganismPermission( |
3
d4ae83dedb14
planemo upload for repository https://github.com/TAMU-CPT/galaxy-webapollo commit 4ac38d0b6dba1183f3e78eb5c224c7051064b4a5
eric-rasche
parents:
0
diff
changeset
|
78 gx_user, org_cn, |
0
6002cc0df04e
planemo upload for repository https://github.com/TAMU-CPT/galaxy-webapollo commit 4e5a5af7689f1713c34a6ad9a9594c205e762fdd
eric-rasche
parents:
diff
changeset
|
79 write=True, |
6002cc0df04e
planemo upload for repository https://github.com/TAMU-CPT/galaxy-webapollo commit 4e5a5af7689f1713c34a6ad9a9594c205e762fdd
eric-rasche
parents:
diff
changeset
|
80 export=True, |
6002cc0df04e
planemo upload for repository https://github.com/TAMU-CPT/galaxy-webapollo commit 4e5a5af7689f1713c34a6ad9a9594c205e762fdd
eric-rasche
parents:
diff
changeset
|
81 read=True, |
6002cc0df04e
planemo upload for repository https://github.com/TAMU-CPT/galaxy-webapollo commit 4e5a5af7689f1713c34a6ad9a9594c205e762fdd
eric-rasche
parents:
diff
changeset
|
82 ) |
6002cc0df04e
planemo upload for repository https://github.com/TAMU-CPT/galaxy-webapollo commit 4e5a5af7689f1713c34a6ad9a9594c205e762fdd
eric-rasche
parents:
diff
changeset
|
83 |
5
7610987e0c48
planemo upload for repository https://github.com/TAMU-CPT/galaxy-webapollo commit 29795b77c0d5c7894219b018a92c5ee7818096c3
eric-rasche
parents:
3
diff
changeset
|
84 # Group access |
7610987e0c48
planemo upload for repository https://github.com/TAMU-CPT/galaxy-webapollo commit 29795b77c0d5c7894219b018a92c5ee7818096c3
eric-rasche
parents:
3
diff
changeset
|
85 if args.group: |
7610987e0c48
planemo upload for repository https://github.com/TAMU-CPT/galaxy-webapollo commit 29795b77c0d5c7894219b018a92c5ee7818096c3
eric-rasche
parents:
3
diff
changeset
|
86 group = wa.groups.loadGroupByName(name=args.group) |
7610987e0c48
planemo upload for repository https://github.com/TAMU-CPT/galaxy-webapollo commit 29795b77c0d5c7894219b018a92c5ee7818096c3
eric-rasche
parents:
3
diff
changeset
|
87 res = wa.groups.updateOrganismPermission(group, org_cn, |
7610987e0c48
planemo upload for repository https://github.com/TAMU-CPT/galaxy-webapollo commit 29795b77c0d5c7894219b018a92c5ee7818096c3
eric-rasche
parents:
3
diff
changeset
|
88 administrate=False, write=True, read=True, |
7610987e0c48
planemo upload for repository https://github.com/TAMU-CPT/galaxy-webapollo commit 29795b77c0d5c7894219b018a92c5ee7818096c3
eric-rasche
parents:
3
diff
changeset
|
89 export=True) |
7610987e0c48
planemo upload for repository https://github.com/TAMU-CPT/galaxy-webapollo commit 29795b77c0d5c7894219b018a92c5ee7818096c3
eric-rasche
parents:
3
diff
changeset
|
90 |
3
d4ae83dedb14
planemo upload for repository https://github.com/TAMU-CPT/galaxy-webapollo commit 4ac38d0b6dba1183f3e78eb5c224c7051064b4a5
eric-rasche
parents:
0
diff
changeset
|
91 data = [o for o in data if o['commonName'] == org_cn] |
5
7610987e0c48
planemo upload for repository https://github.com/TAMU-CPT/galaxy-webapollo commit 29795b77c0d5c7894219b018a92c5ee7818096c3
eric-rasche
parents:
3
diff
changeset
|
92 print(json.dumps(data, indent=2)) |