Newer
Older
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
/* TransferAnnotationTool.java
*
* created: 2008
*
* This file is part of Artemis
*
* Copyright (C) 2008 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;
import java.awt.Font;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.Vector;
import javax.swing.JButton;
import javax.swing.JCheckBox;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextArea;
import uk.ac.sanger.artemis.EntryGroup;
import uk.ac.sanger.artemis.Feature;
import uk.ac.sanger.artemis.FeaturePredicate;
import uk.ac.sanger.artemis.FeatureVector;
import uk.ac.sanger.artemis.SimpleEntryGroup;
import uk.ac.sanger.artemis.chado.ChadoTransactionManager;
import uk.ac.sanger.artemis.components.genebuilder.GeneEdit;
import uk.ac.sanger.artemis.components.genebuilder.GeneUtils;
import uk.ac.sanger.artemis.io.Qualifier;
import uk.ac.sanger.artemis.io.QualifierVector;
import uk.ac.sanger.artemis.sequence.NoSequenceException;
import uk.ac.sanger.artemis.util.DatabaseDocument;
import uk.ac.sanger.artemis.util.InputStreamProgressEvent;
import uk.ac.sanger.artemis.util.InputStreamProgressListener;
import uk.ac.sanger.artemis.util.OutOfRangeException;
class TransferAnnotationTool extends JFrame
{
private static final long serialVersionUID = 1L;
private static String[] NON_TRANSFERABLE_QUALIFIERS =
{
"ID",
"feature_id",
"Derives_from",
"feature_relationship_rank",
"Parent",
"isObsolete",
"timelastmodified",
"orthologous_to",
"paralogous_to",
"fasta_file",
"blastp_file",
"blastn_file",
"systematic_id",
"previous_systematic_id"
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
public TransferAnnotationTool(final Feature feature,
final EntryGroup entryGroup)
{
super("Transfer Annotation Tool :: "
+ feature.getIDString());
JPanel pane = (JPanel) getContentPane();
pane.setLayout(new GridBagLayout());
GridBagConstraints c = new GridBagConstraints();
int nrows = 0;
c.gridx = 0;
c.gridy = 0;
JLabel geneLabel = new JLabel(feature.getIDString() + " Qualifiers");
geneLabel.setFont(geneLabel.getFont().deriveFont(Font.BOLD));
pane.add(geneLabel, c);
c.gridx = 1;
JLabel label = new JLabel("Gene List");
label.setFont(label.getFont().deriveFont(Font.BOLD));
pane.add(label, c);
c.gridx = 0;
c.gridy = ++nrows;
c.anchor = GridBagConstraints.WEST;
final Vector qualifierCheckBoxes = new Vector();
final QualifierVector qualifiers = feature.getQualifiers();
for(int i = 0; i < qualifiers.size(); i++)
{
Qualifier qualifier = ((Qualifier) qualifiers.get(i));
if(isNonTransferable(qualifier.getName()))
continue;
JCheckBox checkBox = new JCheckBox(qualifier.getName(), true);
pane.add(checkBox, c);
qualifierCheckBoxes.add(checkBox);
c.gridy = ++nrows;
}
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
c.gridx = 1;
c.gridy = 1;
c.gridheight = nrows;
c.fill = GridBagConstraints.BOTH;
final JTextArea geneNameTextArea = new JTextArea("gene1");
geneNameTextArea.setEditable(true);
pane.add(geneNameTextArea, c);
c.gridy = ++nrows;
c.gridheight = 1;
c.fill = GridBagConstraints.NONE;
c.gridx = 0;
final JButton toggle = new JButton("Toggle Selection");
toggle.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent e)
{
for(int i = 0; i < qualifierCheckBoxes.size(); i++)
{
JCheckBox cb = (JCheckBox) qualifierCheckBoxes.get(i);
cb.setSelected(!cb.isSelected());
}
}
});
pane.add(toggle, c);
final JCheckBox sameKeyCheckBox = new JCheckBox("Add to feature of same key", true);
final JButton transfer = new JButton(">>TRANSFER");
transfer.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent e)
{
transferAnnotation(qualifierCheckBoxes, geneNameTextArea, feature,
entryGroup, sameKeyCheckBox.isSelected());
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
final JButton close = new JButton("CLOSE");
close.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent e)
{
dispose();
}
});
c.gridy = ++nrows;
pane.add(close, c);
pack();
setVisible(true);
}
/**
* Returns true if this qualifier is non-transferable
* @param qualifierName
* @return
*/
private boolean isNonTransferable(String qualifierName)
{
for(int i=0; i<NON_TRANSFERABLE_QUALIFIERS.length; i++)
{
if(NON_TRANSFERABLE_QUALIFIERS[i].equals(qualifierName))
return true;
}
return false;
}
/**
* Transfer selected qualifiers to the list of features defined
* by the names in the JTextArea
* @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
*/
private void transferAnnotation(final Vector qualifierCheckBoxes,
final JTextArea geneNameTextArea,
final Feature orginatingFeature,
final EntryGroup entryGroup,
final boolean sameKey)
final QualifierVector qualifiersToTransfer = new QualifierVector();
for(int i = 0; i < qualifierCheckBoxes.size(); i++)
{
JCheckBox cb = (JCheckBox) qualifierCheckBoxes.get(i);
if(cb.isSelected())
{
qualifiersToTransfer.addElement(
qualifiers.getQualifierByName(cb.getText()).copy());
}
}
String geneNames[] = geneNameTextArea.getText().split("\\s");
final String key = orginatingFeature.getKey().getKeyString();
final FeatureVector features = entryGroup.getAllFeatures();
// transfer selected annotation
entryGroup.getActionController().startAction();
geneNames = transfer(features, qualifiersToTransfer, key,
sameKey, GeneUtils.isDatabaseEntry(entryGroup), geneNames);
entryGroup.getActionController().endAction();
//
// Commit changes to genes not in Artemis but in the database
//
/* DatabaseDocumentEntry db_entry =
(DatabaseDocumentEntry) orginatingFeature.getEntry().getEMBLEntry();
DatabaseDocument doc = (DatabaseDocument) db_entry.getDocument();
for(int i=0; i<geneNames.length; i++)
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
System.out.println(geneNames[i]);
DatabaseDocumentEntry newDbEntry = GeneEdit.makeGeneEntry(null, geneNames[i], doc, null);
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);
geneNames = transfer(entry.getAllFeatures(), qualifiersToTransfer, key,
sameKey, true, geneNames);
ChadoTransactionManager.commit(
(DatabaseDocument)newDbEntry.getDocument(), false, ctm);
//if(newDbEntry != null)
// GeneEdit.showGeneEditor(null, geneNames[i], newDbEntry);
}*/
}
/**
*
* @param features
* @param qualifiersToTransfer
* @param key
* @param sameKey
* @param isDatabaseEntry
* @param geneNames
* @return
*/
private String[] transfer(final FeatureVector features,
final QualifierVector qualifiersToTransfer,
final String key,
final boolean sameKey,
final boolean isDatabaseEntry,
String[] geneNames)
{
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))
Qualifier qualifier = (Qualifier) qualifiersToTransfer.elementAt(j);
try
thisFeature.addQualifierValues(qualifier);
} catch (Exception e1)
{
e1.printStackTrace();
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
}
return geneNames;
}
/**
* Remove a string from an array of strings. If the string appears multiple
* times in the array this method will delete all occurrences.
* @param strArr
* @param str
* @return
*/
private String[] removeArrayElement(final String strArr[], final String str)
{
String[] newarray = new String[strArr.length - 1];
int count = 0;
for(int i=0;i<strArr.length; i++)
{
if(strArr[i].equals(str))
continue;
// not found str return original array
if(count>=newarray.length)
return strArr;
newarray[count] = strArr[i];
count++;
}
String[] tmparray = new String[count];
System.arraycopy(newarray, 0, tmparray, 0, count);
newarray = tmparray;
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
return newarray;
}
/**
* Test if the feature is nominated to have annotation transferred
* to it.
*/
class TransferFeaturePredicate implements FeaturePredicate
{
private String geneName;
private String key;
private boolean sameKey;
private boolean isDatabaseEntry;
private String[] geneNames;
public TransferFeaturePredicate(final String key,
final boolean sameKey,
final boolean isDatabaseEntry,
final String[] geneNames)
{
this.key = key;
this.sameKey = sameKey;
this.isDatabaseEntry = isDatabaseEntry;
this.geneNames = geneNames;
}
public boolean testPredicate(Feature targetFeature)
{
String targetKey = targetFeature.getKey().getKeyString();
if(!sameKey || !targetKey.equals(key))
return false;
String chadoGeneName = null;
if(isDatabaseEntry)
{
GFFStreamFeature gffFeature = ((GFFStreamFeature)targetFeature.getEmblFeature());
if(gffFeature.getChadoGene() != null)
chadoGeneName = gffFeature.getChadoGene().getGeneUniqueName();
}
String thisFeatureSystematicName = targetFeature.getSystematicName();
for(int i=0;i<geneNames.length;i++)
{
if( geneNames[i].equals(thisFeatureSystematicName) ||
(chadoGeneName != null && geneNames[i].equals(chadoGeneName)) )
{
geneName = geneNames[i];
return true;
}
}
return false;
}
public String getGeneName()
{
return geneName;
}