Search in sources :

Example 1 with GFFFileFilter

use of uk.ac.babraham.SeqMonk.Utilities.FileFilters.GFFFileFilter in project SeqMonk by s-andrews.

the class ManualGenomeBuilderPanel method actionPerformed.

public void actionPerformed(ActionEvent ae) {
    if (ae.getActionCommand().equals("add")) {
        String newName = (String) JOptionPane.showInputDialog(this, "Region name", "Add region", JOptionPane.QUESTION_MESSAGE, null, null, "");
        if (newName != null) {
            manualGenome.getChromosome(newName);
        }
    } else if (ae.getActionCommand().equals("gff")) {
        JFileChooser chooser = new JFileChooser(SeqMonkPreferences.getInstance().getDataLocation());
        chooser.setMultiSelectionEnabled(false);
        chooser.setFileFilter(new GFFFileFilter());
        int result = chooser.showOpenDialog(this);
        /*
			 * There seems to be a bug in the file chooser which allows the user to
			 * select no files, but not cancel if the control+double click on a file
			 */
        if (result == JFileChooser.CANCEL_OPTION || chooser.getSelectedFile() == null) {
            return;
        }
        SeqMonkPreferences.getInstance().setLastUsedDataLocation(chooser.getSelectedFile());
        new SimpleGFFReader(chooser.getSelectedFile());
        gffFiles.add(chooser.getSelectedFile());
    } else if (ae.getActionCommand().equals("pseudo")) {
        if (pseudoBox.isSelected()) {
            int pseudoNumber = 25;
            if (pseudoNumberField.getText().length() > 0) {
                pseudoNumber = Integer.parseInt(pseudoNumberField.getText());
            }
            manualGenome.addPseudoGenome(pseudoNumber);
        } else {
            manualGenome.removePseudoGenome();
        }
    } else if (ae.getActionCommand().equals("fasta")) {
        JFileChooser chooser = new JFileChooser(SeqMonkPreferences.getInstance().getDataLocation());
        chooser.setMultiSelectionEnabled(true);
        chooser.setFileFilter(new FastaFileFilter());
        int result = chooser.showOpenDialog(this);
        /*
			 * There seems to be a bug in the file chooser which allows the user to
			 * select no files, but not cancel if the control+double click on a file
			 */
        if (result == JFileChooser.CANCEL_OPTION || chooser.getSelectedFile() == null) {
            return;
        }
        SeqMonkPreferences.getInstance().setLastUsedDataLocation(chooser.getSelectedFile());
        new SimpleFastaReader(chooser.getSelectedFiles());
    } else if (ae.getActionCommand().equals("remove")) {
        int[] rows = manualChrTable.getSelectedRows();
        ManualGenomeChromosome[] chrsToRemove = new ManualGenomeChromosome[rows.length];
        for (int i = 0; i < rows.length; i++) {
            chrsToRemove[i] = manualGenome.getChromosome(manualChrTable.convertRowIndexToModel(rows[i]));
        }
        manualGenome.removeChromosome(chrsToRemove);
    } else if (ae.getActionCommand().equals("create")) {
        // Check if the species name is set and make the folder if needs be
        String speciesName = speciesField.getText().trim();
        if (speciesName.length() == 0) {
            reportError("Species name isn't filled in");
            return;
        }
        // Check that the assembly name is set
        String assemblyName = assemblyField.getText().trim();
        if (assemblyName.length() == 0) {
            reportError("Assembly name isn't filled in");
            return;
        }
        // Check that we have some chromosomes
        if (manualGenome.getRowCount() == 0) {
            reportError("You need to define some chromosomes before creating your genome");
            return;
        }
        // Check to see if they're making a silly number of chromosomes
        if (manualGenome.getRowCount() > 100 && !pseudoBox.isSelected()) {
            if (JOptionPane.showConfirmDialog(this, "<html>You're making a very large number of chromosomes which won't work well.<br>You should probably choose to make pseudo chromosomes for this genome.<br><br>Really continue?</html>", "That's a lot of chromosomes", JOptionPane.OK_CANCEL_OPTION) != JOptionPane.OK_OPTION)
                return;
            JOptionPane.showMessageDialog(this, "OK, but don't say we didn't warn you", "Going ahead", JOptionPane.WARNING_MESSAGE);
        }
        // See if the species folder exists already and make it if it doesn't
        File speciesFolder = null;
        try {
            speciesFolder = new File(SeqMonkPreferences.getInstance().getGenomeBase().getAbsolutePath() + "/" + speciesName);
            if (!speciesFolder.exists()) {
                if (!speciesFolder.mkdir()) {
                    reportError("Failed to make " + speciesFolder + " folder");
                    return;
                }
            }
        } catch (IOException ioe) {
            throw new IllegalStateException(ioe);
        }
        // Make the assembly folder and bail out if it exists
        File assemblyFolder = new File(speciesFolder.getAbsolutePath() + "/" + assemblyName);
        if (assemblyFolder.exists()) {
            reportError("There is already an assembly called " + speciesName + "/" + assemblyName + " you need to delete this manually before creating a new genome of the same name");
            return;
        }
        if (!assemblyFolder.mkdir()) {
            reportError("Failed to make " + assemblyFolder + " folder");
            return;
        }
        // Copy over the gff files
        try {
            for (int f = 0; f < gffFiles.size(); f++) {
                FileOutputStream fos = new FileOutputStream(new File(assemblyFolder.getAbsolutePath() + "/" + gffFiles.elementAt(f).getName()));
                // PrintWriter pr = new PrintWriter(new File(assemblyFolder.getAbsolutePath()+"/"+gffFiles.elementAt(f).getName()));
                FileInputStream fis = new FileInputStream(gffFiles.elementAt(f));
                // BufferedReader br = new BufferedReader(new FileReader(gffFiles.elementAt(f)));
                byte[] buffer = new byte[1024];
                int noOfBytes = 0;
                while ((noOfBytes = fis.read(buffer)) != -1) {
                    fos.write(buffer, 0, noOfBytes);
                }
                fis.close();
                fos.close();
            }
            // Write out chromosome lengths along with aliases for pseudo chromosomes
            manualGenome.writeGenomeFiles(assemblyFolder);
        } catch (IOException ioe) {
            throw new IllegalStateException(ioe);
        }
        JOptionPane.showMessageDialog(this, "Successfully created new custom genome", "Success", JOptionPane.INFORMATION_MESSAGE);
        if (parent != null) {
            parent.setVisible(false);
            parent.dispose();
        }
    }
}
Also used : GFFFileFilter(uk.ac.babraham.SeqMonk.Utilities.FileFilters.GFFFileFilter) FastaFileFilter(uk.ac.babraham.SeqMonk.Utilities.FileFilters.FastaFileFilter) IOException(java.io.IOException) FileInputStream(java.io.FileInputStream) JFileChooser(javax.swing.JFileChooser) FileOutputStream(java.io.FileOutputStream) File(java.io.File)

Example 2 with GFFFileFilter

use of uk.ac.babraham.SeqMonk.Utilities.FileFilters.GFFFileFilter in project SeqMonk by s-andrews.

the class ReportTableDialog method actionPerformed.

/* (non-Javadoc)
	 * @see java.awt.event.ActionListener#actionPerformed(java.awt.event.ActionEvent)
	 */
public void actionPerformed(ActionEvent ae) {
    if (ae.getActionCommand().equals("close")) {
        setVisible(false);
        dispose();
    } else if (ae.getActionCommand().equals("save")) {
        JFileChooser chooser = new JFileChooser(SeqMonkPreferences.getInstance().getSaveLocation());
        chooser.setMultiSelectionEnabled(false);
        if (report.canExportGFF()) {
            chooser.addChoosableFileFilter(new GFFFileFilter());
        }
        TxtFileFilter txtff = new TxtFileFilter();
        chooser.addChoosableFileFilter(txtff);
        chooser.setFileFilter(txtff);
        int result = chooser.showSaveDialog(this);
        if (result == JFileChooser.CANCEL_OPTION)
            return;
        File file = chooser.getSelectedFile();
        SeqMonkPreferences.getInstance().setLastUsedSaveLocation(file);
        if (file.isDirectory())
            return;
        FileFilter filter = chooser.getFileFilter();
        if (filter instanceof TxtFileFilter) {
            if (!file.getPath().toLowerCase().endsWith(".txt")) {
                file = new File(file.getPath() + ".txt");
            }
        } else if (filter instanceof GFFFileFilter) {
            if (!file.getPath().toLowerCase().endsWith(".gff")) {
                file = new File(file.getPath() + ".gff");
            }
        } else {
            System.err.println("Unknown file filter type " + filter + " when saving image");
            return;
        }
        // Check if we're stepping on anyone's toes...
        if (file.exists()) {
            int answer = JOptionPane.showOptionDialog(this, file.getName() + " exists.  Do you want to overwrite the existing file?", "Overwrite file?", 0, JOptionPane.QUESTION_MESSAGE, null, new String[] { "Overwrite and Save", "Cancel" }, "Overwrite and Save");
            if (answer > 0) {
                return;
            }
        }
        try {
            if (filter instanceof TxtFileFilter) {
                saveTextReport(file);
            } else if (filter instanceof GFFFileFilter) {
                saveGFFReport(file);
            } else {
                System.err.println("Unknown file filter type " + filter + " when saving image");
                return;
            }
        } catch (IOException e) {
            throw new IllegalStateException(e);
        }
    }
}
Also used : GFFFileFilter(uk.ac.babraham.SeqMonk.Utilities.FileFilters.GFFFileFilter) JFileChooser(javax.swing.JFileChooser) TxtFileFilter(uk.ac.babraham.SeqMonk.Utilities.FileFilters.TxtFileFilter) IOException(java.io.IOException) TxtFileFilter(uk.ac.babraham.SeqMonk.Utilities.FileFilters.TxtFileFilter) FileFilter(javax.swing.filechooser.FileFilter) GFFFileFilter(uk.ac.babraham.SeqMonk.Utilities.FileFilters.GFFFileFilter) File(java.io.File)

Aggregations

File (java.io.File)2 IOException (java.io.IOException)2 JFileChooser (javax.swing.JFileChooser)2 GFFFileFilter (uk.ac.babraham.SeqMonk.Utilities.FileFilters.GFFFileFilter)2 FileInputStream (java.io.FileInputStream)1 FileOutputStream (java.io.FileOutputStream)1 FileFilter (javax.swing.filechooser.FileFilter)1 FastaFileFilter (uk.ac.babraham.SeqMonk.Utilities.FileFilters.FastaFileFilter)1 TxtFileFilter (uk.ac.babraham.SeqMonk.Utilities.FileFilters.TxtFileFilter)1