diff build_ctb_gene.py @ 27:3995dfb6b497 draft

planemo upload for repository https://github.com/SANBI-SA/tools-sanbi-uwc.git commit 03cbaf9b605624fc4f6fbad4ccfbbe984d54e990
author sanbi-uwc
date Sun, 22 May 2016 15:34:49 -0400
parents 51a48982f643
children 4864d5866418
line wrap: on
line diff
--- a/build_ctb_gene.py	Fri May 20 06:25:15 2016 -0400
+++ b/build_ctb_gene.py	Sun May 22 15:34:49 2016 -0400
@@ -7,7 +7,9 @@
 import shlex
 import shutil
 import datetime
-from subprocess import check_call, CalledProcessError
+import time
+import random
+from subprocess import check_call, check_output, CalledProcessError
 
 import logging
 
@@ -22,10 +24,11 @@
         # Check whether the options are specified and saves them into the object
         # assert args != None
         self.args = args
+        self.mount_point = None
 
     def build_ctb_gene(self):
-        cmdline_str = "build_ctb_gene goterms ${}".format(input_file)
-        #cmdline_str = "touch /tmp/foo.bar"
+        # cmdline_str = "build_ctb_gene goterms {}".format(self.args.input_file)
+        cmdline_str = "touch /tmp/foo.bar"
         cmdline_str = self.newSplit(cmdline_str)
         build_ctb_run = False
         try:
@@ -36,8 +39,8 @@
         if build_ctb_run:
             self.copy_output_file_to_dataset()
             print("Building a new DB, current time: %s" % str(datetime.date.today()))
-            print("Noe4j Database Name: http://%s:%s@%s:%s/db/data/" % (
-                self.args.username, self.args.password, self.args.url, self.args.port))
+            # print("Noe4j Database Name: http://%s:%s@%s:%s/db/data/" % (
+            #    self.args.username, self.args.password, self.args.url, self.args.port))
             print("GFF File - Input: %s" % str(self.args.input_file))
 
     def newSplit(self, value):
@@ -52,7 +55,8 @@
         Retrieves the output files from the output directory and copies them to the Galaxy output files
         '''
         # retrieve neo4j files to the working gx directory
-        result_file = glob.glob(self.args.mount_point + '/*')
+        mp = self.mount_point + "/graph.db"
+        result_file = glob.glob(mp + '/*')
         for file_name in result_file:
             if os.path.isfile(file_name):
                 shutil.copy2(file_name, self.args.outputdir)
@@ -61,31 +65,60 @@
                 os.chdir(self.args.outputdir)
                 shutil.copytree(file_name, file_name.rsplit('/', 1)[-1])
 
-    def docker_boot(self):
-        cmd = 'docker run -d --name build_ctb_gene thoba/neo4j_galaxy_ie'
+    def docker_stop(self):
+        stop_cmd = 'docker stop build_ctb_gene'
+        stop_cmd_str = self.newSplit(stop_cmd)
+        try:
+            check_call(stop_cmd_str)
+        except CalledProcessError:
+            print("Error running docker stop build_ctb_gene", file=sys.stderr)
+
+    def docker_rm(self):
+        cmd_str = 'docker rm build_ctb_gene'
+        cmd = self.newSplit(cmd_str)
         check_call(cmd)
 
+    def docker_run(self):
+        self.mount_point = "{}/neo4j/data".format(os.getcwd())
+        cmd_str = "docker run -d -p 7474:7474 -v {}:/data -e NEO4J_AUTH=none --name build_ctb_gene neo4j:2.3".format(
+            self.mount_point)
+        cmd = self.newSplit(cmd_str)
+        check_call(cmd)
+
+    def docker_container_check(self):
+        cmd_str = 'docker ps -a -f name=build_ctb_gene | grep build_ctb_gene'
+        output = False
+        try:
+            output = check_output(cmd_str, shell=True)
+        except CalledProcessError:
+            print("Error running docker container check", file=sys.stderr)
+        if output:
+            return True
+        return False
+
+
 def main():
     parser = argparse.ArgumentParser(description="Tool used to extract data about genes using locus_tags")
-    parser.add_argument('--outputfile')
+    # parser.add_argument('--outputfile')
     parser.add_argument('--outputdir')
     parser.add_argument('--input_file')
-    parser.add_argument('--mount_point')
-    parser.add_argument('--username')
-    parser.add_argument('--password')
-    parser.add_argument('--url')
-    parser.add_argument('--port')
+    # parser.add_argument('--mount_point')
+    # parser.add_argument('--username')
+    # parser.add_argument('--password')
+    # parser.add_argument('--url')
+    # parser.add_argument('--port')
     args = parser.parse_args()
 
     ctb_gene_runner = BuildCtbRunner(args)
-    ctb_gene_runner.build_ctb_gene()
 
     # boot up a neo4j docker container
-    ctb_gene_runner.docker_boot()
+    if ctb_gene_runner.docker_container_check():
+        ctb_gene_runner.docker_stop()
+        ctb_gene_runner.docker_rm()
+    ctb_gene_runner.docker_run()
 
-    # docker_cmd = 'docker run '
-
-    export_cmd = "export NEO4J_REST_URL=http://${args.username}:${args.password}@${args.url}:${args.port}/db/data/"
+    # TODO: randomise the ports/names/mount_point and use the autokill image
+    export_cmd = "export NEO4J_REST_URL=http://localhost:7474/db/data/"
     try:
         os.system(export_cmd)
     except (OSError, ValueError), e:
@@ -94,7 +127,8 @@
     # make the output directory
     if not os.path.exists(args.outputdir):
         os.makedirs(args.outputdir)
-
+    time.sleep(60)
+    ctb_gene_runner.build_ctb_gene()
 
 
 if __name__ == "__main__": main()