1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60
| #!/usr/bin/env python
# -*- coding: utf-8 -*-
from PyQt4 import QtCore, QtGui
class QTextEditATGCNU(QtGui.QTextEdit):
"""QTextEdit for DNA/RNA sequences.
Accepts only ATGCNU key events, navigation shortcuts are reimplemented, cut, copy and paste functions also with cleaning of the clipboard string content before pasting.
"""
def cleanNucleic(self, seqToClean):
"""Removes non GATCNU chars from the string."""
cleaned = []
for i in str(seqToClean):
if str(i) in "atgcnuATGCNU":
cleaned.append(i)
return "".join(cleaned)
def paste(self):
"""Pastes clipboard content after cleaning."""
clipboard = QtGui.QApplication.clipboard()
self.insertPlainText(self.cleanNucleic(clipboard.text()))
def keyPressEvent(self, event):
"""Redefines keyPress events"""
# ShortCuts with CONTROL key Modifier
if event.modifiers() == QtCore.Qt.ControlModifier:
if event.key() == QtCore.Qt.Key_A:
self.selectAll()
if event.key() == QtCore.Qt.Key_X:
self.cut()
if event.key() == QtCore.Qt.Key_C:
self.copy()
if event.key() == QtCore.Qt.Key_V:
self.paste()
# Without modifier or with SHIFT
else:
# Sets navigation or selection mode according to SHIFT key usage
modShift = QtGui.QTextCursor.MoveAnchor
if event.modifiers() == QtCore.Qt.ShiftModifier:
modShift = QtGui.QTextCursor.KeepAnchor
# Accepted key inputs
if event.key() in (QtCore.Qt.Key_G, QtCore.Qt.Key_C, QtCore.Qt.Key_T, QtCore.Qt.Key_A, QtCore.Qt.Key_N, QtCore.Qt.Key_U):
QtGui.QTextEdit.keyPressEvent(self, event)
# Navigation or selection with keys
elif event.key() == QtCore.Qt.Key_Up:
self.moveCursor(QtGui.QTextCursor.Up, modShift)
elif event.key() == QtCore.Qt.Key_Down:
self.moveCursor(QtGui.QTextCursor.Down, modShift)
elif event.key() == QtCore.Qt.Key_Left:
self.moveCursor(QtGui.QTextCursor.Left, modShift)
elif event.key() == QtCore.Qt.Key_Right:
self.moveCursor(QtGui.QTextCursor.Right, modShift)
elif event.key() == QtCore.Qt.Key_Home:
self.moveCursor(QtGui.QTextCursor.Start, modShift)
elif event.key() == QtCore.Qt.Key_End:
self.moveCursor(QtGui.QTextCursor.End, modShift) |
Partager