示例#1
0
文件: afDist.py 项目: jsgounot/afDist
	def initRegionViewer(self) :
		screen = QtGui.QDesktopWidget()
		second_screen = screen.screenGeometry(0)
		self.regionViewer = RegionViewer(self)
		self.regionViewer.move(second_screen.left(), second_screen.top())
示例#2
0
文件: afDist.py 项目: jsgounot/afDist
class ApplicationWindow(QtGui.QMainWindow):
	def __init__(self):
		QtGui.QMainWindow.__init__(self)
		self.setAttribute(QtCore.Qt.WA_DeleteOnClose)
		self.setWindowTitle("Allele frequency viewer")

		self.data = None
		self.genes = {}
		self.lastopeneddir = os.path.expanduser("~")

		self.initUI()
		self.init_menu()
		self.initRegionViewer()
		self.main_widget.setFocus()
		self.setCentralWidget(self.main_widget)
		self.showMaximized()

		self.tab_names = []
		#self.test()

	def test(self) :
		vcf = "/Volumes/CORSAIR/PoolsTeo_jan2016/vsYJM326/320_1.rg.dedup.realigned.raw.info.var.vcf"
		#vcf = "/media/jevanni/CORSAIR/PoolsTeo_jan2016/vsYJM326/320_1.rg.dedup.realigned.raw.info.var.vcf"
		fastafile = "./Anno/yjm326.fasta"
		gff3fname = "./Anno/sace_new.gff"
		self.load_vcf(vcf)
		self.load_annotation(fastafile, gff3fname) 

	def update_completer_gene(self) :
		model = QtGui.QStringListModel()
		model.setStringList(self.genes.keys())
		self.completer.setModel(model)

	def initUI(self) :
		self.main_widget = QtGui.QWidget(self)
		self.layout = QtGui.QVBoxLayout(self.main_widget)
		self.layout_top_right = QtGui.QHBoxLayout()
		self.layout_top = QtGui.QHBoxLayout()

		# Search bar with auto completer
		model = QtGui.QStringListModel()
		model.setStringList(self.genes.keys())
		self.completer = QtGui.QCompleter()
		self.completer.setModel(model)
		self.search_bar = QtGui.QLineEdit("Search gene")
		self.search_bar.keyPressEvent = self.search_bar_key_event
		self.search_bar.mousePressEvent = self.search_bar_mouse_event
		self.search_bar.setCompleter(self.completer)

		self.af_bar = QtGui.QLineEdit("Set af bar")
		self.af_bar.keyPressEvent = self.af_bar_key_event
		self.af_bar.mousePressEvent = self.af_bar_mouse_event
		
		self.chromosome_combobox = QtGui.QComboBox()
		self.start_bar = QtGui.QLineEdit("Start coordinate")
		self.start_bar.keyPressEvent = self.start_bar_key_event
		self.start_bar.mousePressEvent = self.start_bar_mouse_event
		self.end_bar = QtGui.QLineEdit("End coordinate")
		self.end_bar.keyPressEvent = self.end_bar_key_event
		self.end_bar.mousePressEvent = self.end_bar_mouse_event
		self.layout_top_right.addWidget(self.chromosome_combobox)
		self.layout_top_right.addWidget(self.start_bar)
		self.layout_top_right.addWidget(self.end_bar)
	
		self.layout_top.addWidget(self.search_bar)
		self.layout_top.addWidget(self.af_bar)
		self.layout_top.addLayout(self.layout_top_right)

		self.tab_widget = QtGui.QTabWidget(self)
		self.tab_widget.currentChanged.connect(self.change_tab)
		self.statut_bar = QtGui.QStatusBar(self)
		self.layout.addLayout(self.layout_top)
		self.layout.addWidget(self.tab_widget)
		self.layout.addWidget(self.statut_bar)

	def init_menu(self) :
		self.file_menu = QtGui.QMenu("&Files", self)
		self.file_menu.addAction("&Open files", self.load_annotation, QtCore.Qt.CTRL + QtCore.Qt.Key_O)
		self.menuBar().addMenu(self.file_menu)

	def initRegionViewer(self) :
		screen = QtGui.QDesktopWidget()
		second_screen = screen.screenGeometry(0)
		self.regionViewer = RegionViewer(self)
		self.regionViewer.move(second_screen.left(), second_screen.top())

	def load_vcf(self, fname=None) :
		if not fname : fname = str(QtGui.QFileDialog.getOpenFileName(self, 'Open VCF file', self.lastopeneddir))
		if not fname : return False
		self.data = DataManager(self, fname)
		self.setWindowTitle("Allele frequency viewer : %s" %os.path.basename(fname))
		self.chromosome_combobox.addItems(self.data.get_chromosomes())
		self.lastopeneddir = os.path.dirname(fname)
		self.show_plot()
		return True

	def load_annotation(self, fasta=None, annofname=None) :
		loaded = True
		if not self.data : loaded = self.load_vcf()
		if not loaded : return
		if not fasta : fasta = str(QtGui.QFileDialog.getOpenFileName(self, 'Open Fasta file', self.lastopeneddir))
		if not annofname : annofname = str(QtGui.QFileDialog.getOpenFileName(self, 'Open annotation file', self.lastopeneddir))

		if not fasta or not annofname : return

		self.annotations = get_feature_gff3(annofname, fasta, self.data)
		self.genes = self.annotations.get_dict_parents()
		self.update_completer_gene()
		self.lastopeneddir = os.path.dirname(annofname)

	def show_plot(self, chromosome=None) :
		name = chromosome if chromosome else "Genome"
		if name not in self.tab_names :
			canva = self.data.get_canva(chromosome=chromosome)
			self.tab_widget.addTab(canva, name)
			self.tab_names.append(name)

		self.tab_widget.setCurrentIndex(self.tab_names.index(name))
		self.get_current_tabwidget().update_info()

	def get_current_tabwidget(self) :
		return self.tab_widget.currentWidget()

	def update_statut_whole(self, chromosome=None) :
		info = self.data.get_statut(chromosome=chromosome)

	def search_bar_mouse_event(self, event) :
		if event.button() == 1 and self.search_bar.text() == "Search gene" :
			self.search_bar.setText("")

	def search_bar_key_event(self, event) :
		if event.text() == '\r' : self.zone_gene(self.search_bar.text())
		else : QtGui.QLineEdit.keyPressEvent(self.search_bar, event)

	def zone_gene(self, gene_name) :
		annotation = self.genes.get(str(gene_name), None)
		if not annotation : return

		chromosome, start, end = annotation["seqname"], annotation["start"], annotation["end"]
		self.show_plot(chromosome=chromosome)
		self.get_current_tabwidget().draw_region(start, end)
		self.get_current_tabwidget().send_info()

	def af_bar_key_event(self, event) :
		if event.text() == '\r' : self.set_bar(self.af_bar.text())
		else : QtGui.QLineEdit.keyPressEvent(self.af_bar, event)

	def af_bar_mouse_event(self, event) :
		if event.button() == 1 and self.af_bar.text() == "Set af bar" :
			self.af_bar.setText("")

	def start_bar_mouse_event(self, event) :
		if event.button() == 1 and self.start_bar.text() == "Start coordinate" :
			self.start_bar.setText("")

	def end_bar_mouse_event(self, event) :
		if event.button() == 1 and self.end_bar.text() == "End coordinate" :
			self.end_bar.setText("")

	def start_bar_key_event(self, event) :
		if event.text() == '\r' : self.set_coordinate_manualy()
		else : QtGui.QLineEdit.keyPressEvent(self.start_bar, event)

	def end_bar_key_event(self, event) :
		if event.text() == '\r' : self.set_coordinate_manualy()
		else : QtGui.QLineEdit.keyPressEvent(self.end_bar, event)

	def set_coordinate_manualy(self) :
		chromosome = str(self.chromosome_combobox.currentText())
		start = str(self.start_bar.text())
		end = str(self.end_bar.text())
		try :
			start, end = int(start), int(end)
		except ValueError :
			return

		self.show_plot(chromosome=chromosome)
		self.get_current_tabwidget().draw_region(start, end)

	def set_bar(self, value) :
		self.get_current_tabwidget().remove_line()
		try : value = float(value)
		except ValueError : return
		if value > 1 or value < 0 : return
		self.get_current_tabwidget().draw_bar(value)

	def change_tab(self) :
		self.get_current_tabwidget().update_info()

	def closeEvent(self, ce) :
		if self.regionViewer :
			self.regionViewer.close()