public boolean testPredicate(Feature targetFeature) {
    String targetKey = targetFeature.getKey().getKeyString();
    if (sameKey && !targetKey.equals(key)) return false;

    Vector<String> chadoNames = null;
    if (isDatabaseEntry) {
      GFFStreamFeature gffFeature = ((GFFStreamFeature) targetFeature.getEmblFeature());
      if (gffFeature.getChadoGene() != null) {
        chadoNames = new Vector<String>();

        ChadoCanonicalGene chadoGene = gffFeature.getChadoGene();
        chadoNames.add(chadoGene.getGeneUniqueName());
        List<uk.ac.sanger.artemis.io.Feature> transcripts = chadoGene.getTranscripts();
        for (int i = 0; i < transcripts.size(); i++) {
          GFFStreamFeature feature = (GFFStreamFeature) transcripts.get(i);
          chadoNames.add(GeneUtils.getUniqueName(feature));
        }
      }
    }

    String thisFeatureSystematicName = targetFeature.getSystematicName();
    for (int i = 0; i < geneNames.length; i++) {
      if (geneNames[i].equals(thisFeatureSystematicName)
          || (chadoNames != null && chadoNames.contains(geneNames[i]))) {
        geneName = geneNames[i];
        return true;
      }
    }
    return false;
  }
 private static String getGeneName(Feature feature) {
   try {
     return ((GFFStreamFeature) feature.getEmblFeature()).getChadoGene().getGeneUniqueName();
   } catch (Exception e) {
   }
   return null;
 }
  /**
   * Tool for transferring annotation from one feature to other feature(s)
   *
   * @param feature
   * @param entryGroup
   * @param geneNames
   */
  public TransferAnnotationTool(
      final Feature feature, final EntryGroup entryGroup, final MatchPanel matchPanel) {
    super("Transfer Annotation Tool :: " + feature.getIDString());
    this.matchPanel = matchPanel;

    List<String> geneNames = null;
    if (matchPanel != null) geneNames = matchPanel.getGeneNameList();

    JPanel panel = new JPanel(new FlowLayout(FlowLayout.LEFT));
    JPanel pane = new JPanel(new GridBagLayout());
    JScrollPane jsp = new JScrollPane(panel);
    panel.setBackground(Color.white);
    pane.setBackground(Color.white);
    panel.add(pane);

    JPanel framePanel = (JPanel) getContentPane();
    framePanel.add(jsp, BorderLayout.CENTER);
    framePanel.setPreferredSize(new Dimension(600, 600));

    final Vector<JCheckBox> geneNameCheckBoxes = new Vector<JCheckBox>();
    final Vector<QualifierPanel> qualifierPanels = new Vector<QualifierPanel>();

    addMainPanel(feature, pane, qualifierPanels, geneNameCheckBoxes, geneNames);
    addBottomButtons(
        qualifierPanels, geneNameCheckBoxes,
        framePanel, entryGroup);
    pack();
    setVisible(true);
  }
Ejemplo n.º 4
0
  private void updateFromFeature(Feature feature) {
    GridBagConstraints c = new GridBagConstraints();
    JPanel gridPanel = new JPanel(new GridBagLayout());
    gridPanel.setBackground(Color.white);

    //
    // literature & dbxref
    literatureTextArea = new QualifierTextArea();
    literatureTextArea.setBorder(BorderFactory.createLineBorder(Color.gray));
    dbxrefTextArea = new QualifierTextArea();
    dbxrefTextArea.setBorder(BorderFactory.createLineBorder(Color.gray));

    literatureTextArea
        .getDocument()
        .addDocumentListener(new TextAreaDocumentListener(literatureTextArea));

    dbxrefTextArea.getDocument().addDocumentListener(new TextAreaDocumentListener(dbxrefTextArea));

    final QualifierVector qualifiers = feature.getQualifiers().copy();
    final StringBuffer litBuffer = new StringBuffer();
    final StringBuffer dbxrefBuffer = new StringBuffer();

    for (int i = 0; i < qualifiers.size(); ++i) {
      Qualifier this_qualifier = (Qualifier) qualifiers.elementAt(i);
      if (this_qualifier.getName().equals("literature"))
        appendToBuffer(this_qualifier.getValues(), litBuffer);
      else if (this_qualifier.getName().equalsIgnoreCase("Dbxref"))
        appendToBuffer(this_qualifier.getValues(), dbxrefBuffer);
    }

    c.gridx = 0;
    c.gridy = 0;
    c.ipadx = 5;
    c.ipady = 5;
    c.anchor = GridBagConstraints.NORTHWEST;
    c.fill = GridBagConstraints.NONE;
    gridPanel.add(new JLabel("Literature"), c);
    c.gridx = 1;
    gridPanel.add(literatureTextArea, c);

    c.gridx = 0;
    c.gridy = 1;
    gridPanel.add(new JLabel("Dbxref"), c);
    c.gridx = 1;
    gridPanel.add(dbxrefTextArea, c);

    add(gridPanel);

    literatureTextArea.setText(litBuffer.toString() + "\n");
    dbxrefTextArea.setText(dbxrefBuffer.toString() + "\n");
  }
  /**
   * Add a panel to display a given features qualifiers.
   *
   * @param f
   * @param qualifierPanels
   * @param c
   * @param nrows
   * @param pane
   */
  private void addQualifierPanel(
      Feature f,
      Vector<QualifierPanel> qualifierPanels,
      GridBagConstraints c,
      int nrows,
      JPanel pane) {
    QualifierPanel qPanel = new QualifierPanel(f, f.getKey().getKeyString());
    if (qPanel.nrows == 0) return;

    c.fill = GridBagConstraints.HORIZONTAL;
    c.anchor = GridBagConstraints.WEST;
    c.weightx = 100;
    qualifierPanels.add(qPanel);
    c.gridy = ++nrows;

    JLabel l = new JLabel(f.getIDString());
    l.setFont(l.getFont().deriveFont(Font.BOLD));
    l.setForeground(STEEL_BLUE);
    pane.add(l, c);

    c.gridy = ++nrows;
    pane.add(qPanel, c);
    c.weightx = 0.d;
  }
  /**
   * @param features
   * @param qualifiersToTransfer
   * @param key
   * @param sameKey
   * @param isDatabaseEntry
   * @param geneNames
   * @return
   */
  private static String[] transfer(
      final FeatureVector features,
      final QualifierVector qualifiersToTransfer,
      final String key,
      final boolean sameKey,
      final boolean overwrite,
      final boolean isDatabaseEntry,
      String[] geneNames,
      final StringBuffer genesUpdated) {
    final TransferFeaturePredicate predicate =
        new TransferFeaturePredicate(key, sameKey, isDatabaseEntry, geneNames);

    for (int i = 0; i < features.size(); i++) {
      Feature thisFeature = features.elementAt(i);
      if (predicate.testPredicate(thisFeature)) {
        StringBuffer qualifierBuffer = new StringBuffer();
        for (int j = 0; j < qualifiersToTransfer.size(); j++) {
          Qualifier newQualifier = qualifiersToTransfer.elementAt(j);
          String qualifierName = newQualifier.getName();
          try {
            if (overwrite) {
              thisFeature.setQualifier(newQualifier);
              qualifierBuffer.append(
                  "  "
                      + qualifierName
                      + " (overwritten)\n"
                      + parseStringVector(newQualifier.getValues()));
            } else {
              final StringVector oldValues;
              if (thisFeature.getQualifierByName(newQualifier.getName()) == null) oldValues = null;
              else oldValues = thisFeature.getQualifierByName(newQualifier.getName()).getValues();

              final Qualifier newQualifierTmp =
                  getQualifierWithoutDuplicateValues(newQualifier, oldValues);
              if (newQualifierTmp == null) continue;
              thisFeature.addQualifierValues(newQualifierTmp);
              qualifierBuffer.append(
                  "  "
                      + qualifierName
                      + " (added)\n"
                      + parseStringVector(newQualifier.getValues()));
            }
          } catch (Exception e1) {
            e1.printStackTrace();
          }
        }

        geneNames = removeArrayElement(geneNames, predicate.getGeneName());
        if (qualifierBuffer.length() > 0)
          genesUpdated.append(
              thisFeature.getSystematicName() + " (" + key + ")\n" + qualifierBuffer);
      }
    }
    return geneNames;
  }
  public QualifierPanel(Feature feature, String title) {
    super(new GridBagLayout());

    this.feature = feature;

    TitledBorder titleBorder =
        BorderFactory.createTitledBorder(
            BorderFactory.createEtchedBorder(EtchedBorder.LOWERED), title);
    titleBorder.setTitleJustification(TitledBorder.LEFT);
    titleBorder.setTitleColor(TransferAnnotationTool.STEEL_BLUE);
    setBorder(titleBorder);

    GridBagConstraints c = new GridBagConstraints();
    c.anchor = GridBagConstraints.WEST;
    c.ipadx = 0;
    final QualifierVector qualifiers = feature.getQualifiers();

    for (int i = 0; i < qualifiers.size(); i++) {
      nrows = addQualifierComponents(qualifiers.get(i), qualifierCheckBoxes, c, nrows);
    }

    setMinimumSize(new Dimension(titleBorder.getMinimumSize(this).width, getMinimumSize().height));
  }
  /**
   * Change the stop_codon_redefined_as_selenocysteine SO qualifier to the transl_except EMBL
   * qualifier.
   *
   * @param qualifiers
   * @param feature
   */
  private void handleSelenocysteine(QualifierVector qualifiers, Feature feature) {
    if (!feature.getKey().getKeyString().equals(DatabaseDocument.EXONMODEL)) return;
    qualifiers.removeQualifierByName("stop_codon_redefined_as_selenocysteine");

    uk.ac.sanger.artemis.Feature f = ((uk.ac.sanger.artemis.Feature) feature.getUserData());

    int translatedBasePosion = 0;
    String aa = f.getTranslation().toString();
    for (int i = 0; i < aa.length(); i++) {
      if (AminoAcidSequence.isStopCodon(aa.charAt(i))) {
        translatedBasePosion = i * 3;
        break;
      }
    }

    FeatureSegmentVector segments = f.getSegments();
    int nbases = 0;
    int sequenceloc = 0;
    for (int i = 0; i < segments.size(); i++) {
      int seglen = segments.elementAt(i).getBases().length();
      if (nbases + seglen > translatedBasePosion) {
        Bases bases = f.getStrand().getBases();
        sequenceloc =
            segments.elementAt(i).getStart().getPosition() + (translatedBasePosion - nbases);

        if (!f.isForwardFeature()) sequenceloc = bases.getComplementPosition(sequenceloc);
      }
      nbases += seglen;
    }

    String pos = "";
    if (f.isForwardFeature()) pos = sequenceloc + ".." + (sequenceloc + 2);
    else pos = "complement(" + (sequenceloc - 2) + ".." + sequenceloc + ")";

    qualifiers.add(new Qualifier("transl_except", "(pos:" + pos + ",aa:Sec)"));
  }
  /**
   * Transfer selected qualifiers to the list of features defined by the selected names.
   *
   * @param qualifierCheckBoxes - list of qualifier check boxes
   * @param geneNameTextArea - text with a list of feature names to transfer to
   * @param feature - feature to copy from
   * @param entryGroup
   * @param sameKey
   * @param overwrite
   */
  protected static int transferAnnotation(
      final Hashtable<JCheckBox, Vector<JCheckBox>> qualifierCheckBoxes,
      final Vector<JCheckBox> geneNameCheckBoxes,
      final Feature orginatingFeature,
      final EntryGroup entryGroup,
      final boolean sameKey,
      final boolean overwrite,
      final boolean setEvidenceAndWithFrom,
      final StringBuffer buff,
      final StringBuffer genesUpdated) {
    // transfer selected annotation to genes
    final QualifierVector qualifiers = orginatingFeature.getQualifiers();
    final QualifierVector qualifiersToTransfer = new QualifierVector();

    Enumeration<JCheckBox> enumQualifiers = qualifierCheckBoxes.keys();
    while (enumQualifiers.hasMoreElements()) {
      JCheckBox cb = enumQualifiers.nextElement();
      if (cb.isSelected()) {
        Vector<JCheckBox> qualifierValuesCheckBox = qualifierCheckBoxes.get(cb);
        final StringVector values = qualifiers.getQualifierByName(cb.getText()).getValues();
        StringVector valuesToTransfer = new StringVector(values);

        logger4j.debug("TRANSFER " + cb.getText());
        for (int i = 0; i < qualifierValuesCheckBox.size(); i++) {
          JCheckBox valuesCb = qualifierValuesCheckBox.get(i);
          if (!valuesCb.isSelected()) {
            valuesToTransfer.remove(valuesCb.getText());
            logger4j.debug("NOT TRANSFERING " + valuesCb.getText());
          }
        }

        if (valuesToTransfer.size() < 1) continue;

        valuesToTransfer =
            new StringVector(
                getTransferValues(
                    setEvidenceAndWithFrom, orginatingFeature, cb.getText(), valuesToTransfer));

        qualifiersToTransfer.addElement(new Qualifier(cb.getText(), valuesToTransfer));
      }
    }

    int count = 0;
    for (int i = 0; i < geneNameCheckBoxes.size(); i++) {
      if (geneNameCheckBoxes.get(i).isSelected()) count++;
    }

    if (count < 1) {
      JOptionPane.showMessageDialog(
          null, "No genes selected.", "Warning", JOptionPane.WARNING_MESSAGE);
      return -1;
    }

    String geneNames[] = new String[count];
    count = 0;
    for (int i = 0; i < geneNameCheckBoxes.size(); i++) {
      JCheckBox cb = geneNameCheckBoxes.get(i);
      if (cb.isSelected()) {
        geneNames[count] = cb.getText();
        logger4j.debug("TRANSFER ANNOTATION TO " + geneNames[count]);
        count++;
      }
    }

    final String key = orginatingFeature.getKey().getKeyString();
    final FeatureVector features = entryGroup.getAllFeatures();

    // transfer selected annotation
    entryGroup.getActionController().startAction();
    geneNames =
        transfer(
            features,
            qualifiersToTransfer,
            key,
            sameKey,
            overwrite,
            GeneUtils.isDatabaseEntry(entryGroup),
            geneNames,
            genesUpdated);
    entryGroup.getActionController().endAction();

    //
    // Commit changes to genes not in Artemis but in the database
    //
    Vector<String> genesNotFound = null;
    if (geneNames != null
        && orginatingFeature.getEntry().getEMBLEntry() instanceof DatabaseDocumentEntry) {
      DatabaseDocumentEntry db_entry =
          (DatabaseDocumentEntry) orginatingFeature.getEntry().getEMBLEntry();
      DatabaseDocument doc = (DatabaseDocument) db_entry.getDocument();

      for (int i = 0; i < geneNames.length; i++) {
        DatabaseDocumentEntry newDbEntry = GeneEdit.makeGeneEntry(null, geneNames[i], doc, null);

        if (newDbEntry == null) {
          if (genesNotFound == null) genesNotFound = new Vector<String>();
          genesNotFound.add(geneNames[i]);
          continue;
        }

        char[] c = new char[1];
        PartialSequence ps = new PartialSequence(c, 100, 0, null, null);
        newDbEntry.setPartialSequence(ps);
        Entry entry = null;
        try {
          entry = new Entry(newDbEntry);
        } catch (Exception e) {
          e.printStackTrace();
        }

        SimpleEntryGroup entry_group = new SimpleEntryGroup();
        entry_group.addElement(entry);

        ChadoTransactionManager ctm = new ChadoTransactionManager();
        entry_group.addFeatureChangeListener(ctm);
        entry_group.addEntryChangeListener(ctm);
        ctm.setEntryGroup(entry_group);

        transfer(
            entry.getAllFeatures(),
            qualifiersToTransfer,
            key,
            sameKey,
            overwrite,
            true,
            geneNames,
            genesUpdated);

        for (int j = 0; j < ctm.getTransactionCount(); j++)
          buff.append(ctm.getTransactionAt(j).getLogComment() + "\n");
        ChadoTransactionManager.commit((DatabaseDocument) newDbEntry.getDocument(), false, ctm);

        entry_group.removeFeatureChangeListener(ctm);
        entry_group.removeEntryChangeListener(ctm);
        // if(newDbEntry != null)
        // GeneEdit.showGeneEditor(null, geneNames[i], newDbEntry);
      }
    }

    if (genesNotFound != null)
      JOptionPane.showMessageDialog(
          null,
          "Gene(s) Not Found:\n" + genesNotFound.toString(),
          "Gene(s) Not Found",
          JOptionPane.WARNING_MESSAGE);
    return 0;
  }
  /**
   * Construct the panel for setting up the gene list and the list of qualifiers to transfer.
   *
   * @param feature
   * @param pane
   * @param qualifierCheckBoxes
   * @param geneNameCheckBoxes
   * @param geneNames
   */
  private void addMainPanel(
      final Feature feature,
      final JPanel pane,
      final Vector<QualifierPanel> qualifierPanels,
      final Vector<JCheckBox> geneNameCheckBoxes,
      final List<String> geneNames) {
    GridBagConstraints c = new GridBagConstraints();
    int nrows = 0;

    c.anchor = GridBagConstraints.NORTHWEST;
    c.gridx = 2;
    c.gridy = 0;
    c.ipadx = 50;

    JLabel geneLabel = new JLabel("Qualifier(s)");
    geneLabel.setFont(geneLabel.getFont().deriveFont(Font.BOLD));
    pane.add(geneLabel, c);

    c.gridy = 0;
    c.gridx = 0;
    JLabel label = new JLabel("Gene List");
    label.setFont(label.getFont().deriveFont(Font.BOLD));
    pane.add(label, c);

    nrows += 3;
    c.gridx = 2;
    c.gridy = nrows;
    c.anchor = GridBagConstraints.WEST;

    addQualifierPanel(feature, qualifierPanels, c, nrows, pane);
    nrows += 2;

    if (feature.getEmblFeature() instanceof GFFStreamFeature) {
      GFFStreamFeature gffFeature = ((GFFStreamFeature) feature.getEmblFeature());
      if (gffFeature.getChadoGene() != null) {
        String id = GeneUtils.getUniqueName(gffFeature);
        ChadoCanonicalGene chadoGene = gffFeature.getChadoGene();
        Feature gene = (Feature) chadoGene.getGene().getUserData();

        if (!id.equals(GeneUtils.getUniqueName(((GFFStreamFeature) chadoGene.getGene()))))
          addQualifierPanel(gene, qualifierPanels, c, nrows, pane);

        nrows += 2;

        String transcriptName =
            chadoGene.getTranscriptFromName(GeneUtils.getUniqueName(gffFeature));

        if (transcriptName != null) {
          GFFStreamFeature transcript =
              (GFFStreamFeature) chadoGene.getFeatureFromId(transcriptName);
          addQualifierPanel((Feature) transcript.getUserData(), qualifierPanels, c, nrows, pane);
          nrows += 2;

          Set<uk.ac.sanger.artemis.io.Feature> children = chadoGene.getChildren(transcript);
          Iterator<uk.ac.sanger.artemis.io.Feature> it = children.iterator();

          while (it.hasNext()) {
            GFFStreamFeature kid = (GFFStreamFeature) it.next();
            if (id.equals(GeneUtils.getUniqueName(((GFFStreamFeature) kid)))) continue;
            addQualifierPanel((Feature) kid.getUserData(), qualifierPanels, c, nrows, pane);
            nrows += 2;
          }
        }
      }
    }

    c.gridx = 0;
    c.gridy = 3;
    c.gridheight = nrows;
    c.fill = GridBagConstraints.BOTH;

    final Box geneNameBox = Box.createVerticalBox();
    pane.add(geneNameBox, c);

    if (geneNames != null) {
      for (int i = 0; i < geneNames.size(); i++) {
        JCheckBox cb = new JCheckBox((String) geneNames.get(i), true);
        geneNameBox.add(cb);
        geneNameCheckBoxes.add(cb);
      }
    }

    c.gridy = 1;
    c.gridheight = 1;
    c.fill = GridBagConstraints.NONE;
    c.gridx = 2;
    final JButton toggle = new JButton("Toggle");
    toggle.addActionListener(
        new ActionListener() {
          public void actionPerformed(ActionEvent e) {
            for (int i = 0; i < qualifierPanels.size(); i++) {
              QualifierPanel qP = qualifierPanels.get(i);
              Enumeration<JCheckBox> enumQualifiers = qP.getQualifierCheckBoxes().keys();
              while (enumQualifiers.hasMoreElements()) {
                JCheckBox cb = enumQualifiers.nextElement();
                cb.setSelected(!cb.isSelected());
              }
            }
          }
        });
    pane.add(toggle, c);

    Box xBox = Box.createHorizontalBox();
    final JButton toggleGeneList = new JButton("Toggle");
    toggleGeneList.addActionListener(
        new ActionListener() {
          public void actionPerformed(ActionEvent e) {
            for (int i = 0; i < geneNameCheckBoxes.size(); i++) {
              JCheckBox cb = geneNameCheckBoxes.get(i);
              cb.setSelected(!cb.isSelected());
            }
            geneNameBox.repaint();
          }
        });
    xBox.add(toggleGeneList);

    final JButton addGenes = new JButton("Add");
    addGenes.addActionListener(
        new ActionListener() {
          public void actionPerformed(ActionEvent e) {
            JTextArea geneNameTextArea = new JTextArea();
            geneNameTextArea.setEditable(true);
            JScrollPane jsp = new JScrollPane(geneNameTextArea);

            int res =
                JOptionPane.showConfirmDialog(
                    TransferAnnotationTool.this,
                    jsp,
                    "Paste Feature Names to Add",
                    JOptionPane.OK_CANCEL_OPTION);
            if (res == JOptionPane.CANCEL_OPTION) return;

            String geneNames[] = geneNameTextArea.getText().split("\\s");
            for (int i = 0; i < geneNames.length; i++) {
              if (geneNames[i] == null || geneNames[i].equals("")) continue;
              JCheckBox cb = new JCheckBox(geneNames[i], true);
              geneNameBox.add(cb);
              geneNameCheckBoxes.add(cb);
            }
            pane.revalidate();
          }
        });
    xBox.add(addGenes);
    c.gridx = 0;
    pane.add(xBox, c);

    final List<String> clusterList = (matchPanel == null ? null : matchPanel.getGeneNameList(true));
    if (clusterList != null && !geneNames.contains(clusterList.get(0))) {
      final JButton importCluster = new JButton("Import Cluster Names");
      importCluster.addActionListener(
          new ActionListener() {
            public void actionPerformed(ActionEvent e) {
              for (String n : clusterList) {
                if (n == null || n.equals("")) continue;
                JCheckBox cb = new JCheckBox(n, true);
                geneNameBox.add(cb);
                geneNameCheckBoxes.add(cb);
              }
              importCluster.setEnabled(false);
              pane.revalidate();
            }
          });
      c.gridy = 2;
      pane.add(importCluster, c);
    }
  }