1 /*
2  * This file is part of Artemis
3  *
4  * Copyright (C) 2014  Genome Research Limited
5  *
6  * This program is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU General Public License
8  * as published by the Free Software Foundation; either version 2
9  * of the License, or (at your option) any later version.
10  *
11  * This program is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14  * GNU General Public License for more details.
15  *
16  * You should have received a copy of the GNU General Public License
17  * along with this program; if not, write to the Free Software
18  * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
19  *
20  */
21 package uk.ac.sanger.artemis.io;
22 
23 import static org.junit.Assert.assertTrue;
24 import static org.junit.Assert.assertFalse;
25 import static org.junit.Assert.assertEquals;
26 import junit.framework.Assert;
27 
28 import org.junit.Test;
29 import org.junit.Before;
30 
31 import uk.ac.sanger.artemis.io.GFF3AttributeAggregator;
32 import uk.ac.sanger.artemis.io.GFF3AttributeBuilder;
33 import uk.ac.sanger.artemis.io.GFF3Encoder;
34 import uk.ac.sanger.artemis.util.StringVector;
35 
36 public class GFF3AttributeBuilderTest {
37   private GFF3Encoder                    enc;
38   private String[]                       invals  = { "foo", "bar" };
39   private String[]                       invalsC = { "foo,bar", "baz,quux" };
40   private static GFF3AttributeAggregator testProc;
41 
42   @Before
setUp()43   public void setUp() {
44     enc = new GFF3Encoder();
45     testProc = new GFF3AttributeAggregator() {
46       @Override
47       public String process(StringVector values) {
48         StringBuilder buffer = new StringBuilder();
49         if (values != null && values.size() > 0) {
50           for (int value_index = 0; value_index < values.size(); ++value_index) {
51             buffer.append(">>"
52                 + GFF3Encoder.encode(values.elementAt(value_index)) + "<<");
53             if (value_index < (values.size()) - 1)
54               buffer.append("|");
55           }
56         }
57         return buffer.toString();
58       }
59     };
60   }
61 
62   @Test
63   /**
64    * Tests adding one attribute.
65    */
testAdd1()66   public void testAdd1() {
67     GFF3AttributeBuilder ab = new GFF3AttributeBuilder();
68     StringVector in = new StringVector(invals);
69 
70     ab.add("attr1", in);
71     assertEquals(ab.toString(), "attr1=foo,bar");
72   }
73 
74   @Test
75   /**
76    * Tests adding two different attributes.
77    */
testAdd2()78   public void testAdd2() {
79     GFF3AttributeBuilder ab = new GFF3AttributeBuilder();
80     StringVector in = new StringVector(invals);
81 
82     ab.add("attr1", in);
83     ab.add("attr2", in);
84     assertEquals(ab.toString(), "attr1=foo,bar;attr2=foo,bar");
85   }
86 
87   @Test
88   /**
89    * Tests adding attributes with custom aggregators.
90    */
testAddWithAggs()91   public void testAddWithAggs() {
92     GFF3AttributeBuilder ab = new GFF3AttributeBuilder();
93     StringVector in = new StringVector(invals);
94     ab.setAggregator("attr1", testProc);
95     ab.add("attr1", in);
96     assertEquals(ab.toString(), "attr1=>>foo<<|>>bar<<");
97     ab.add("attr1", in);
98     assertEquals(ab.toString(), "attr1=>>foo<<|>>bar<< >>foo<<|>>bar<<");
99   }
100 
101   @Test
102   /**
103    * Tests adding attributes (encoded values) with custom aggregators.
104    */
testAddWithAggsCommas()105   public void testAddWithAggsCommas() {
106     GFF3AttributeBuilder ab = new GFF3AttributeBuilder();
107     StringVector in = new StringVector(invalsC);
108     ab.add("attr1", in);
109     assertEquals(ab.toString(), "attr1=foo%2Cbar,baz%2Cquux");
110     ab.add("attr1", in);
111     assertEquals(ab.toString(),
112         "attr1=foo%2Cbar,baz%2Cquux foo%2Cbar,baz%2Cquux");
113     ab = new GFF3AttributeBuilder();
114     ab.setAggregator("attr1", testProc);
115     ab.add("attr1", in);
116     assertEquals(ab.toString(), "attr1=>>foo%2Cbar<<|>>baz%2Cquux<<");
117     ab.add("attr1", in);
118     assertEquals(ab.toString(),
119         "attr1=>>foo%2Cbar<<|>>baz%2Cquux<< >>foo%2Cbar<<|>>baz%2Cquux<<");
120   }
121 
122   @Test
123   /**
124    * Tests the ignoring of attribute fields in the output.
125    */
testIgnore()126   public void testIgnore() {
127     GFF3AttributeBuilder ab = new GFF3AttributeBuilder();
128     StringVector in = new StringVector(invals);
129 
130     ab.ignore("attr1");
131     ab.add("attr1", in);
132     ab.add("attr2", in);
133     assertEquals(ab.toString(), "attr2=foo,bar");
134     ab.unignore("attr1");
135     assertEquals(ab.toString(), "attr1=foo,bar;attr2=foo,bar");
136   }
137 
138   @Test
139   /**
140    * Tests the handling of duplicate attributes.
141    */
testAddMultiAttr()142   public void testAddMultiAttr() {
143     GFF3AttributeBuilder ab = new GFF3AttributeBuilder();
144     StringVector in = new StringVector(invals);
145 
146     ab.add("attr1", in);
147     ab.add("attr1", in);
148     assertEquals(ab.toString(), "attr1=foo,bar foo,bar");
149   }
150 
151   @Test
152   /**
153    * Tests the handling of duplicate attributes, with delimiter.
154    */
testAddMultiAttrWithGlue()155   public void testAddMultiAttrWithGlue() {
156     GFF3AttributeBuilder ab = new GFF3AttributeBuilder();
157     StringVector in = new StringVector(invals);
158 
159     ab.setGlue("attr1", "X");
160     ab.add("attr1", in);
161     ab.add("attr1", in);
162     ab.add("attr1", in);
163     assertEquals(ab.toString(), "attr1=foo,barXfoo,barXfoo,bar");
164   }
165 
166   @Test
167   /**
168    * Tests cloning of attributes to separate keys with default aggregator.
169    */
testClone()170   public void testClone() {
171     GFF3AttributeBuilder ab = new GFF3AttributeBuilder();
172     StringVector in = new StringVector(invals);
173 
174     ab.setClone("attr1", "brand_new");
175     ab.add("attr1", in);
176     assertEquals(ab.toString(), "attr1=foo,bar;brand_new=foo,bar");
177     ab.add("brand_new", in);
178     assertEquals(ab.toString(), "attr1=foo,bar;brand_new=foo,bar foo,bar");
179   }
180 
181   @Test
182   /**
183    * Tests cloning of attributes to separate keys with different aggregators.
184    */
testCloneWithAggs()185   public void testCloneWithAggs() {
186     GFF3AttributeBuilder ab = new GFF3AttributeBuilder();
187     StringVector in = new StringVector(invals);
188     ab.setClone("attr1", "brand_new");
189     ab.setAggregator("brand_new", testProc);
190     ab.add("attr1", in);
191     assertEquals(ab.toString(), "attr1=foo,bar;brand_new=>>foo<<|>>bar<<");
192     ab.add("attr1", in);
193     assertEquals(ab.toString(),
194         "attr1=foo,bar foo,bar;brand_new=>>foo<<|>>bar<< >>foo<<|>>bar<<");
195   }
196 
197   @Test
198   /**
199    * Tests mapping/cloning of attributes to separate keys with different aggregators.
200    */
testMappingAndCloneWithAggs1()201   public void testMappingAndCloneWithAggs1() {
202     GFF3AttributeBuilder ab = new GFF3AttributeBuilder();
203     StringVector in = new StringVector(invals);
204 
205     ab.setMapping("attr1", "aaaa");
206     ab.setClone("aaaa", "brand_new");
207     ab.setAggregator("brand_new", testProc);
208     ab.add("attr1", in);
209     assertEquals(ab.toString(), "aaaa=foo,bar;brand_new=>>foo<<|>>bar<<");
210     ab.add("attr1", in);
211     assertEquals(ab.toString(),
212         "aaaa=foo,bar foo,bar;brand_new=>>foo<<|>>bar<< >>foo<<|>>bar<<");
213   }
214 
215   @Test
216   /**
217    * Tests mapping/cloning of attributes to separate keys with different aggregators.
218    */
testMappingAndCloneWithAggs2()219   public void testMappingAndCloneWithAggs2() {
220     GFF3AttributeBuilder ab = new GFF3AttributeBuilder();
221     StringVector in = new StringVector(invals);
222 
223     ab.setMapping("attr1", "aaaa");
224     ab.setClone("attr1", "brand_new");
225     ab.setAggregator("brand_new", testProc);
226     ab.add("attr1", in);
227     assertEquals(ab.toString(), "aaaa=foo,bar;brand_new=>>foo<<|>>bar<<");
228     ab.add("attr1", in);
229     assertEquals(ab.toString(),
230         "aaaa=foo,bar foo,bar;brand_new=>>foo<<|>>bar<< >>foo<<|>>bar<<");
231   }
232 
233   @Test
234   /**
235    * Tests mapping one attribute to a new name.
236    */
testMapping()237   public void testMapping() {
238     GFF3AttributeBuilder ab = new GFF3AttributeBuilder();
239     StringVector in = new StringVector(invals);
240 
241     ab.setMapping("attr1", "brand_new");
242     ab.add("attr1", in);
243     ab.add("attr2", in);
244     assertEquals(ab.toString(), "attr2=foo,bar;brand_new=foo,bar");
245   }
246 
247   @Test
248   /**
249    * Tests mapping one attribute to a new name with custom target aggregator.
250    */
testMappingWithAggs()251   public void testMappingWithAggs() {
252     GFF3AttributeBuilder ab = new GFF3AttributeBuilder();
253     StringVector in = new StringVector(invals);
254     ab.setMapping("attr1", "brand_new");
255     ab.setAggregator("brand_new", testProc);
256     ab.add("attr1", in);
257     ab.add("attr2", in);
258     assertEquals(ab.toString(), "attr2=foo,bar;brand_new=>>foo<<|>>bar<<");
259   }
260 
261   @Test
262   /**
263    * Tests mapping one attribute to a new name with custom target aggregator.
264    */
testMappingCollision()265   public void testMappingCollision() {
266     GFF3AttributeBuilder ab = new GFF3AttributeBuilder();
267     StringVector in = new StringVector(invals);
268 
269     ab.setMapping("attr1", "attr2");
270     ab.add("attr1", in);
271     ab.add("attr2", in);
272     assertEquals(ab.toString(), "attr2=foo,bar foo,bar");
273   }
274 
275   @Test
276   /**
277    * Tests mapping one attribute to a new name with custom target aggregator.
278    */
testMappingCollisionWithAggs()279   public void testMappingCollisionWithAggs() {
280     GFF3AttributeBuilder ab = new GFF3AttributeBuilder();
281     StringVector in = new StringVector(invals);
282 
283     ab.setMapping("attr1", "attr2");
284     ab.setAggregator("attr2", testProc);
285     ab.add("attr1", in);
286     ab.add("attr2", in);
287     assertEquals(ab.toString(), "attr2=>>foo<<|>>bar<< >>foo<<|>>bar<<");
288     ab = new GFF3AttributeBuilder();
289     ab.setMapping("attr1", "attr2");
290     ab.setAggregator("attr1", testProc);
291     ab.add("attr1", in);
292     ab.add("attr2", in);
293     assertEquals(ab.toString(), "attr2=>>foo<<|>>bar<< foo,bar");
294   }
295 }
296