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 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 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 123 124 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 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 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 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 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 299 300 301 302 303 304 305 306
|
/*
* BioJava development code
*
* This code may be freely distributed and modified under the
* terms of the GNU Lesser General Public Licence. This should
* be distributed with the code. If you do not have a copy,
* see:
*
* http://www.gnu.org/copyleft/lesser.html
*
* Copyright for this code is held jointly by the individual
* authors. These should be listed in @author doc comments.
*
* For more information on the BioJava project and its aims,
* or to join the biojava-l mailing list, visit the home page
* at:
*
* http://www.biojava.org/
*
*/
package org.biojava.bio.seq.project;
import junit.framework.TestCase;
import org.biojava.bio.Annotation;
import org.biojava.bio.seq.DNATools;
import org.biojava.bio.seq.Feature;
import org.biojava.bio.seq.FeatureFilter;
import org.biojava.bio.seq.FilterUtils;
import org.biojava.bio.seq.Sequence;
import org.biojava.bio.seq.StrandedFeature;
import org.biojava.bio.seq.impl.SimpleSequence;
import org.biojava.bio.seq.projection.ProjectedFeatureHolder;
import org.biojava.bio.seq.projection.ReparentContext;
import org.biojava.bio.seq.projection.TranslateFlipContext;
import org.biojava.bio.symbol.RangeLocation;
import org.biojava.utils.ChangeListener;
import org.biojava.utils.ChangeType;
import org.biojava.utils.ChangeVetoException;
/**
* Tests for ProjectedFeatureHolder
*
* @author Thomas Down
* @author Matthew Pocock
* @since 1.3
*/
public class ProjectedFeatureHolderTest extends TestCase
{
public ProjectedFeatureHolderTest(String name) {
super(name);
}
public void testReparentContext()
throws Exception
{
Sequence seq = new SimpleSequence(
DNATools.createDNA("gattaca"),
"test",
"test",
Annotation.EMPTY_ANNOTATION
);
Sequence seq2 = new SimpleSequence(
DNATools.createDNA("-------"),
"test",
"test",
Annotation.EMPTY_ANNOTATION
);
Feature.Template template = new Feature.Template();
template.type = "test";
template.source = "foo";
template.location = new RangeLocation(2, 4);
template.annotation = Annotation.EMPTY_ANNOTATION;
Feature origFeat = seq.createFeature(template);
ProjectedFeatureHolder pfh = new ProjectedFeatureHolder(
new ReparentContext(seq2, seq));
assertEquals("Same size", pfh.countFeatures(), seq.countFeatures());
Feature projFeat = (Feature) pfh.features().next();
assertEquals("getSource()", origFeat.getSource(), projFeat.getSource());
assertEquals("getType()", origFeat.getType(), projFeat.getType());
assertEquals("getLocation()", origFeat.getLocation(), projFeat.getLocation());
assertEquals("origFeat.getParent()", origFeat.getParent(), seq);
assertEquals("projFeat.getParent()", projFeat.getParent(), seq2);
}
public void testReparentContext_create()
throws Exception
{
Sequence seq = new SimpleSequence(
DNATools.createDNA("gattaca"),
"test",
"test",
Annotation.EMPTY_ANNOTATION
);
Sequence seq2 = new SimpleSequence(
DNATools.createDNA("-------"),
"test",
"test",
Annotation.EMPTY_ANNOTATION
);
Feature.Template template = new Feature.Template();
template.type = "test";
template.source = "foo";
template.location = new RangeLocation(2, 4);
template.annotation = Annotation.EMPTY_ANNOTATION;
ProjectedFeatureHolder pfh = new ProjectedFeatureHolder(
new ReparentContext(seq2, seq));
Feature projFeat = pfh.createFeature(template);
Feature origFeat = (Feature) seq.features().next();
assertEquals("Same size", pfh.countFeatures(), seq.countFeatures());
assertEquals("getSource()", origFeat.getSource(), projFeat.getSource());
assertEquals("getType()", origFeat.getType(), projFeat.getType());
assertEquals("getLocation()", origFeat.getLocation(), projFeat.getLocation());
assertEquals("origFeat.getParent()", origFeat.getParent(), seq);
assertEquals("projFeat.getParent()", projFeat.getParent(), seq2);
}
public void testTranslateFlipContext_translateOnly()
throws Exception {
Sequence seq = new SimpleSequence(
DNATools.createDNA("gattaca"),
"test",
"test",
Annotation.EMPTY_ANNOTATION
);
Sequence seq2 = new SimpleSequence(
DNATools.createDNA("-------"),
"test",
"test",
Annotation.EMPTY_ANNOTATION
);
StrandedFeature.Template template = new StrandedFeature.Template();
template.type = "test";
template.source = "foo";
template.location = new RangeLocation(2, 4);
template.annotation = Annotation.EMPTY_ANNOTATION;
template.strand = StrandedFeature.NEGATIVE;
StrandedFeature origFeat = (StrandedFeature) seq.createFeature(template);
ProjectedFeatureHolder pfh = new ProjectedFeatureHolder(
new TranslateFlipContext(seq2, seq, 3, false));
assertEquals("Same size", pfh.countFeatures(), seq.countFeatures());
StrandedFeature projFeat = (StrandedFeature) pfh.features().next();
assertEquals("getSource()", origFeat.getSource(), projFeat.getSource());
assertEquals("getType()", origFeat.getType(), projFeat.getType());
assertEquals("getLocation()", origFeat.getLocation().translate(3), projFeat.getLocation());
assertEquals("getStrand()", origFeat.getStrand(), projFeat.getStrand());
assertEquals("projFeat.getParent()", projFeat.getParent(), seq2);
assertEquals("pfh.filter(5,7).size() == 1",
1,
pfh.filter(FilterUtils.containedByLocation(projFeat.getLocation())).countFeatures());
assertTrue("pfh.filter(5,7).contains(projFeat)",
pfh.filter(FilterUtils.containedByLocation(projFeat.getLocation())).containsFeature(projFeat));
assertEquals("projFeat.filter(POSITIVE).size() == 1",
1,
pfh.filter(FilterUtils.byStrand(origFeat.getStrand())).countFeatures());
}
public void testTranslateFlipContext_translateAndFlip()
throws Exception {
Sequence seq = new SimpleSequence(
DNATools.createDNA("gattaca"),
"test",
"test",
Annotation.EMPTY_ANNOTATION
);
Sequence seq2 = new SimpleSequence(
DNATools.createDNA("-------"),
"test",
"test",
Annotation.EMPTY_ANNOTATION
);
StrandedFeature.Template template = new StrandedFeature.Template();
template.type = "test";
template.source = "foo";
template.location = new RangeLocation(2, 3);
template.annotation = Annotation.EMPTY_ANNOTATION;
template.strand = StrandedFeature.NEGATIVE;
StrandedFeature origFeat = (StrandedFeature) seq.createFeature(template);
ProjectedFeatureHolder pfh = new ProjectedFeatureHolder(
new TranslateFlipContext(seq2, seq, seq.length() + 1, true));
assertEquals("Same size", pfh.countFeatures(), seq.countFeatures());
StrandedFeature projFeat = (StrandedFeature) pfh.features().next();
assertEquals("getSource()", origFeat.getSource(), projFeat.getSource());
assertEquals("getType()", origFeat.getType(), projFeat.getType());
assertEquals("getLocation()",
new RangeLocation(5,6),
projFeat.getLocation());
assertEquals("getStrand()", StrandedFeature.POSITIVE, projFeat.getStrand());
assertEquals("projFeat.getParent()", projFeat.getParent(), seq2);
assertEquals("pfh.filter(5,6).size() == 1",
1,
pfh.filter(FilterUtils.containedByLocation(projFeat.getLocation())).countFeatures());
assertTrue("pfh.filter(5,6).contains(projFeat)",
pfh.filter(FilterUtils.containedByLocation(projFeat.getLocation())).containsFeature(projFeat));
assertEquals("projFeat.filter(NEGATIVE).size() == 0",
0,
pfh.filter(FilterUtils.byStrand(StrandedFeature.NEGATIVE)).countFeatures());
assertEquals("projFeat.filter(POSITIVE).size() == 1",
1,
pfh.filter(FilterUtils.byStrand(StrandedFeature.POSITIVE)).countFeatures());
}
public void testTranslateFlipContext_translateAndFlip_create()
throws Exception {
Sequence seq = new SimpleSequence(
DNATools.createDNA("gattaca"),
"test",
"test",
Annotation.EMPTY_ANNOTATION
);
Sequence seq2 = new SimpleSequence(
DNATools.createDNA("-------"),
"test",
"test",
Annotation.EMPTY_ANNOTATION
);
StrandedFeature.Template template = new StrandedFeature.Template();
template.type = "test";
template.source = "foo";
template.location = new RangeLocation(5, 6);
template.annotation = Annotation.EMPTY_ANNOTATION;
template.strand = StrandedFeature.POSITIVE;
ProjectedFeatureHolder pfh = new ProjectedFeatureHolder(
new TranslateFlipContext(seq2, seq, seq.length() + 1, true));
StrandedFeature projFeat = (StrandedFeature) pfh.createFeature(template);
Feature origFeat = (StrandedFeature) seq.features().next();
assertEquals("Same size", pfh.countFeatures(), seq.countFeatures());
assertEquals("getSource()", origFeat.getSource(), projFeat.getSource());
assertEquals("getType()", origFeat.getType(), projFeat.getType());
assertEquals("getLocation()",
new RangeLocation(5,6),
projFeat.getLocation());
assertEquals("getStrand()", StrandedFeature.POSITIVE, projFeat.getStrand());
assertEquals("projFeat.getParent()", projFeat.getParent(), seq2);
assertEquals("pfh.filter(5,6).size() == 1",
1,
pfh.filter(FilterUtils.containedByLocation(projFeat.getLocation())).countFeatures());
assertTrue("pfh.filter(5,6).contains(projFeat)",
pfh.filter(FilterUtils.containedByLocation(projFeat.getLocation())).containsFeature(projFeat));
assertEquals("projFeat.filter(NEGATIVE).size() == 0",
0,
pfh.filter(FilterUtils.byStrand(StrandedFeature.NEGATIVE)).countFeatures());
assertEquals("projFeat.filter(POSITIVE).size() == 1",
1,
pfh.filter(FilterUtils.byStrand(StrandedFeature.POSITIVE)).countFeatures());
}
public void testFeatureChangeEvent()
throws Exception
{
Sequence seq = new SimpleSequence(
DNATools.createDNA("gattaca"),
"test",
"test",
Annotation.EMPTY_ANNOTATION
);
Feature.Template template = new Feature.Template();
template.type = "test";
template.source = "foo";
template.location = new RangeLocation(2, 4);
template.annotation = Annotation.EMPTY_ANNOTATION;
Feature seqFeature = seq.createFeature(template);
ProjectedFeatureHolder pfh = new ProjectedFeatureHolder(
new TranslateFlipContext(seq, seq, 7, true));
Feature pfhFeature = (Feature) pfh.filter(new FeatureFilter.ByType("test")).features().next();
pfhFeature.addChangeListener(ChangeListener.ALWAYS_VETO, ChangeType.UNKNOWN);
boolean vetoed = false;
try {
seqFeature.setLocation(new RangeLocation(1, 3));
} catch (ChangeVetoException cve) {
vetoed = true;
}
assertTrue(vetoed);
pfhFeature.removeChangeListener(ChangeListener.ALWAYS_VETO, ChangeType.UNKNOWN);
seqFeature.setLocation(new RangeLocation(1, 3));
}
}
|