diff TrackHub.py @ 17:c02720d1afee draft

planemo upload for repository https://github.com/goeckslab/hub-archive-creator commit 1adbf397de1fc7af4d91e026093d7fff983e21cf
author rmarenco
date Fri, 30 Sep 2016 15:14:24 -0400
parents 3233451a3bd6
children d786bca6a75d
line wrap: on
line diff
--- a/TrackHub.py	Sun Sep 25 11:25:38 2016 -0400
+++ b/TrackHub.py	Fri Sep 30 15:14:24 2016 -0400
@@ -33,11 +33,23 @@
         self.default_pos = None
         self.user_email = user_email
 
+        # Set containing the groups already added. Updated by addGroup()
+        self.groups = set()
+
         # TODO: Modify according to the files passed in parameter
+        # ---- Templates ----
+        # Template trackDb
         mylookup = TemplateLookup(directories=[os.path.join(tool_directory, 'templates/trackDb')],
                                   output_encoding='utf-8', encoding_errors='replace')
         self.trackDbTemplate = mylookup.get_template("layout.txt")
 
+        # Template groups
+        mylookup = TemplateLookup(directories=[os.path.join(self.tool_directory, 'templates/groupsTxt')],
+                                  output_encoding='utf-8', encoding_errors='replace')
+        self.groupsTemplate = mylookup.get_template("layout.txt")
+
+        # ---- End Templates ----
+
         self.extra_files_path = extra_files_path
         self.outputFile = outputFile
 
@@ -68,16 +80,43 @@
     def addTrack(self, trackDbObject=None):
         # Create the trackDb.txt file in the specie folder, if not exists
         # Else append the new track
+        # TODO: Get this out of the function
         trackDbTxtFilePath = os.path.join(self.mySpecieFolderPath, 'trackDb.txt')
 
         # Append to trackDbTxtFilePath the trackDbTemplate populate with the newTrack object
         with open(trackDbTxtFilePath, 'a+') as trackDbFile:
             trackDbs = [trackDbObject]
+
+            # TODO: The addGroup does not belong here. Move it when the group becomes more than just a label
+            # Add the group as well, if exists in trackDbObject
+            self.addGroup(trackDbObject.group_name)
+
             htmlMakoRendered = self.trackDbTemplate.render(
                 trackDbs=trackDbs
             )
             trackDbFile.write(htmlMakoRendered)
 
+    def addGroup(self, group_name="Default"):
+        # If not already present in self.groups, add to groups.txt
+        # Create the trackDb.txt file in the specie folder, if not exists
+        # Else append the new track
+        # TODO: Get this out of the function
+        groupsTxtFilePath = os.path.join(self.mySpecieFolderPath, 'groups.txt')
+
+        # If the group is already present, we don't need to add it
+        if group_name in self.groups:
+            return
+
+        # Append to trackDbTxtFilePath the trackDbTemplate populate with the newTrack object
+        with open(groupsTxtFilePath, 'a+') as groupFile:
+            # Add the group as well, if exists in trackDbObject
+
+            htmlMakoRendered = self.groupsTemplate.render(
+                    label=group_name
+            )
+            groupFile.write(htmlMakoRendered)
+        self.groups.add(group_name)
+
     def terminate(self):
         # Just a test to output a simple HTML
         # TODO: Create a class to handle the file object
@@ -182,8 +221,8 @@
 
         # Create the file groups.txt
         # TODO: If not inputs for this, do no create the file
-        groupsTxtFilePath = os.path.join(mySpecieFolderPath, 'groups.txt')
-        self.__fillGroupsTxtFile__(groupsTxtFilePath)
+        # groupsTxtFilePath = os.path.join(mySpecieFolderPath, 'groups.txt')
+        # self.__fillGroupsTxtFile__(groupsTxtFilePath)
 
         # Create the folder tracks into the specie folder
         tracksFolderPath = os.path.join(mySpecieFolderPath, "tracks")