view KinaMine-Galaxy-7-7/src/kinamine/KinaMineGUIController.java @ 0:67635b462045 draft

Uploaded
author jfb
date Tue, 20 Feb 2018 14:31:15 -0500
parents
children
line wrap: on
line source

/**
 *****************************************************************************
 * <p>
 * Copyright (c) Regents of the University of Minnesota. All Rights Reserved.
 * <p>
 * Author: Kevin Murray University of Minnesota - (murra668@umn.edu)
 * <p>
 *****************************************************************************
 */
package kinamine;

import java.io.File;
import java.net.URL;
import java.util.ArrayList;
import java.util.ResourceBundle;
import javafx.event.ActionEvent;
import javafx.fxml.FXML;
import javafx.fxml.Initializable;
import javafx.geometry.Pos;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.control.Label;
import javafx.scene.control.TextField;
import javafx.scene.layout.VBox;
import javafx.stage.DirectoryChooser;
import javafx.stage.FileChooser;
import javafx.stage.FileChooser.ExtensionFilter;
import javafx.stage.Modality;
import javafx.stage.Stage;

/**
 * Controller class for KinaMine GUI. Implements ActionEvents for GUI FXML frame
 * - built in Scene Builder.
 *
 * @version 1.0
 * @author murra668
 */
public class KinaMineGUIController implements Initializable {

    /**
     * Textfields for Peptide Report path, FASTA Database path, Output
     * Directory, FDR Score and Output Group Name.
     */
    @FXML
    private TextField pepPath, fastaPath, outPath, fdrScore, outGroup;

    /** Debug */
    private boolean debug;

    /** Location of last directory visited. */
    @FXML
    File lastDirectory;

    /**
     * Handles ActionEvent of pepBrowseButton being pressed. Opens file browser
     * for selection of peptide report tabular file. Stores location of the pep
     * report in the PepPath Textfield.
     *
     * @param event
     */
    @FXML
    public void pepSearchClicked(ActionEvent event) {

        /** Creates new FileChooser. */
        final FileChooser fileChooser = new FileChooser();

        /** Creates new ExtensionFilter for selection of tabular file. */
        ExtensionFilter filter
                = new ExtensionFilter("Tabular", "*.tabular", "*.txt");

        /** Configures FileChooser with parameters and last directory. */
        configFileChooser(fileChooser, lastDirectory, filter);

        /** Opens FileChooser window. */
        final File selectedFile
                = fileChooser.showOpenDialog(KinaMine.stage);

        /**
         * If the selected file is non-null, set the text of the pepPath
         * TextField to the absolute path of the file. Update last directory.
         */
        if (selectedFile != null) {
            lastDirectory = new File(selectedFile.getParent());
            pepPath.setText(selectedFile.getAbsolutePath());

        }
    }

    /**
     * Handles ActionEvent of fastaSearchButton being pressed. Opens a file
     * browser for the selection of a FASTA database and stores the location of
     * the file in the fastaPath Textfield.
     *
     * @param event
     */
    @FXML
    public void fastaSearchClicked(ActionEvent event) {

        /** Initialize new filechosoer. */
        final FileChooser fileChooser = new FileChooser();

        /** Create new extension filter */
        ExtensionFilter filter
                = new ExtensionFilter("Database", "*.fasta", "*.tabular");

        /** Configure file chooser */
        configFileChooser(fileChooser, lastDirectory, filter);

        /** Open file select window. */
        final File selectedFile
                = fileChooser.showOpenDialog(KinaMine.stage);

        /** Update fastapath and last directory, if not null */
        if (selectedFile != null) {
            lastDirectory = new File(selectedFile.getParent());
            fastaPath.setText(selectedFile.getAbsolutePath());

        }
    }

    /**
     * Configures FileChooser.
     *
     * @param fileChooser
     * @param lastDir
     * @param filter
     */
    private static void configFileChooser(final FileChooser fileChooser,
            File lastDir, ExtensionFilter filter) {

        fileChooser.setTitle("Open File");

        fileChooser.getExtensionFilters().add(filter);

        /** If last directory exists, start file selection from there. */
        if (lastDir == null) {
            fileChooser.setInitialDirectory(
                    new File(System.getProperty("user.home")));
        } else {
            fileChooser.setInitialDirectory(lastDir);
        }
    }

    /**
     * Handles ActionEvent that browse output was clicked. Opens directory
     * chooser and records path to directory chosen.
     *
     * @param event
     */
    public void browseFolderClicked(ActionEvent event) {

        /** Initialize new DirectoryChooser */
        final DirectoryChooser dirChooser = new DirectoryChooser();

        dirChooser.setTitle("Choose Output Location");

        /** Configure starting location for directory chooser. */
        if (lastDirectory != null) {
            dirChooser.setInitialDirectory(lastDirectory);
        } else {
            dirChooser.setInitialDirectory(new File(System.getProperty("user.home")));
        }

        /** Opens directory chooser window. */
        final File output = dirChooser.showDialog(KinaMine.stage);

        /** Set path if file not null */
        if (output != null) {
            outPath.setText(output.getAbsolutePath());
        }

    }

    /**
     * Handles event that submit button is clicked. Collects are required inputs
     * for KineMineDriver.
     *
     * @param event
     */
    public void submitButtonClicked(ActionEvent event) {

        /** Collect arguments */
        String args[] = new String[5];
        args[0] = pepPath.getText();
        args[1] = fastaPath.getText();
        args[2] = outPath.getText();
        args[3] = fdrScore.getText();
        args[4] = outGroup.getText();

        ArrayList<String> error = checkForErrors(args);

        /** Handle errors */
        if (error.isEmpty()) {
            boolean status = KinaMineDriver.run(args, debug = true);
            completeWindow();
        } else {
            alertError(error);
        }
    }

    public ArrayList<String> checkForErrors(String[] args) {

        ArrayList<String> error = new ArrayList<>();

        File pep = new File(args[0]);
        File fasta = new File(args[1]);
        File dir = new File(args[2]);
        String score = args[3];

        if (!pep.isFile()) {
            error.add(args[0] + " is not a file.");
        }
        if (!pep.canRead()) {
            error.add("Can't read peptide report: " + args[0]);
        }
        if (!fasta.isFile()) {
            error.add(args[1] + " is not a file.");
        }
        if (!fasta.canRead()) {
            error.add("Can't read database: " + args[0]);
        }
        if (!dir.isDirectory()){
            error.add(dir + " is not a directory.");
        }
        if (!score.matches("[-+]?\\d*\\.?\\d+")) {
            error.add("FDR Score: " + score + " is not a number.");
        }

        return error;
    }

    /**
     * If error in arguments for KinaMineDriver, open error window and write
     * error message to user.
     *
     * @param errors list of errors
     */
    public void alertError(ArrayList<String> errors) {

        /** Format window. */
        Stage window = new Stage();
        window.initModality(Modality.APPLICATION_MODAL);
        window.setTitle("Error");
        window.setMinWidth(250);

        /** Format message*/
        String message = "";
        for (String error : errors) {
            message += error + "\n";
        }
        Label label = new Label(message);
        
        /** Format Button */
        Button closeButton = new Button("Close\n");
        closeButton.setOnAction(e -> window.close());

        /** Format layout */
        VBox layout = new VBox(10);
        layout.getChildren().addAll(label, closeButton);
        layout.setAlignment(Pos.CENTER);

        /** Set scene */
        Scene scene = new Scene(layout);
        window.setScene(scene);
        window.showAndWait();

    }

    /**
     * Opens window when Run is complete.
     */
    public void completeWindow() {

        /** Format window. */
        Stage window = new Stage();
        window.initModality(Modality.APPLICATION_MODAL);
        window.setMinWidth(250);

        /** Format close button */
        Label label = new Label("Run Complete");
        Button closeButton = new Button("Done");
        closeButton.setOnAction(e -> window.close());

        /** Format layout */
        VBox layout = new VBox(10);
        layout.getChildren().addAll(label, closeButton);
        layout.setAlignment(Pos.CENTER);

        /** Set scene */
        Scene scene = new Scene(layout);
        window.setScene(scene);
        window.showAndWait();

    }

    /**
     * Initialize method for KinaMineGUI. Initializes all paths.
     *
     * @param url
     * @param rb
     */
    @Override
    public void initialize(URL url, ResourceBundle rb) {
        System.out.println("Initializing...");
        lastDirectory = null;
        assert pepPath != null : "fx:id=\"pepPath\" was not injected: check your FXML file 'KinaMineGUI.fxml'.";
        assert fastaPath != null : "fx:id=\"fastaPath\" was not injected: check your FXML file 'KinaMineGUI.fxml'.";
        assert outPath != null : "fx:id=\"outPath\" was not injected: check your FXML file 'KinaMineGUI.fxml'.";

    }

}