8e03a843df809622d839a5f59a1d0b450b1a36e4
wong
  Wed Jan 18 16:47:08 2012 -0800
Willy draft 2 for trackDb print option in ra.py
diff --git python/lib/ucscgenomics/ra.py python/lib/ucscgenomics/ra.py
index 5cb074f..344b564 100644
--- python/lib/ucscgenomics/ra.py
+++ python/lib/ucscgenomics/ra.py
@@ -395,80 +395,82 @@
                 newOther = list()
                 #filter out keys in other that aren't in self, or the term we're interested in
                 for i in otherKeys:
                     if not i in selfKeys and i != term:
                         continue
                     else:
                         newOther.append(i)
                 #merge self keylist and filtered other list
                 masterList = ucscUtils.mergeList(newOther, selfKeys)
                 for i in masterList:
                     if i == term:
                         tempStanza[i] = other[stanza][i]
                     else:
                         tempStanza[i] = self[stanza][i]
             ret[stanza] = tempStanza
-
         return ret
 
     def printTrackDbFormat(self):
+        '''
+        Converts a .ra file into TrackDb format.
+        Returns a printable string.
+        '''
         retstring = ""
-        space = False
-        tab = False
+        parentTrack = ""
+        tier = 0
         commentList = []
         for stanza in self:
             if stanza == "":
                 if commentList:
                     for line in commentList:
-                        if space == True:
-                            retstring += "    "
-                        if tab == True:
+                        for i in range(tier):
                             retstring += "    "
                         retstring += line + "\n"
                     commentList = []
                     retstring += "\n"
                 continue
             if stanza.startswith("#"):
                 commentList.append(stanza)
                 continue
-            if "visibility" in self[stanza].keys():
-                tab = False
-                space = True
-            if "subGroups" in self[stanza].keys():
-                tab = True
-                space = True
+            if "subTrack" in self[stanza].keys():
+                if parentTrack not in self[stanza]['subTrack'] or parentTrack == "":
+                    parentTrack = self[stanza]['track']
+                    tier = 1
+                else:
+                    tier = 2
+            if "parent" in self[stanza].keys():
+                if parentTrack not in self[stanza]['parent'] or parentTrack == "":
+                    parentTrack = self[stanza]['track']
+                    tier = 1
+                else:
+                    tier = 2
             if commentList:
                 for line in commentList:
-                    if space == True:
-                        retstring += "    "
-                    if tab == True:
+                    for i in range(tier):
                         retstring += "    "
                     retstring += line + "\n"
                 commentList = []
             for line in self[stanza]:
-                if space == True:
-                    retstring += "    "
-                if tab == True:
+                for i in range(tier):
                     retstring += "    "
                 if line.startswith("#"):
                     retstring += line + "\n"
                 else:
                     retstring += line + " " + self[stanza][line] + "\n"
             retstring += "\n"
         return retstring
 
-
     def __str__(self):
         str = ''
         for item in self.iteritems():
             if len(item) == 1:
                 str += item[0].__str__() + '\n'
             else:
                 str += item[1].__str__() + '\n'
         return str #.rsplit('\n', 1)[0]
 
 
 class RaStanza(OrderedDict):
     '''
     Holds an individual entry in the RaFile.
     '''