comparison delete_features.py @ 10:c6172b538caf draft

"planemo upload for repository https://github.com/galaxy-genome-annotation/galaxy-tools/tree/master/tools/apollo commit 08015be1ee8a784e0619f961aaa724857debfd6f"
author gga
date Mon, 02 Dec 2019 05:42:52 -0500
parents 207c1c281360
children
comparison
equal deleted inserted replaced
9:b0714ffcf142 10:c6172b538caf
3 3
4 import argparse 4 import argparse
5 import logging 5 import logging
6 import random 6 import random
7 7
8 from webapollo import GuessOrg, OrgOrGuess, PermissionCheck, WAAuth, WebApolloInstance, retry 8 from apollo import accessible_organisms
9 from apollo.util import GuessOrg, OrgOrGuess, retry
10
11 from arrow.apollo import get_apollo_instance
12
13 from webapollo import UserObj, handle_credentials
14
9 logging.basicConfig(level=logging.INFO) 15 logging.basicConfig(level=logging.INFO)
10 log = logging.getLogger(__name__) 16 log = logging.getLogger(__name__)
11 17
12 18
13 if __name__ == '__main__': 19 if __name__ == '__main__':
14 parser = argparse.ArgumentParser(description='Sample script to delete all features from an organism') 20 parser = argparse.ArgumentParser(description='Script to delete all features from an organism')
15 WAAuth(parser)
16 parser.add_argument('email', help='User Email') 21 parser.add_argument('email', help='User Email')
17 parser.add_argument('--type', help='Feature type filter') 22 parser.add_argument('--type', help='Feature type filter')
18 OrgOrGuess(parser) 23 OrgOrGuess(parser)
19 24
20 args = parser.parse_args() 25 args = parser.parse_args()
21 26
22 wa = WebApolloInstance(args.apollo, args.username, args.password) 27 wa = get_apollo_instance()
23 # User must have an account 28 # User must have an account
24 gx_user = wa.users.assertOrCreateUser(args.email) 29 gx_user = UserObj(**wa.users._assert_or_create_user(args.email))
30 handle_credentials(gx_user)
25 31
26 # Get organism 32 # Get organism
27 org_cn = GuessOrg(args, wa) 33 org_cn = GuessOrg(args, wa)
28 if isinstance(org_cn, list): 34 if isinstance(org_cn, list):
29 org_cn = org_cn[0] 35 org_cn = org_cn[0]
30 36
31 if not PermissionCheck(gx_user, org_cn, "WRITE"): 37 all_orgs = wa.organisms.get_organisms()
32 raise Exception("Action not permitted") 38 if 'error' in all_orgs:
33 org = wa.organisms.findOrganismByCn(org_cn) 39 all_orgs = []
40 all_orgs = [org['commonName'] for org in all_orgs]
41 if org_cn not in all_orgs:
42 raise Exception("Could not find organism %s" % org_cn)
34 43
35 sequences = wa.organisms.getSequencesForOrganism(org['id']) 44 orgs = accessible_organisms(gx_user, [org_cn], 'WRITE')
45 if not orgs:
46 raise Exception("You do not have write permission on this organism")
47 org = wa.organisms.show_organism(org_cn)
48
49 sequences = wa.organisms.get_sequences(org['id'])
36 for sequence in sequences['sequences']: 50 for sequence in sequences['sequences']:
37 log.info("Processing %s %s", org['commonName'], sequence['name']) 51 log.info("Processing %s %s", org['commonName'], sequence['name'])
38 # Call setSequence to tell apollo which organism we're working with 52 # Call setSequence to tell apollo which organism we're working with
39 wa.annotations.setSequence(sequence['name'], org['id']) 53 wa.annotations.set_sequence(org['id'], sequence['name'])
40 # Then get a list of features. 54 # Then get a list of features.
41 features = wa.annotations.getFeatures() 55 features = wa.annotations.get_features()
42 # For each feature in the features 56 # For each feature in the features
43 for feature in sorted(features['features'], key=lambda x: random.random()): 57 for feature in sorted(features['features'], key=lambda x: random.random()):
44 if args.type: 58 if args.type:
45 if args.type == 'tRNA': 59 if args.type == 'tRNA':
46 if feature['type']['name'] != 'tRNA': 60 if feature['type']['name'] != 'tRNA':
58 raise Exception("Unknown type") 72 raise Exception("Unknown type")
59 73
60 # We see that deleteFeatures wants a uniqueName, and so we pass 74 # We see that deleteFeatures wants a uniqueName, and so we pass
61 # is the uniquename field in the feature. 75 # is the uniquename field in the feature.
62 def fn(): 76 def fn():
63 wa.annotations.deleteFeatures([feature['uniquename']]) 77 wa.annotations.delete_feature(feature['uniquename'])
64 print('Deleted %s [type=%s]' % (feature['uniquename'], feature['type']['name'])) 78 print('Deleted %s [type=%s]' % (feature['uniquename'], feature['type']['name']))
65 79
66 if not retry(fn, limit=3): 80 if not retry(fn, limit=3):
67 print('Error %s' % feature['uniquename']) 81 print('Error %s' % feature['uniquename'])