package versusSNP;
import java.awt.BorderLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.ComponentAdapter;
import java.awt.event.ComponentEvent;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.io.File;
import java.util.Locale;
import javax.swing.JFileChooser;
import javax.swing.JFrame;
import javax.swing.JOptionPane;
import javax.swing.JTable;
import versusSNP.blast.BlastExecutor;
import versusSNP.blast.BlastParser;
import versusSNP.genome.Genome;
import versusSNP.gui.ChoosePanel;
import versusSNP.gui.Menu;
import versusSNP.gui.SeqPanel;
import versusSNP.gui.TabbedPane;
import versusSNP.gui.UICaption;
import versusSNP.gui.dialogs.AboutDialog;
import versusSNP.gui.dialogs.AlignmentLoaderDialog;
import versusSNP.gui.dialogs.HelpDialog;
import versusSNP.gui.dialogs.ORFLoaderDialog;
import versusSNP.gui.dialogs.OptionSheet;
import versusSNP.gui.dialogs.QuerySubjectSelectDialog;
import versusSNP.gui.dialogs.RunBlastDialog;
import versusSNP.gui.dialogs.SNPExporterDialog;
import versusSNP.gui.dialogs.TrimSequenceDialog;
import versusSNP.io.ExcelExporter;
import versusSNP.io.FastaFile;
import versusSNP.io.FileUtils;
import versusSNP.io.ORFFile;
import versusSNP.util.swing.SwingUtils;
public class VersusSNP extends JFrame implements ActionListener {
private static final long serialVersionUID = -2740630966808634894L;
private Menu menu;
private Document document;
private ChoosePanel choosePanel;
private SeqPanel seqPanel;
private TabbedPane tabbedPane;
public VersusSNP() {
super(UICaption.mainframe_title);
menu = new Menu(this);
document = new Document(menu);
choosePanel = new ChoosePanel(this);
seqPanel = new SeqPanel();
tabbedPane = new TabbedPane(document);
addComponentListener(new ComponentAdapter(){
@Override
public void componentResized(ComponentEvent e) {
if (isVisible()) {
if (getTabbedPane().getSize().width > getTabbedPane().getSPanel().getTable().getSize().width) {
getTabbedPane().getSPanel().getTable().setAutoResizeMode(JTable.AUTO_RESIZE_SUBSEQUENT_COLUMNS);
getTabbedPane().getNsPanel().getTable().setAutoResizeMode(JTable.AUTO_RESIZE_SUBSEQUENT_COLUMNS);
getTabbedPane().getSnpPanel().getTable().setAutoResizeMode(JTable.AUTO_RESIZE_SUBSEQUENT_COLUMNS);
getTabbedPane().getInPanel().getTable().setAutoResizeMode(JTable.AUTO_RESIZE_SUBSEQUENT_COLUMNS);
getTabbedPane().getDelPanel().getTable().setAutoResizeMode(JTable.AUTO_RESIZE_SUBSEQUENT_COLUMNS);
} else {
getTabbedPane().getSPanel().getTable().setAutoResizeMode(JTable.AUTO_RESIZE_OFF);
getTabbedPane().getNsPanel().getTable().setAutoResizeMode(JTable.AUTO_RESIZE_OFF);
getTabbedPane().getSnpPanel().getTable().setAutoResizeMode(JTable.AUTO_RESIZE_OFF);
getTabbedPane().getInPanel().getTable().setAutoResizeMode(JTable.AUTO_RESIZE_OFF);
getTabbedPane().getDelPanel().getTable().setAutoResizeMode(JTable.AUTO_RESIZE_OFF);
}
}
super.componentResized(e);
}
});
setDefaultCloseOperation(DO_NOTHING_ON_CLOSE);
addWindowListener(new WindowAdapter(){
@Override
public void windowIconified(WindowEvent e) {
getTabbedPane().getScatterPlotPanel().hideTipster();
super.windowIconified(e);
}
@Override
public void windowClosing(WindowEvent e) {
if (JOptionPane.showConfirmDialog(null, UICaption.dialog_label_exit, UICaption.dialog_caption_exit, JOptionPane.YES_NO_OPTION, JOptionPane.QUESTION_MESSAGE) == JOptionPane.YES_OPTION)
System.exit(0);
}
});
setSize(Size.main_frame);
SwingUtils.centerScreen(this);
setJMenuBar(menu);
document.addObserver(choosePanel);
getContentPane().add(choosePanel, BorderLayout.WEST);
getContentPane().add(seqPanel, BorderLayout.SOUTH);
getContentPane().add(tabbedPane, BorderLayout.CENTER);
}
public void init() {
document = new Document();
}
private void loopORFLoader(ORFLoaderDialog dialog) {
if (dialog.showDialog() == JOptionPane.OK_OPTION) {
if (dialog.getName().equals("")) {
JOptionPane.showMessageDialog(null, UICaption.dialog_error_orfloader_no_genome_name, UICaption.dialog_caption_error, JOptionPane.ERROR_MESSAGE);
loopORFLoader(dialog);
} else if (dialog.getPath1().equals("")) {
JOptionPane.showMessageDialog(null, UICaption.dialog_error_orfloader_no_orf_position, UICaption.dialog_caption_error, JOptionPane.ERROR_MESSAGE);
loopORFLoader(dialog);
} else if (dialog.getPath2().equals("")) {
JOptionPane.showMessageDialog(null, UICaption.dialog_error_orfloader_no_orf_sequence, UICaption.dialog_caption_error, JOptionPane.ERROR_MESSAGE);
loopORFLoader(dialog);
}
Parameter.path_previous_dir = dialog.getPath1().substring(0, dialog.getPath1().lastIndexOf(File.separator));
Genome genome = new Genome(dialog.getName());
new Thread(new ORFFile(dialog.getPath1(), dialog.getPath2(), genome, document, (dialog.getPath1().endsWith("csv") ? ORFFile.COMMA : ORFFile.TAB))).start();
}
}
private void loopAlignmentLoader(AlignmentLoaderDialog dialog) {
if (dialog.showDialog() == JOptionPane.OK_OPTION) {
if (dialog.getId1() == dialog.getId2()) {
JOptionPane.showMessageDialog(null, UICaption.dialog_error_querysubjectselect_identical_ids, UICaption.dialog_caption_error, JOptionPane.ERROR_MESSAGE);
loopAlignmentLoader(dialog);
}
if (dialog.getPath().equals("")) {
JOptionPane.showMessageDialog(null, UICaption.dialog_error_path_empty, UICaption.dialog_caption_error, JOptionPane.ERROR_MESSAGE);
loopAlignmentLoader(dialog);
}
document.setQueryGenome(dialog.getId1());
document.setSubjectGenome(dialog.getId2());
new Thread(new BlastParser(dialog.getPath(), document)).start();
}
}
private void loopSNPExporter(SNPExporterDialog dialog) {
if (dialog.showDialog() == JOptionPane.OK_OPTION) {
if (dialog.getPath().equals("")) {
JOptionPane.showMessageDialog(null, UICaption.dialog_error_save_path_empty, UICaption.dialog_caption_error, JOptionPane.ERROR_MESSAGE);
loopSNPExporter(dialog);
}
switch (dialog.getId()) {
case 0: new Thread(new ExcelExporter(tabbedPane.getSPanel().getTable(), dialog.getPath())).start(); break;
case 1: new Thread(new ExcelExporter(tabbedPane.getNsPanel().getTable(), dialog.getPath())).start(); break;
case 2: new Thread(new ExcelExporter(tabbedPane.getInPanel().getTable(), dialog.getPath())).start(); break;
case 3: new Thread(new ExcelExporter(tabbedPane.getDelPanel().getTable(), dialog.getPath())).start(); break;
case 4: new Thread(new ExcelExporter(tabbedPane.getSnpPanel().getTable(), dialog.getPath())).start(); break;
default: break;
}
}
}
private void loopBlastRunner(RunBlastDialog dialog) {
if (dialog.showDialog() == JOptionPane.OK_OPTION) {
if (dialog.getId1() == dialog.getId2()) {
JOptionPane.showMessageDialog(null, UICaption.dialog_error_querysubjectselect_identical_ids, UICaption.dialog_caption_error, JOptionPane.ERROR_MESSAGE);
loopBlastRunner(dialog);
} else if (!dialog.checkThresholdsFormat()) {
JOptionPane.showMessageDialog(null, UICaption.dialog_label_blast_runner_format_thresholds, UICaption.dialog_caption_error, JOptionPane.ERROR_MESSAGE);
loopBlastRunner(dialog);
}
if (Parameter.path_program_blastall == null || Parameter.path_program_blastall.equals("")) {
JOptionPane.showMessageDialog(null, UICaption.dialog_label_prompt_blast_path_blastall, UICaption.dialog_caption_error, JOptionPane.ERROR_MESSAGE);
return;
} else if (Parameter.path_program_formatdb == null || Parameter.path_program_formatdb.equals("")) {
JOptionPane.showMessageDialog(null, UICaption.dialog_label_prompt_blast_path_formatdb, UICaption.dialog_caption_error, JOptionPane.ERROR_MESSAGE);
return;
} else if (Parameter.temp_path == null || Parameter.temp_path.equals("")) {
JOptionPane.showMessageDialog(null, UICaption.dialog_label_prompt_temp_path, UICaption.dialog_caption_error, JOptionPane.ERROR_MESSAGE);
return;
}
document.setQueryGenome(dialog.getId1());
document.setSubjectGenome(dialog.getId2());
String qPath = Parameter.temp_path + Parameter.temp_output_sequence_query;
String sPath = Parameter.temp_path + Parameter.temp_output_sequence_subject;
String oPath = Parameter.temp_path + Parameter.temp_output_blast;
document.getQueryGenome().toFasta(qPath);
document.getSubjectGenome().toFasta(sPath);
new Thread(new BlastExecutor( qPath, sPath, oPath,
dialog.getMatrix(), dialog.getThresholds(), dialog.hasFilters(),
document)).start();
}
}
@Override
public void actionPerformed(ActionEvent ae) {
Object obj = ae.getSource();
if (obj == menu.getMenuFileNew()) {
seqPanel.clear();
tabbedPane.clear();
document.clear();
document.updateAllViews();
} else if (obj == menu.getMenuFileOpen()) {
loopORFLoader(new ORFLoaderDialog(document.getGenomeNames()));
} else if (obj == menu.getMenuFileExit()) {
if (JOptionPane.showConfirmDialog(null, UICaption.dialog_label_exit, UICaption.dialog_caption_exit, JOptionPane.YES_NO_OPTION, JOptionPane.QUESTION_MESSAGE) == JOptionPane.YES_OPTION)
System.exit(0);
} else if (obj == menu.getMenuFileOpenAlign()) {
loopAlignmentLoader(new AlignmentLoaderDialog(document.getGenomeNames()));
} else if (obj == menu.getMenuFileSaveAlign()) {
JFileChooser chooser = new JFileChooser();
if (chooser.showSaveDialog(null) == JFileChooser.APPROVE_OPTION) {
String blastPath = Parameter.temp_path + Parameter.temp_output_blast;
if (!new File(blastPath).exists()) {
new Thread(new FileUtils(blastPath, chooser.getSelectedFile().getAbsolutePath(), FileUtils.COPY)).start();
}
}
} else if (obj == menu.getMenuFileSaveSNP()) {
loopSNPExporter(new SNPExporterDialog());
} else if (obj == menu.getMenuEditChangeQuerySubject()) {
document.loopQuerySubjectSelect(new QuerySubjectSelectDialog(document.getGenomeNames()));
} else if (obj == menu.getMenuEditTrim()) {
new TrimSequenceDialog(document).showDialog();
} else if (obj == menu.getMenuToolOption()) {
new OptionSheet().setVisible(true);
} else if (obj == menu.getMenuLangEnglish()) {
new UICaption(Locale.ENGLISH);
menu.setLabels();
menu.setSelectedLang();
tabbedPane.setTitles();
tabbedPane.setHeaderTexts();
tabbedPane.setToolTipTexts();
} else if (obj == menu.getMenuLangSimpChinese()) {
new UICaption(Locale.SIMPLIFIED_CHINESE);
menu.setLabels();
menu.setSelectedLang();
tabbedPane.setTitles();
tabbedPane.setHeaderTexts();
tabbedPane.setToolTipTexts();
} else if (obj == menu.getMenuRunBlast()) {
loopBlastRunner(new RunBlastDialog(document.getGenomeNames()));
} else if (obj == menu.getMenuHelpHelp()) {
new HelpDialog().showDialog();
} else if (obj == menu.getMenuHelpAbout()) {
new AboutDialog().showDialog();
}
}
public ChoosePanel getChoosePanel() {
return choosePanel;
}
public SeqPanel getSeqPanel() {
return seqPanel;
}
public TabbedPane getTabbedPane() {
return tabbedPane;
}
}