ca1f73bcb5983fb36199f9bb09f5fd75b27f4ac4
Merge parents e14a4cf 438ca3f
mmaddren
  Mon Jan 30 10:50:53 2012 -0800
fixed merge conflict with ra.py
diff --cc python/lib/ucscgenomics/ra.py
index 04abb68,2595548..fd32b73
--- python/lib/ucscgenomics/ra.py
+++ python/lib/ucscgenomics/ra.py
@@@ -368,70 -363,117 +368,123 @@@
                  RetThis[stanza.name] = stanza
              elif thisSelectDict[stanza.name] != thatSelectDict[stanza.name]:
                  RetThis[stanza.name] = stanza
          return RetThis
  
      def updateDiffFilter(self, term, other):
          '''
          Replicates updateMetadata.
          Input:
              Term
              Other raFile
  
          Output:
              Merged RaFile
                  Stanzas found in 'self' and 'other' that have the 'Term' in 'other'
 -                are overwritten (or inserted if not found) into 'self'. 
 -                Final merged dictionary is returned.
 +                are overwritten (or inserted if not found) into 'self'. Final merged
 +                dictionary is returned.
          '''
          ret = self
          common = set(self.iterkeys()) & set(other.iterkeys())
          for stanza in common:
              if term not in self[stanza] and term not in other[stanza]:
                  continue
              if term in self[stanza] and term not in other[stanza]:
                      del ret[stanza][term]
                      continue
 +
              if term in other[stanza]:
                  #Remake stanza to keep order of terms
                  tempStanza = RaStanza()
                  tempStanza._name = stanza
 -                selfKeys = list(self[stanza].iterkeys())
 -                otherKeys = list(other[stanza].iterkeys())
 -                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]
 +                try:
 +                    tempStanza['metaObject'] = self[stanza]['metaObject']
 +                    tempStanza['objType'] = self[stanza]['objType']
 +                    termList = self[stanza].keys()
 +                    termList.remove('metaObject')
 +                    termList.remove('objType')
 +                except KeyError:
 +                    termList = self[stanza].keys()
 +                if term not in termList:
 +                    termList.append(term)
 +                for t in sorted(termList, key=str.lower):
 +                    if t == term:
 +                        if t not in self[stanza]:
 +                            tempStanza[t] = other[stanza][t]
 +                        elif self[stanza][t] != other[stanza][t]:
 +                            tempStanza[t] = other[stanza][t]
 +                        else:
 +                            tempStanza[t] = self[stanza][t]
                      else:
 -                        tempStanza[i] = self[stanza][i]
 -            ret[stanza] = tempStanza
 +                        tempStanza[t] = self[stanza][t]
 +                ret[stanza] = tempStanza
 +
          return ret
  
+     def printTrackDbFormat(self):
+         '''
+         Converts a .ra file into TrackDb format.
+         Returns a printable string.
+         '''
+         retstring = ""
+         parentTrack = ""
+         tier = 0
+         commentList = []
+         p = re.compile('^.*parent')
+         p2 = re.compile('^.*subTrack')
+         for stanza in self:
+             if stanza == "":
+                 if commentList:
+                     for line in commentList:
+                         for i in range(tier):
+                             retstring += "    "
+                         retstring += line + "\n"
+                     commentList = []
+                     retstring += "\n"
+                 continue
+             if stanza.startswith("#"):
+                 commentList.append(stanza)
+                 continue
+             keys = self[stanza].keys()
+             parentKey = "NOKEYFOUND"
+             for key in keys:
+                 if p.search(key):
+                     parentKey = key
+                 if p2.search(key):
+                     parentKey = key
+             if parentKey in keys:
+                 if parentTrack not in self[stanza][parentKey] or parentTrack == "":
+                     parentTrack = self[stanza]['track']
+                     tier = 1
+                 else:
+                     tier = 2
+             if commentList:
+                 for line in commentList:
+                     for i in range(tier):
+                         retstring += "    "
+                     retstring += line + "\n"
+                 commentList = []
+             for line in self[stanza]:
+                 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.
      '''