+ private void addSequencesFromFile() {
+ if ( ( getCurrentTreePanel() == null ) || ( getCurrentTreePanel().getPhylogeny() == null ) ) {
+ JOptionPane.showMessageDialog( this,
+ "Need to load evolutionary tree first",
+ "Can Not Read Sequences",
+ JOptionPane.WARNING_MESSAGE );
+ return;
+ }
+ final File my_dir = getCurrentDir();
+ if ( my_dir != null ) {
+ _sequences_filechooser.setCurrentDirectory( my_dir );
+ }
+ final int result = _sequences_filechooser.showOpenDialog( _contentpane );
+ final File file = _sequences_filechooser.getSelectedFile();
+ List<Sequence> seqs = null;
+ if ( ( file != null ) && !file.isDirectory() && ( result == JFileChooser.APPROVE_OPTION ) ) {
+ try {
+ if ( FastaParser.isLikelyFasta( new FileInputStream( file ) ) ) {
+ seqs = FastaParser.parse( new FileInputStream( file ) );
+ }
+ else {
+ JOptionPane.showMessageDialog( this,
+ "Format does not appear to be Fasta",
+ "Multiple sequence file format error",
+ JOptionPane.ERROR_MESSAGE );
+ return;
+ }
+ }
+ catch ( final MsaFormatException e ) {
+ setArrowCursor();
+ JOptionPane.showMessageDialog( this,
+ e.getLocalizedMessage(),
+ "Multiple sequence file format error",
+ JOptionPane.ERROR_MESSAGE );
+ return;
+ }
+ catch ( final IOException e ) {
+ setArrowCursor();
+ JOptionPane.showMessageDialog( this,
+ e.getLocalizedMessage(),
+ "Failed to read multiple sequence file",
+ JOptionPane.ERROR_MESSAGE );
+ return;
+ }
+ catch ( final Exception e ) {
+ setArrowCursor();
+ e.printStackTrace();
+ JOptionPane.showMessageDialog( this,
+ e.getLocalizedMessage(),
+ "Unexpected error during reading of multiple sequence file",
+ JOptionPane.ERROR_MESSAGE );
+ return;
+ }
+ if ( ( seqs == null ) || ( seqs.size() < 1 ) ) {
+ JOptionPane.showMessageDialog( this,
+ "Multiple sequence file is empty",
+ "Empty multiple sequence file",
+ JOptionPane.ERROR_MESSAGE );
+ setArrowCursor();
+ return;
+ }
+ }
+ if ( seqs != null ) {
+ for( final Sequence seq : seqs ) {
+ System.out.println( seq.getIdentifier() );
+ }
+ final Phylogeny phy = getCurrentTreePanel().getPhylogeny();
+ int total_counter = 0;
+ int attached_counter = 0;
+ for( final Sequence seq : seqs ) {
+ ++total_counter;
+ final String seq_name = seq.getIdentifier();
+ if ( !ForesterUtil.isEmpty( seq_name ) ) {
+ List<PhylogenyNode> nodes = phy.getNodesViaSequenceName( seq_name );
+ if ( nodes.isEmpty() ) {
+ nodes = phy.getNodesViaSequenceSymbol( seq_name );
+ }
+ if ( nodes.isEmpty() ) {
+ nodes = phy.getNodesViaGeneName( seq_name );
+ }
+ if ( nodes.isEmpty() ) {
+ nodes = phy.getNodes( seq_name );
+ }
+ if ( nodes.size() > 1 ) {
+ JOptionPane.showMessageDialog( this,
+ "Sequence name \"" + seq_name + "\" is not unique",
+ "Sequence name not unique",
+ JOptionPane.ERROR_MESSAGE );
+ setArrowCursor();
+ return;
+ }
+ final String[] a = seq_name.split( "\\s" );
+ if ( nodes.isEmpty() && ( a.length > 1 ) ) {
+ final String seq_name_split = a[ 0 ];
+ nodes = phy.getNodesViaSequenceName( seq_name_split );
+ if ( nodes.isEmpty() ) {
+ nodes = phy.getNodesViaSequenceSymbol( seq_name_split );
+ }
+ if ( nodes.isEmpty() ) {
+ nodes = phy.getNodes( seq_name_split );
+ }
+ if ( nodes.size() > 1 ) {
+ JOptionPane.showMessageDialog( this, "Split sequence name \"" + seq_name_split
+ + "\" is not unique", "Sequence name not unique", JOptionPane.ERROR_MESSAGE );
+ setArrowCursor();
+ return;
+ }
+ }
+ if ( nodes.size() == 1 ) {
+ ++attached_counter;
+ final PhylogenyNode n = nodes.get( 0 );
+ if ( !n.getNodeData().isHasSequence() ) {
+ n.getNodeData().addSequence( new org.forester.phylogeny.data.Sequence() );
+ }
+ n.getNodeData().getSequence().setMolecularSequence( seq.getMolecularSequenceAsString() );
+ if ( ForesterUtil.isEmpty( n.getNodeData().getSequence().getName() ) ) {
+ n.getNodeData().getSequence().setName( seq_name );
+ }
+ }
+ }
+ }
+ if ( attached_counter > 0 ) {
+ int ext_nodes = 0;
+ int ext_nodes_with_seq = 0;
+ for( final PhylogenyNodeIterator iter = phy.iteratorExternalForward(); iter.hasNext(); ) {
+ ++ext_nodes;
+ final PhylogenyNode n = iter.next();
+ if ( n.getNodeData().isHasSequence()
+ && !ForesterUtil.isEmpty( n.getNodeData().getSequence().getMolecularSequence() ) ) {
+ ++ext_nodes_with_seq;
+ }
+ }
+ final String s;
+ if ( ext_nodes == ext_nodes_with_seq ) {
+ s = "All " + ext_nodes_with_seq + " external nodes now have a molecular sequence attached to them.";
+ }
+ else {
+ s = ext_nodes_with_seq + " out of " + ext_nodes
+ + " external nodes now have a molecular sequence attached to them.";
+ }
+ if ( ( attached_counter == total_counter ) && ( ext_nodes == ext_nodes_with_seq ) ) {
+ JOptionPane.showMessageDialog( this,
+ "Attached all " + total_counter + " sequences to tree nodes.\n" + s,
+ "All sequences attached",
+ JOptionPane.INFORMATION_MESSAGE );
+ }
+ else {
+ JOptionPane.showMessageDialog( this, "Attached " + attached_counter
+ + " sequences out of a total of " + total_counter + " sequences.\n" + s, attached_counter
+ + " sequences attached", JOptionPane.WARNING_MESSAGE );
+ }
+ }
+ else {
+ JOptionPane.showMessageDialog( this, "No maching tree node for any of the " + total_counter
+ + " sequences", "Could not attach any sequences", JOptionPane.ERROR_MESSAGE );
+ }
+ }
+ }
+