Skip to content
Snippets Groups Projects
GoBox.java 17.3 KiB
Newer Older
tjc's avatar
tjc committed
/* GoBox.java
 *
 * created: 2007
 *
 * This file is part of Artemis
 *
 * Copyright (C) 2007  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.
 *
 **/

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

tjc's avatar
tjc committed
import java.awt.Color;
tjc's avatar
tjc committed
import java.awt.Cursor;
tjc's avatar
tjc committed
import java.awt.Dimension;
tjc's avatar
tjc committed
import java.awt.Font;
tjc's avatar
tjc committed
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
tjc's avatar
tjc committed
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.awt.event.MouseMotionAdapter;
tjc's avatar
tjc committed
import java.util.Vector;

import javax.swing.Box;
import javax.swing.JLabel;
tjc's avatar
tjc committed
import javax.swing.JOptionPane;
tjc's avatar
tjc committed
import javax.swing.JTextField;

import org.gmod.schema.cv.CvTerm;

tjc's avatar
tjc committed
import uk.ac.sanger.artemis.Options;
tjc's avatar
tjc committed
import uk.ac.sanger.artemis.components.genebuilder.JExtendedComboBox;
tjc's avatar
tjc committed
import uk.ac.sanger.artemis.components.SwingWorker;
import uk.ac.sanger.artemis.editor.BrowserControl;
tjc's avatar
tjc committed
import uk.ac.sanger.artemis.io.Qualifier;
import uk.ac.sanger.artemis.io.QualifierVector;
import uk.ac.sanger.artemis.util.DatabaseDocument;
import uk.ac.sanger.artemis.util.StringVector;

tjc's avatar
tjc committed
public class GoBox extends AbstractCvBox
tjc's avatar
tjc committed
{
  public static String[][] evidenceCodes = 
tjc's avatar
tjc committed
  { 
tcarver's avatar
tcarver committed
     {"EXP", "IC", "IDA", "IEA", "IEP", "IGC", 
       "IBA", "IBD", "IKR", "IRD", "IGI", 
tjc's avatar
tjc committed
      "IMP", "IPI", "ISA", "ISM", "ISO", "ISS", 
tjc's avatar
tjc committed
      "NAS", "ND", "RCA", "TAS", "NR" }, 
tjc's avatar
tjc committed
     {"EXP\t:: Inferred from Experiment",
      "IC \t:: Inferred by Curator",
      "IDA\t:: Inferred from Direct Assay",
      "IEA\t:: Inferred from Electronic Annotation",
      "IEP\t:: Inferred from Expression Pattern",
      "IGC\t:: Inferred from Genomic Context",
tcarver's avatar
tcarver committed
      "IBA\t:: Inferred from Biological aspect of Ancestor",
      "IBD\t:: Inferred from Biological aspect of Descendent",
      "IKR\t:: Inferred from Key Residues",
      "IRD\t:: Inferred from Rapid Divergence",
tjc's avatar
tjc committed
      "IGI\t:: Inferred from Genetic Interaction",
      "IMP\t:: Inferred from Mutant Phenotype",
      "IPI\t:: Inferred from Physical Interaction",
      "ISA\t:: Inferred from Sequence Alignment",
      "ISM\t:: Inferred from Sequence Model",
      "ISO\t:: Inferred from Sequence Orthology",
      "ISS\t:: Inferred from Sequence or Structural Similarity",
      "NAS\t:: Non-traceable Author Statement",
      "ND \t:: No biological Data available",
      "RCA\t:: inferred from Reviewed Computational Analysis",
      "TAS\t:: Traceable Author Statement",
      "NR \t:: Not Recorded"},
      { "Inferred from Experiment",
        "Inferred by Curator",
        "Inferred from Direct Assay",
        "Inferred from Electronic Annotation",
        "Inferred from Expression Pattern",
        "Inferred from Genomic Context",
tcarver's avatar
tcarver committed
        "Inferred from Biological aspect of Ancestor",
        "Inferred from Biological aspect of Descendent",
        "Inferred from Key Residues",
        "Inferred from Rapid Divergence",
tjc's avatar
tjc committed
        "Inferred from Genetic Interaction",
        "Inferred from Mutant Phenotype",
        "Inferred from Physical Interaction",
        "Inferred from Sequence Alignment",
        "Inferred from Sequence Model",
        "Inferred from Sequence Orthology",
        "Inferred from Sequence or Structural Similarity",
        "Non-traceable Author Statement",
        "No biological Data available",
        "inferred from Reviewed Computational Analysis",
        "Traceable Author Statement",
        "Not Recorded"}
tjc's avatar
tjc committed
  };
  
  private Dimension go_dimension;
tjc's avatar
tjc committed
  private static Dimension evidenceListDimension;
  
tjc's avatar
tjc committed
  private Box xBox;
  private int value_index;
  private JTextField withTextField;
  private JTextField dbxrefTextField;
  private JExtendedComboBox evidenceList;
  private JTextField qualfTextField;
tjc's avatar
tjc committed
  private DatePanel dateField;
tjc's avatar
tjc committed
  private String origQualifierString;
  private Qualifier origQualifier;
tjc's avatar
tjc committed
  private static Cursor cbusy = new Cursor(Cursor.WAIT_CURSOR);
  private static Cursor cdone = new Cursor(Cursor.DEFAULT_CURSOR);
  private static Cursor chand = new Cursor(Cursor.HAND_CURSOR);
  
  public static org.apache.log4j.Logger logger4j = 
    org.apache.log4j.Logger.getLogger(GoBox.class);
  
tjc's avatar
tjc committed
  private static String AMIGOURL;
tjc's avatar
tjc committed
  
  protected GoBox(final Qualifier qualifier,
                  final String qualifierString,
                  final int value_index,
                  Dimension go_dimension, final Dimension dimension)
  {
    this.origQualifier = qualifier;
    this.origQualifierString = qualifierString;
    this.go_dimension = go_dimension;
    this.value_index  = value_index;
    this.xBox = Box.createHorizontalBox();
    
    String goId = getField("GOid=", qualifierString);
    final String term = getField("term=", qualifierString);
tjc's avatar
tjc committed
    CvTerm cvTerm = getGOCvTerm(term);
    
tjc's avatar
tjc committed
    final JLabel goTermField = new JLabel(goId);
    addGoLabelLiteners(goTermField);
    
tjc's avatar
tjc committed
    JLabel goAspect = null;
    Font font = goTermField.getFont().deriveFont(Font.BOLD);
    
tjc's avatar
tjc committed
    if(cvTerm.getCv().getName().indexOf("molecular_function")>-1)
tjc's avatar
tjc committed
    {
      goAspect = new JLabel(" [F] ");
      goAspect.setForeground(Color.RED);
      goAspect.setFont(font);
    }
tjc's avatar
tjc committed
    else if(cvTerm.getCv().getName().indexOf("biological_process")>-1)
tjc's avatar
tjc committed
    {
      goAspect = new JLabel(" [P] ");
      goAspect.setForeground(Color.GREEN);
      goAspect.setFont(font);
    }
tjc's avatar
tjc committed
    else if(cvTerm.getCv().getName().indexOf("cellular_component")>-1)
tjc's avatar
tjc committed
    {
      goAspect = new JLabel(" [C] ");
      goAspect.setForeground(Color.BLUE);
      goAspect.setFont(font);
    }
tjc's avatar
tjc committed
    else
    {
      goAspect = new JLabel(" [?] ");
      goAspect.setForeground(Color.BLACK);
      goAspect.setFont(font);
    }
tjc's avatar
tjc committed
    
    if(go_dimension == null)
tjc's avatar
tjc committed
      this.go_dimension = new Dimension(goTermField.getPreferredSize().width+
                                        goAspect.getPreferredSize().width,
tjc's avatar
tjc committed
                                        goTermField.getPreferredSize().height);
    goTermField.setToolTipText(term);
    xBox.add(goTermField);
tjc's avatar
tjc committed
    xBox.add(goAspect);
tjc's avatar
tjc committed
    
    // the WITH column is associated with one or more FeatureCvTermDbXRef
    String with = getField("with=", qualifierString);
    withTextField = new JTextField(with);
tjc's avatar
tjc committed
    withTextField.setToolTipText("with/from column");
tjc's avatar
tjc committed
    withTextField.setPreferredSize(dimension);
    withTextField.setMaximumSize(dimension);
    withTextField.setActionCommand("with=");
    xBox.add(withTextField);
 

    // N.B. for /GO the db_xref is a Pub (for primary pubs) 
    //      or FeatureCvTermPub (for others) in /GO
    String dbxref = getField("db_xref=", qualifierString);
    dbxrefTextField = new JTextField(dbxref);
    dbxrefTextField.setToolTipText("dbxref column");
    dbxrefTextField.setPreferredSize(dimension);
    dbxrefTextField.setMaximumSize(dimension);
    dbxrefTextField.setActionCommand("db_xref=");
    xBox.add(dbxrefTextField);
 
    // feature_cvterm_prop's
    String evidence = getField("evidence=", qualifierString);
    
    evidenceList = new JExtendedComboBox(evidenceCodes[1]);
tjc's avatar
tjc committed
    evidenceList.setOpaque(false);
tjc's avatar
tjc committed
    evidenceList.setToolTipText("evidence column");
    evidenceList.setSelectedIndex( getEvidenceIndex(evidence) );
tjc's avatar
tjc committed
    evidenceList.addActionListener(new ActionListener()
    {
      public void actionPerformed(ActionEvent e)
      {
        if(((String)evidenceList.getSelectedItem()).startsWith("NR \t::"))
          JOptionPane.showMessageDialog(null, 
              "This evicence code is obsolete:\n"+
              evidenceList.getSelectedItem(), 
              "Obsolete Evidence Code", JOptionPane.WARNING_MESSAGE);
      } 
    });
tjc's avatar
tjc committed
    evidenceListDimension = evidenceList.getPreferredSize();
tjc's avatar
tjc committed
    evidenceListDimension = new Dimension(90,(int)evidenceListDimension.getHeight());
tjc's avatar
tjc committed
    evidenceList.setPreferredSize(evidenceListDimension);
    evidenceList.setMaximumSize(evidenceListDimension);
tjc's avatar
tjc committed
    evidenceList.setActionCommand("evidence=");
    xBox.add(evidenceList);
    
    String qual = getField("qualifier=", qualifierString);
    qualfTextField = new JTextField(qual);      
    qualfTextField.setToolTipText("qualifier column");
    qualfTextField.setPreferredSize(dimension);
    qualfTextField.setMaximumSize(dimension);
    qualfTextField.setActionCommand("qualifier=");
    xBox.add(qualfTextField);
    
tjc's avatar
tjc committed
    dateField = new DatePanel( getField("date=", qualifierString), 
tjc's avatar
tjc committed
                                        dimension.height); 
tcarver's avatar
tcarver committed
    xBox.add(dateField);
tjc's avatar
tjc committed
  }
  
tjc's avatar
tjc committed
  public static CvTerm getGOCvTerm(String term)
  {
    CvTerm cvTerm = DatabaseDocument.getCvTermByCvTermName(term);
    
    if(cvTerm.getCv().getName().indexOf("molecular_function") < 0 &&
       cvTerm.getCv().getName().indexOf("biological_process") < 0 &&
       cvTerm.getCv().getName().indexOf("cellular_component") < 0)
    {
      CvTerm thisCvTerm = DatabaseDocument.getCvTermByCvAndCvTerm(term,
                                                 "molecular_function");
      
      if(thisCvTerm == null)
        thisCvTerm = DatabaseDocument.getCvTermByCvAndCvTerm(term,
                                            "biological_process");
      
      if(thisCvTerm == null)
        thisCvTerm = DatabaseDocument.getCvTermByCvAndCvTerm(term,
                                            "cellular_component");
      if(thisCvTerm != null)
        cvTerm = thisCvTerm;
    }
    return cvTerm;
  }
  
tjc's avatar
tjc committed
  /**
   * Add GO listeners for opening Amigo.
   * @param goTermField
   */
  private void addGoLabelLiteners(final JLabel goTermField)
  {
    setAmigoUrl();
    goTermField.addMouseListener(new MouseAdapter()
    {
      public void mouseClicked(final MouseEvent e)
      {
        SwingWorker browserLaunch = new SwingWorker()
        {
          public Object construct()
          {
            if(e.getClickCount() == 1)
            {
              goTermField.setCursor(cbusy);
tcarver's avatar
tcarver committed
              BrowserControl.displayURL((AMIGOURL+goTermField.getText()).replaceFirst("GO:GO:", "GO:"));
tjc's avatar
tjc committed
              goTermField.setCursor(cdone);
            }
            return null;
          }
        };
        browserLaunch.start();
      }
    });
    
    goTermField.addMouseMotionListener(new MouseMotionAdapter()
    {
      public void mouseMoved(MouseEvent e)
      {
        goTermField.setCursor(chand);
      }
    });
  }
  
  /**
   * Set the Amigo URL for hyperlinking GO terms.
   */
  private void setAmigoUrl()
  {
    if(AMIGOURL == null)
    {
      StringVector dbsLinks = Options.getOptions().getOptionValues("hyperlinks");
      for(int i=0; i<dbsLinks.size(); i+=2)
      {
        if(dbsLinks.get(i).equals("GO"))
        {
tcarver's avatar
tcarver committed
          AMIGOURL = dbsLinks.get(i+1);
tjc's avatar
tjc committed
          return;
        }
      }
      AMIGOURL = "http://amigo.geneontology.org/cgi-bin/amigo/term-details.cgi?term=";
    }
  }
  
tjc's avatar
tjc committed
  protected static int getEvidenceIndex(String evidence)
  {
    for(int i=0; i<evidenceCodes[2].length; i++)
    {
      // look for full text or abbreviation of the code
      if(evidenceCodes[2][i].equalsIgnoreCase(evidence) ||
         evidenceCodes[0][i].equalsIgnoreCase(evidence))
        return i;
    }
    return -1;
  }
  
tjc's avatar
tjc committed
  protected Dimension getGoDimension()
  {
    return go_dimension;
  }
  
  protected Box getBox()
  {
    return xBox;
  }

  protected boolean isQualifierChanged()
  {
    String old = getField("with=", origQualifierString);
    if(!old.equals(withTextField.getText().trim()))
      return true;
    
    old = getField("db_xref=", origQualifierString);
    if(!old.equals(dbxrefTextField.getText().trim()))
      return true;
    
    old = getField("evidence=", origQualifierString);
    
    if(evidenceList.getSelectedIndex() > -1 &&
       !old.equalsIgnoreCase(evidenceCodes[2][ evidenceList.getSelectedIndex() ]))
tjc's avatar
tjc committed
      return true;
    
    old = getField("qualifier=", origQualifierString);
    if(!old.equals(qualfTextField.getText()))
      return true;
    
    old = getField("date=", origQualifierString);
    if(!old.equals(dateField.getText()))
      return true;
    
    return false;
  }
  
  protected int getValueIndex()
  {
    return value_index;  
  }
  
  /**
   * Update the qualifier from the GO form.
   */
tjc's avatar
tjc committed
  protected void updateQualifier(final QualifierVector qv)
  {
tjc's avatar
tjc committed
    int index = qv.indexOfQualifierWithName(origQualifier.getName());
    Qualifier oldQualifier = qv.getQualifierByName(origQualifier.getName());
tjc's avatar
tjc committed
    
    final String goId = getField("GOid=", origQualifierString);
    
    StringVector oldValues = oldQualifier.getValues();
tcarver's avatar
tcarver committed
    Vector<Integer> values_index = new Vector<Integer>();
    for(int i=0; i<oldValues.size(); i++)
tcarver's avatar
tcarver committed
      String oldValue = oldValues.get(i);
      String newGoId = getField("GOid=", oldValue);
tjc's avatar
tjc committed
      if(newGoId.equals(goId))
        values_index.add(new Integer(i));
    }
  
    if(values_index.size() > 0)
tcarver's avatar
tcarver committed
      String oldValue = oldValues.get(value_index);
      String oldGoId  = getField("GOid=", oldValue);
      
      if(!goId.equals(oldGoId))
        if(values_index.size() == 1)
tcarver's avatar
tcarver committed
          value_index = values_index.get(0).intValue();
          final String with = getField("with=", origQualifierString);
          final String evidence = getField("evidence=", origQualifierString);
          final String dbxref = getField("dbxref=", origQualifierString);
          for(int i=0; i<values_index.size(); i++)
tcarver's avatar
tcarver committed
            int ind = values_index.get(i).intValue();
            value_index = ind;
tcarver's avatar
tcarver committed
            String value = oldValues.get(ind);

            if(!with.equals(""))
            {
              String thisWith = getField("with=", value);
              if(thisWith.equals(with))
                break;
            }
            if(!dbxref.equals(""))
            {
              String thisDbxref = getField("dbxref=", value);
              if(thisDbxref.equals(dbxref))
                break;
            }
          
            String thisEvidence = getField("evidence=", value);
            if(thisEvidence.equals(evidence))
    else
      value_index = -99;
tjc's avatar
tjc committed
    if(value_index > -1)
      oldValues.remove(value_index);
tjc's avatar
tjc committed
    String updatedQualifierString = updateQualifierString();
tjc's avatar
tjc committed
    logger4j.debug(origQualifierString);
    logger4j.debug(updatedQualifierString);
    oldValues.add(value_index, updatedQualifierString);
tjc's avatar
tjc committed
    
    origQualifier = new Qualifier(origQualifier.getName(), oldValues);
tjc's avatar
tjc committed
    qv.remove(index);
    qv.add(index, origQualifier);
  }
  
  private String updateQualifierString()
  {
    String newQualifierString = origQualifierString;
    
    String old = getField("with=", origQualifierString);
    if(!old.equals(withTextField.getText().trim()))
      newQualifierString = changeField("with=", withTextField.getText().trim(), 
                                       newQualifierString);
    
    old = getField("db_xref=", origQualifierString);
    if(!old.equals(dbxrefTextField.getText().trim()))
      newQualifierString = changeField("db_xref=", dbxrefTextField.getText().trim(), 
                                       newQualifierString);
    
    old = getField("evidence=", origQualifierString);
    if(!old.equals(evidenceCodes[2][ evidenceList.getSelectedIndex() ]))
      newQualifierString = changeField("evidence=", evidenceCodes[2][ evidenceList.getSelectedIndex() ], 
tjc's avatar
tjc committed
                                       newQualifierString);
    
    old = getField("qualifier=", origQualifierString);
    if(!old.equals(qualfTextField.getText()))
      newQualifierString = changeField("qualifier=", qualfTextField.getText().trim(), 
                                       newQualifierString);
    
    old = getField("date=", origQualifierString);
    if(!old.equals(dateField.getText()))
      newQualifierString = changeField("date=", dateField.getText().trim(), 
                                       newQualifierString);
    return newQualifierString;
  }
tjc's avatar
tjc committed

  protected static Dimension getEvidenceListDimension()
tjc's avatar
tjc committed
  {
tjc's avatar
tjc committed
    if(evidenceListDimension == null)
    {
      JExtendedComboBox evidenceList = new JExtendedComboBox(evidenceCodes[1]);
      evidenceListDimension = evidenceList.getPreferredSize();
      evidenceListDimension = new Dimension(80,(int)evidenceListDimension.getHeight());
    }
tjc's avatar
tjc committed
    return evidenceListDimension;
  }
tjc's avatar
tjc committed
  
tjc's avatar
tjc committed
  /**
   * Given the string:
   * aspect=F;GOid=GO:0003674;term=molecular_function;evidence=No biological Data available
   * return the string:
   * aspect=F;GOid=GO:0003674;term=molecular_function;evidence=ND
   * @param goText
   * @return
   */
  public static String getEvidenceCodeGoTextFromText(String goText)
  {
    final String oldEvidence = getField("evidence=", goText); 
    String newEvidence = oldEvidence;
    
    for(int i=0; i<evidenceCodes[2].length; i++)
    {
      if(evidenceCodes[2][i].equalsIgnoreCase(oldEvidence.toLowerCase()))
      {
        newEvidence = evidenceCodes[0][i];
        break;
      }
    }
    
    if(!oldEvidence.equals(newEvidence))
      goText = goText.replaceAll(oldEvidence, newEvidence);
    return goText;
  }
tcarver's avatar
tcarver committed
}