Example #1
0
 def __init__(self, old_id_path, options, plus=False, log_info=None):  # {
     self.log_info = log_info
     self.options = options
     tool_name = "idmapper"
     if plus:  # {
         tool_name = "idmapper_plus"
     # } end if
     self.command = [GetCommand(self, tool_name), "-s", "human", "-f", old_id_path]
     DebugMsg(self, "Update command:\n  %s" % (" ".join(self.command)))
     self.stream = RunCommandFromList(self.command, stdout=STREAM_OUT, dpt=getattr(self.options, "dpt", False))
     # ExtremeDebugMsg(self, "  finished creating update stream")
     self.GetLine()
Example #2
0
class GeneIDUpdateStreamCls:  # {
    def __init__(self, old_id_path, options, plus=False, log_info=None):  # {
        self.log_info = log_info
        self.options = options
        tool_name = "idmapper"
        if plus:  # {
            tool_name = "idmapper_plus"
        # } end if
        self.command = [GetCommand(self, tool_name), "-s", "human", "-f", old_id_path]
        DebugMsg(self, "Update command:\n  %s" % (" ".join(self.command)))
        self.stream = RunCommandFromList(self.command, stdout=STREAM_OUT, dpt=getattr(self.options, "dpt", False))
        # ExtremeDebugMsg(self, "  finished creating update stream")
        self.GetLine()
        # ExtremeDebugMsg(self, "  got the first line")

    # } end def

    def __iter__(self):  # {
        # ExtremeDebugMsg(self, "Creating iterator for GeneIDUpdateStreamCls")
        return self

    # } end def

    # returns a dictionary: mapping[old_id] = set(new_ids)
    def next(self):  # {
        # ExtremeDebugMsg(self, "In GeneIDUpdateStreamCls.next()...")
        if getattr(self, "finished", False):  # {
            raise StopIteration
        # } end if
        if ID_UPDATE_HEADER != self.curr_line:  # {
            raise MainClassError('Invalid ID update header line: "%s"' % self.curr_line)
        # } end if
        mapping = dict()
        self.GetLine()
        # ExtremeDebugMsg(self, "Finished? %s" % getattr(self, "finished", False))
        # ExtremeDebugMsg(self, "CURR LINE: %s" % self.curr_line)
        while not getattr(self, "finished", False) and ID_UPDATE_HEADER != self.curr_line:  # {
            # ExtremeDebugMsg(self, "  Processing update: %s" % self.curr_line)
            (old_id, new_id) = self.curr_line.split(", ")[:2]
            if "." in old_id:  # {
                old_id = old_id.split(".")[0]
            # } end if
            if "." in new_id:  # {
                new_id = new_id.split(".")[0]
            # } end if
            # ExtremeDebugMsg(self, "    Old ID: %s, New ID: %s" % (old_id, new_id))
            if old_id != new_id:  # {
                if old_id not in mapping:  # {
                    mapping[old_id] = set()
                # } end if
                mapping[old_id].add(new_id)
            # } end if
            self.GetLine()
        # } end while
        return mapping

    # } end def

    def GetLine(self):  # {
        MAX_LOOPS = 100
        count = 0
        # self.GetRawLine()
        next_line = ""
        while not getattr(self, "finished", False) and "" == next_line:  # {
            # "" == getattr(self, "curr_line", "")): #{}
            # self.GetRawLine()
            next_line = self.stream.next()
            # next() should return at least "\n" for a blank line
            if "" == next_line:  # {
                # ExtremeDebugMsg(self, "STREAM COMPLETE")
                self.finished = True
                break
            # } end if
            next_line = CleanLine(next_line)
            # ExtremeDebugMsg(self, "UPDATE LINE: %s" % next_line)
            count += 1
            if MAX_LOOPS < count:  # {
                raise MainClassError("Too many loops while getting update line!")
            # } end if
            # ExtremeDebugMsg(self, "POLLING STREAM")
            # ret will be None until the process terminates
            # ret = self.stream.poll()
            # if (None != ret): #{
            #  ExtremeDebugMsg(self, "STREAM COMPLETE")
            #  self.finished = True
            # } end if
        # } end while
        self.curr_line = next_line