class TDController(QMainWindow, ui_tempDesign.Ui_Dialog):
    def __init__(self, parent=None):
        super(TDController, self).__init__(parent)
        self.setupUi(self)
        self.openPath = None
        self.warningDialog = None
        self.tempDesignTable.setColumnWidth(2, 290)
        self.setWindowTitle('Template Design')
#        self.tempDesignTable.setDragEnabled(True)
#        self.tempDesignTable.setDragDropOverwriteMode(False)
#        self.tempDesignTable.DropIndicatorPosition(QAbstractItemView.BelowItem)
#        self.tempDesignTable.setDragDropMode(QAbstractItemView.DragDrop)
#        self.tempDesignTable.setSelectionBehavior(QAbstractItemView.SelectRows)
        self.establishConnections()

        self.addNewSequence('NNNNNNNNNNNNNNNNNNNN', 'S')
        self.savedFlag = True
        self.saveEnable = True
        self.editPosCB.setCurrentIndex(self.editPosCB.currentIndex() + 1)

    def establishConnections(self):
        self.addSeqPB.pressed.connect(self.addSeq)
        self.rmSeqPB.pressed.connect(self.rmSeq)
        self.newTempPB.pressed.connect(self.newTemp)
        self.loadTempPB.pressed.connect(self.loadTemp)
        self.saveTempPB.pressed.connect(self.saveTemp)
        self.saveTempAsPB.pressed.connect(self.saveTempAs)

        self.tempDesignTable.cellChanged.connect(self.cellChangeEvent)

    def cellChangeEvent(self, row, column):
        self.savedFlag = False
        self.saveEnable = True
        if column == 2 and \
            len(self.tempDesignTable.item(row, column).text()) < 20:
            self.tempDesignTable.item(row, column).setBackground(QBrush(QColor(200, 35, 35)))
            self.saveEnable = False
        else:
            self.tempDesignTable.item(row, column).setBackground(QBrush(QColor(255, 255, 255)))



    def primerLabelUpdate(self):
        index = 65
        for row in range(self.tempDesignTable.rowCount()):
            if index >= 65 + 26:
                self.tempDesignTable.cellWidget(row, 0).setCurrentIndex(0)
            if self.tempDesignTable.cellWidget(row, 0).currentIndex() == 1:
                self.tempDesignTable.item(row, 1).setText(chr(index))
                index = index + 1
        self.savedFlag = False

    def addSeq(self):
        self.addNewSequence('NNNNNNNNNNNNNNNNNNNN', 'S')

    def addNewSequence(self, sequence, seqType):
        row = self.editPosCB.currentIndex() + 1

        self.tempDesignTable.insertRow(row)
        self.editPosCB.addItem(QString(str(self.tempDesignTable.rowCount())))
        self.editPosCB.setCurrentIndex(self.editPosCB.currentIndex() + 1)

        setattr(self, str(row)+'1', QTableWidgetItem(QString(''), QTableWidgetItem.Type))
        setattr(self, str(row)+'2', QTableWidgetItem(QString(sequence), QTableWidgetItem.Type))
        getattr(self, str(row)+'1').setFlags(Qt.NoItemFlags)
        self.tempDesignTable.setItem(row, 1, getattr(self, str(row)+'1'))
        self.tempDesignTable.setItem(row, 2, getattr(self, str(row)+'2'))
        self.tempDesignTable.setCellWidget(row , 0, typeCB(row, self))

        #Reassign Class Names to reflect rearrangement
        if row + 1 < self.tempDesignTable.rowCount():
            for missRow in range(self.tempDesignTable.rowCount() - row):
                newRow = self.tempDesignTable.rowCount() - missRow - 1
                setattr(self, str(newRow) + '1', getattr(self, str(newRow - 1)+'1'))
                setattr(self, str(newRow) +'2', getattr(self, str(newRow - 1)+'2'))
                self.tempDesignTable.cellWidget(newRow, 0).row = newRow

        if seqType == 'P':
            self.tempDesignTable.cellWidget(row, 0).setCurrentIndex(1)
        self.savedFlag = False

    def rmSeq(self):
        row = self.editPosCB.currentIndex()
        if self.tempDesignTable.rowCount() > 1:

            self.tempDesignTable.removeRow(row)
            self.editPosCB.removeItem(self.editPosCB.count()-1)

        #Reassign Class Names to reflect rearrangement
        if row + 1 < self.tempDesignTable.rowCount():
            for missRow in range(self.tempDesignTable.rowCount() - row):
                newRow = self.tempDesignTable.rowCount() - missRow - 1
                setattr(self, str(newRow) + '1', getattr(self, str(newRow - 1)+'1'))
                setattr(self, str(newRow) +'2', getattr(self, str(newRow - 1)+'2'))
                self.tempDesignTable.cellWidget(newRow, 0).row = newRow

        self.primerLabelUpdate()
        self.savedFlag = False


    def newTemp(self):
        if self.savedFlag == False:
            self.warningDialog = WarningDialog('Any unsaved data will be lost.'\
                                +'Do you wish to proceed?', self.newTempApproved)
            self.warningDialog.show()
        else:
            self.newTempApproved()

    def newTempApproved(self):
        self.tempDesignTable.setRowCount(0)
        self.addSeq()
        self.savedFlag = True

    def loadTemp(self):
        if self.savedFlag == False:
            self.warningDialog = WarningDialog('Any unsaved data will be lost.'\
                                +'Do you wish to proceed?', self.loadApproved)
            self.warningDialog.show()
        else:
            self.loadApproved()

    def loadApproved(self):
        self.openPath = QFileDialog.getOpenFileName(self, \
                            "Open File", _root_dir + '/templateSchemes/' , \
                            QString("Schemes (*.cfg)"))
        self.tempDesignTable.setRowCount(0)
        f = file(self.openPath, 'r')
        seqList = json.load(f)[1]
        for row in range(len(seqList)):
            self.addNewSequence(seqList[row][1:], seqList[row][0])
        self.primerLabelUpdate()
        self.savedFlag = True

    def saveTemp(self):
        if self.saveEnable == True:
            if self.openPath == None:
                self.saveTempAs()
            else:
                self.saveFile(self.openPath)
        else:
            self.warningDialog = WarningDialog('Make sure no sequences are'\
                                +' less than 20 bp in length', None)
            self.warningDialog.show()

    def saveTempAs(self):
        if self.saveEnable == True:
            self.openPath = QFileDialog.getSaveFileName(self, \
                                "Save File As", _root_dir + '/templateSchemes/' , \
                                QString("Schemes (*.cfg)"))
            try:
                self.saveFile(self.openPath)
            except:
                pass
        else:
            self.warningDialog = WarningDialog('Make sure no sequences are'\
                                +' less than 20 bp in length', None)
            self.warningDialog.show()

    def saveFile(self, path):
        seqList = []
        for row in range(self.tempDesignTable.rowCount()):
            seq = ''
            if self.tempDesignTable.cellWidget(row, 0).currentIndex() == 1:
                seq = 'P'
            else:
                seq = 'S'
            seq = seq + str(self.tempDesignTable.item(row, 2).text())
            seqList.append(seq)
        f = file(path, 'w')
        json.dump(('None', seqList), f)
        self.savedFlag = True
예제 #2
0
class seqTab:
    def __init__(self, mainWin, parent=None):

        self.mainWin = mainWin
        self.functions = SeqTabFunctions(self)


        self.seqStartPB = self.mainWin.seqStartPB
        self.seqStartPB.setEnabled(False)
        self.includeHomeAxisRB = self.mainWin.includeHomeAxisRB
        self.cycleEntryValidatePB = self.mainWin.cycleEntryValidatePB
        self.clearSelectionPB = self.mainWin.clearSelectionPB
        self.applyPB = self.mainWin.applyPB
        self.applyRepeatPB = self.mainWin.applyRepeatPB

        self.applyPB.setEnabled(False)
        self.applyRepeatPB.setEnabled(False)

        self.warningDialog = None

        self.touchFlagCB = self.mainWin.touchFlagCB
        self.cycleTable = self.mainWin.cycleTable
        self.sequenceGraphicsView = self.mainWin.sequenceGraphicsView
        
        self.baseClassL = []
        self.sortBaseL = []
        self.sortedCyclesL = []
        self.sortCycleD = {}
        self.seqList = []
        self.polonatorCycleListVector = self.mainWin.polonatorCycleListVector

        self.seqScene = QGraphicsScene()
        self.seqScene.setSceneRect(QRectF(0, 0, self.sequenceGraphicsView.width(), self.sequenceGraphicsView.height()))

        self.sequenceGraphicsView.setDragMode(QGraphicsView.RubberBandDrag)
        self.sequenceGraphicsView.setScene(self.seqScene)
        self.sequenceGraphicsView.mouseReleaseEvent = self.graphicsViewMouseRelease
        self.sequenceGraphicsView.setRubberBandSelectionMode(Qt.ContainsItemBoundingRect)

        #load most recent template
        f = file(_root_dir + "/.config/.polGV.cfg", 'r')
        path = json.load(f)
        if path != 'None':
            try:    
                self.functions.addSequence(path)
            except:
                f = file(_root_dir + "/.config/.polGV.cfg", 'w')
                json.dump('None', f)

        #initialize table widget
        self.functions.updateCycleList(True)

        self.establishConnections()


    def establishConnections(self):
        self.seqStartPB.pressed.connect(self.start)
        self.cycleEntryValidatePB.pressed.connect(self.validate)
        self.clearSelectionPB.pressed.connect(self.clear)
        self.applyPB.pressed.connect(self.applySeq)
        self.applyRepeatPB.pressed.connect(self.applyRepeatSeq)
        #prevent user from starting invalid cycle
        self.cycleTable.cellChanged.connect(self.disableStart)

    def disableStart(self):
        self.seqStartPB.setEnabled(False)

    def start(self):
        print 'start'
        touchFlag = "0"
        for i in range(self.cycleTable.rowCount()):
            if self.cycleTable.item(i,0).text() != '':
                print self.cycleTable.item(i,0).text() + \
                        self.cycleTable.item(i,1).text() + \
                        self.cycleTable.item(i,2).text()

        cmd = 'python ' + _root_dir + '/pol_API/G.007_fluidics/src/polonator_main.py ' + touchFlag
#        os.system(cmd)
        print cmd
  
        '''
        try:
            with open("/home/polonator/G.007/G.007_fluidics/src/cycle_list", "w") as outfile:
                for i in range(len(entry)):
                    outfile.write(str(self.polonatorCycleListVector[i][0]+self.polonatorCycleListVector[i][1]+self.polonatorCycleListVector[i][2]))
                    outfile.write('\n')
                if self.touchFlagCB.currentIndex() == 1:
                    touchFlag = "1"

            cmd = "python /home/polonator/G.007/G.007_fluidics/src/polonator_main.py "+ touchFlag 
            #os.system(cmd)

        except IOError: #as (errno, strerror):
            print "Error writing to cycle_list file, I/O error" #: ({0}): {1}".format(errno, strerror)        
        #  self.process_start(cmd, ['pass'], "self.process_pass()")   
        '''
    def validate(self):
        self.seqStartPB.setEnabled(True)
        self.functions.executeValidation()

    def clear(self):
        self.warningDialog = WarningDialog('Are you sure that you want to'+\
                                        ' clear the cycle list?', self.okClear)
        self.warningDialog.show()

    def okClear(self):
        self.cycleTable.clearContents()
        while len(self.baseClassL) != 0:
            #Make sure to remove all repeats, base select turns bases purple
            self.baseClassL[0].fill = self.baseClassL[0].red
            self.baseClassL[0].baseSelect()
        self.functions.updateCycleList(True)
        self.applyPB.setEnabled(False)
        self.applyRepeatPB.setEnabled(False)
        self.warningDialog = None

    def applySeq(self):
        self.functions.updateCycleList(True)
        self.applyPB.setEnabled(False)
        self.applyRepeatPB.setEnabled(True)

    def applyRepeatSeq(self):
        baseClassLPreserve = []
        for base in self.baseClassL:
            if baseClassLPreserve.count(base) == 0:
                baseClassLPreserve.append(base)
        for base in baseClassLPreserve:
            self.baseClassL.append(base)
        self.functions.updateCycleList(True)

    def graphicsViewMouseRelease(self, event):
        #Create cycle name from base
        if len(self.sortBaseL) != 0:
            sortCycleL = []
            for base in self.sortBaseL:
                cycle = base.primer.primerLetter
                if base.parentItem().pos().x() > base.primer.pos().x():
                    cycle = cycle + 'P'
                else:
                    cycle = cycle + 'M'
                cycle = cycle + base.position

                self.sortCycleD.update({cycle : base})
                sortCycleL.append(cycle)

            #Create a plus list and a minus list
            plusL = []
            minusL = []
            maxPos = 1

            cycleCount = 0
            for cycle in sortCycleL:
                if cycle[1] == 'P':
                    plusL.append(cycle)
                if cycle[1] == 'M':
                    minusL.append(cycle)

            #Sort Plus
            sortL = []
            for cycle in plusL:
                pos = int(cycle[2:])
                if sortL.count(pos) == 0:
                    sortL.append(pos)
            sortL.sort()
            sortL.reverse()
            for num in sortL:
                for cycle in plusL:
                    pos = int(cycle[2:])
                    if pos == num:
                        self.sortedCyclesL.append(cycle)
            
            #Sort Minus
            sortL = []
            for cycle in minusL:
                pos = int(cycle[2:])
                if sortL.count(pos) == 0:
                    sortL.append(pos)
            sortL.sort()
            sortL.reverse()
            for num in sortL:
                for cycle in minusL:
                    pos = int(cycle[2:])
                    if pos == num:
                        self.sortedCyclesL.append(cycle)

            for cycle in self.sortedCyclesL:
                self.baseClassL.append(self.sortCycleD[cycle])

            #Reinitialize data structures
            self.sortedCyclesL = []
            self.sortCycleD = {}

            #Empty list without jeapordizing data structure location
            while len(self.sortBaseL) != 0:
                self.sortBaseL.pop()
            self.applyPB.setEnabled(True)