diff delete_features.py @ 7:f9a6e151b3b4 draft

planemo upload for repository https://github.com/TAMU-CPT/galaxy-webapollo commit 52b9e5bf6a6efb09a5cb845ee48703651c644174
author eric-rasche
date Tue, 27 Jun 2017 04:05:17 -0400
parents 7610987e0c48
children
line wrap: on
line diff
--- a/delete_features.py	Sat Mar 04 18:00:52 2017 -0500
+++ b/delete_features.py	Tue Jun 27 04:05:17 2017 -0400
@@ -1,7 +1,9 @@
 #!/usr/bin/env python
+from __future__ import print_function
 import argparse
+import random
 from webapollo import WebApolloInstance
-from webapollo import WAAuth, OrgOrGuess, GuessOrg, AssertUser
+from webapollo import WAAuth, OrgOrGuess, GuessOrg, AssertUser, retry
 import logging
 logging.basicConfig(level=logging.INFO)
 log = logging.getLogger(__name__)
@@ -11,6 +13,7 @@
     parser = argparse.ArgumentParser(description='Sample script to delete all features from an organism')
     WAAuth(parser)
     parser.add_argument('email', help='User Email')
+    parser.add_argument('--type', help='Feature type filter')
     OrgOrGuess(parser)
 
     args = parser.parse_args()
@@ -27,13 +30,36 @@
     # TODO: Check user perms on org.
     org = wa.organisms.findOrganismByCn(org_cn)
 
-    # Call setSequence to tell apollo which organism we're working with
-    wa.annotations.setSequence(org['commonName'], org['id'])
-    # Then get a list of features.
-    features = wa.annotations.getFeatures()
-    # For each feature in the features
-    for feature in features['features']:
-        # We see that deleteFeatures wants a uniqueName, and so we pass
-        # is the uniquename field in the feature.
-        print(wa.annotations.deleteFeatures([feature['uniquename']]))
+    sequences = wa.organisms.getSequencesForOrganism(org['id'])
+    for sequence in sequences['sequences']:
+        log.info("Processing %s %s", org['commonName'], sequence['name'])
+        # Call setSequence to tell apollo which organism we're working with
+        wa.annotations.setSequence(sequence['name'], org['id'])
+        # Then get a list of features.
+        features = wa.annotations.getFeatures()
+        # For each feature in the features
+        for feature in sorted(features['features'], key=lambda x: random.random()):
+            if args.type:
+                if args.type == 'tRNA':
+                    if feature['type']['name'] != 'tRNA':
+                        continue
 
+                elif args.type == 'terminator':
+                    if feature['type']['name'] != 'terminator':
+                        continue
+
+                elif args.type == 'mRNA':
+                    if feature['type']['name'] != 'mRNA':
+                        continue
+
+                else:
+                    raise Exception("Unknown type")
+
+            # We see that deleteFeatures wants a uniqueName, and so we pass
+            # is the uniquename field in the feature.
+            def fn():
+                wa.annotations.deleteFeatures([feature['uniquename']])
+                print('Deleted %s [type=%s]' % (feature['uniquename'], feature['type']['name']))
+
+            if not retry(fn, limit=3):
+                print('Error %s' % feature['uniquename'])