Skip to content
Snippets Groups Projects
GeneComponentTree.java 6.36 KiB
Newer Older
tjc's avatar
tjc committed
/* GeneComponentTree.java
 *
 * created: 2006
 *
 * This file is part of Artemis
 *
 * Copyright(C) 2006  Genome Research Limited
 *
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License
 * as published by the Free Software Foundation; either version 2
 * of the License, or(at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
 *
tjc's avatar
tjc committed
 * $Header: //tmp/pathsoft/artemis/uk/ac/sanger/artemis/components/genebuilder/GeneComponentTree.java,v 1.5 2006-07-25 10:50:20 tjc Exp $
tjc's avatar
tjc committed
 */

package uk.ac.sanger.artemis.components.genebuilder;

import uk.ac.sanger.artemis.io.ChadoCanonicalGene;
import uk.ac.sanger.artemis.io.Feature;
import uk.ac.sanger.artemis.io.InvalidRelationException;
tjc's avatar
tjc committed
import uk.ac.sanger.artemis.chado.ChadoFeature;
tjc's avatar
tjc committed

import javax.swing.*;
import javax.swing.event.*;

import javax.swing.tree.*;
tjc's avatar
tjc committed
import java.util.List;
tjc's avatar
tjc committed
import java.util.Enumeration;
tjc's avatar
tjc committed

/**
 * Tree to display a gene hierarchy.
 */
public class GeneComponentTree extends JTree
{
  
  public GeneComponentTree(final ChadoCanonicalGene chado_gene,
tjc's avatar
tjc committed
                           final GeneBuilderFrame gene_builder)
tjc's avatar
tjc committed
  {
tjc's avatar
tjc committed
    final Feature gene = (Feature)chado_gene.getGene();
tjc's avatar
tjc committed
    final String gene_id;
    try
    {
      gene_id = (String)gene.getQualifierByName("ID").getValues().get(0);
      DefaultMutableTreeNode top =
           new DefaultMutableTreeNode(gene_id);

      createNodes(top, chado_gene);
      DefaultTreeModel model = new DefaultTreeModel(top);
      setModel(model);
    }
    catch(InvalidRelationException e)
    {
      e.printStackTrace();
    }
    
    //Listen for when a file is selected
    addTreeSelectionListener(new TreeSelectionListener()
    {
      public void valueChanged(TreeSelectionEvent e)
      {
        DefaultMutableTreeNode node = 
          (DefaultMutableTreeNode)GeneComponentTree.this.getLastSelectedPathComponent();
        if(node == null)
          return;
       
tjc's avatar
tjc committed
        Feature feature = (Feature)chado_gene.getFeatureFromId((String)node.getUserObject());
        gene_builder.setActiveFeature((uk.ac.sanger.artemis.Feature)feature.getUserData());
tjc's avatar
tjc committed
      }
    });
    
  }
  
  /**
   * Build the node hierarchy for this gene, i.e. transcript, exons, 
   * CDS, polypeptide.
   * @param gene_node
   * @param chado_gene
   * @throws InvalidRelationException
   */
  private void createNodes(final DefaultMutableTreeNode gene_node,
                           final ChadoCanonicalGene chado_gene) 
          throws InvalidRelationException 
  {
tjc's avatar
tjc committed
    List transcripts = chado_gene.getTranscripts();
    List exons;
tjc's avatar
tjc committed
    Feature transcript;
    Feature exon;
tjc's avatar
tjc committed
    Object  protein;
tjc's avatar
tjc committed
    String transcript_id;
    String exon_id;
    String protein_id;
    DefaultMutableTreeNode transcript_node;
    DefaultMutableTreeNode exon_node;
    DefaultMutableTreeNode protein_node;
    
    for(int i=0; i<transcripts.size(); i++)
    {
      transcript = (Feature)transcripts.get(i);
tjc's avatar
tjc committed
      transcript_id = 
        (String)transcript.getQualifierByName("ID").getValues().get(0);
tjc's avatar
tjc committed
      transcript_node = new DefaultMutableTreeNode(transcript_id);
      gene_node.add(transcript_node);
      
      exons = chado_gene.getExonsOfTranscript(transcript_id);
      if(exons == null)
        continue;

      for(int j=0; j<exons.size(); j++)
      {
tjc's avatar
tjc committed
        if(exons.get(j) instanceof Feature)
        {
          exon = (Feature)exons.get(j);
          exon_id = (String)exon.getQualifierByName("ID").getValues().get(0);
        }
        else  // ChadoFeature
          exon_id = ((ChadoFeature)exons.get(j)).getUniquename();
        
tjc's avatar
tjc committed
        exon_node = new DefaultMutableTreeNode(exon_id);
        transcript_node.add(exon_node);
      }
      
tjc's avatar
tjc committed
      protein = chado_gene.getProteinOfTranscript(transcript_id);
tjc's avatar
tjc committed
      if(protein == null)
        continue;
      
tjc's avatar
tjc committed
      if(protein instanceof Feature)
        protein_id = 
          (String)((Feature)protein).getQualifierByName("ID").getValues().get(0);
      else
        protein_id = ((ChadoFeature)protein).getUniquename();
      
tjc's avatar
tjc committed
      protein_node = new DefaultMutableTreeNode(protein_id);
      
      transcript_node.add(protein_node);
    }
  }
  
tjc's avatar
tjc committed
  /**
   * Change a node name.
   * @param old_id  the old uniquename of the feature
   * @param new_id  the new uniquename of the feature
   */
  protected void changeNode(String old_id, String new_id)
  {
    DefaultMutableTreeNode root = (DefaultMutableTreeNode)getModel().getRoot();
    
    DefaultMutableTreeNode change_node = searchChildren(root, old_id);
    if(change_node != null)
    {
      change_node.setUserObject(new_id);
      return;
    }
    
    Enumeration root_children = root.children();
    while(root_children.hasMoreElements())
    {
      DefaultMutableTreeNode child = 
           (DefaultMutableTreeNode)root_children.nextElement();
      
      change_node = searchChildren(child, old_id);
      if(change_node != null)
      {
        change_node.setUserObject(new_id);
        repaint();
        return;
      }
    }
  }
  
  /**
   * Delete a node and all its descendents from the tree.
   * @param id  the uniquename of the feature being removed
   */
  protected void deleteNode(final String id)
  {
    DefaultMutableTreeNode root = (DefaultMutableTreeNode)getModel().getRoot();
    Enumeration root_children = root.children();
    while(root_children.hasMoreElements())
    {
      DefaultMutableTreeNode child = 
           (DefaultMutableTreeNode)root_children.nextElement();
      
      if(id.equals((String)child.getUserObject()))
        ((DefaultTreeModel)getModel()).removeNodeFromParent(child);
    }
  }
  
  private DefaultMutableTreeNode searchChildren(
        final DefaultMutableTreeNode node,
        final String id)
  {
    Enumeration root_children = node.children();
    while(root_children.hasMoreElements())
    {
      DefaultMutableTreeNode child = 
           (DefaultMutableTreeNode)root_children.nextElement();

      if(id.equals((String)child.getUserObject()))
        return child;
    }
    return null;
  }
tjc's avatar
tjc committed
}