Search in sources :

Example 1 with FastaFileFilter

use of uk.ac.babraham.SeqMonk.Utilities.FileFilters.FastaFileFilter 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)

Aggregations

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