7837 |
28 Aug 20 |
nicklas |
1 |
/* |
7837 |
28 Aug 20 |
nicklas |
$Id: TestGenericOverview.java 6100 2012-09-04 08:43:21Z nicklas $ |
7837 |
28 Aug 20 |
nicklas |
3 |
|
7837 |
28 Aug 20 |
nicklas |
Copyright (C) 2009 Nicklas Nordborg |
7837 |
28 Aug 20 |
nicklas |
5 |
|
7837 |
28 Aug 20 |
nicklas |
This file is part of BASE - BioArray Software Environment. |
7837 |
28 Aug 20 |
nicklas |
Available at http://base.thep.lu.se/ |
7837 |
28 Aug 20 |
nicklas |
8 |
|
7837 |
28 Aug 20 |
nicklas |
BASE is free software; you can redistribute it and/or |
7837 |
28 Aug 20 |
nicklas |
modify it under the terms of the GNU General Public License |
7837 |
28 Aug 20 |
nicklas |
as published by the Free Software Foundation; either version 3 |
7837 |
28 Aug 20 |
nicklas |
of the License, or (at your option) any later version. |
7837 |
28 Aug 20 |
nicklas |
13 |
|
7837 |
28 Aug 20 |
nicklas |
BASE is distributed in the hope that it will be useful, |
7837 |
28 Aug 20 |
nicklas |
but WITHOUT ANY WARRANTY; without even the implied warranty of |
7837 |
28 Aug 20 |
nicklas |
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
7837 |
28 Aug 20 |
nicklas |
GNU General Public License for more details. |
7837 |
28 Aug 20 |
nicklas |
18 |
|
7837 |
28 Aug 20 |
nicklas |
You should have received a copy of the GNU General Public License |
7837 |
28 Aug 20 |
nicklas |
along with BASE. If not, see <http://www.gnu.org/licenses/>. |
7837 |
28 Aug 20 |
nicklas |
21 |
*/ |
7837 |
28 Aug 20 |
nicklas |
22 |
import java.util.Arrays; |
7837 |
28 Aug 20 |
nicklas |
23 |
import java.util.HashSet; |
7837 |
28 Aug 20 |
nicklas |
24 |
import java.util.Set; |
7837 |
28 Aug 20 |
nicklas |
25 |
|
7837 |
28 Aug 20 |
nicklas |
26 |
import net.sf.basedb.core.BaseException; |
7837 |
28 Aug 20 |
nicklas |
27 |
import net.sf.basedb.core.DbControl; |
7837 |
28 Aug 20 |
nicklas |
28 |
import net.sf.basedb.core.Item; |
7837 |
28 Aug 20 |
nicklas |
29 |
import net.sf.basedb.core.Nameable; |
7837 |
28 Aug 20 |
nicklas |
30 |
import net.sf.basedb.core.SyncFilter.SourceItemTransform; |
7837 |
28 Aug 20 |
nicklas |
31 |
import net.sf.basedb.util.listable.ListableUtil; |
7837 |
28 Aug 20 |
nicklas |
32 |
import net.sf.basedb.util.listable.SourceItemTransformer; |
7837 |
28 Aug 20 |
nicklas |
33 |
import net.sf.basedb.util.listable.TransformContext; |
7837 |
28 Aug 20 |
nicklas |
34 |
|
7837 |
28 Aug 20 |
nicklas |
35 |
public class TestSourceItemTransformer |
7837 |
28 Aug 20 |
nicklas |
36 |
{ |
7837 |
28 Aug 20 |
nicklas |
37 |
static boolean ok = true; |
7837 |
28 Aug 20 |
nicklas |
38 |
|
7837 |
28 Aug 20 |
nicklas |
39 |
public static void main(String[] args) |
7837 |
28 Aug 20 |
nicklas |
40 |
{ |
7837 |
28 Aug 20 |
nicklas |
41 |
TestUtil.checkArgs(args); |
7837 |
28 Aug 20 |
nicklas |
42 |
TestUtil.begin(); |
7837 |
28 Aug 20 |
nicklas |
43 |
ok = test_all(); |
7837 |
28 Aug 20 |
nicklas |
44 |
TestUtil.stop(); |
7837 |
28 Aug 20 |
nicklas |
45 |
} |
7837 |
28 Aug 20 |
nicklas |
46 |
|
7837 |
28 Aug 20 |
nicklas |
47 |
static boolean test_all() |
7837 |
28 Aug 20 |
nicklas |
48 |
{ |
7837 |
28 Aug 20 |
nicklas |
49 |
write("++Testing source item transformer"); |
7837 |
28 Aug 20 |
nicklas |
50 |
|
7837 |
28 Aug 20 |
nicklas |
// Create extracts: RNA, Library, Pooled library |
7837 |
28 Aug 20 |
nicklas |
52 |
int rna1 = TestExtract.test_create(0, "RNA.1", 0, 0, false); |
7837 |
28 Aug 20 |
nicklas |
53 |
int rna2 = TestExtract.test_create(0, "RNA.2", 0, 0, false); |
7837 |
28 Aug 20 |
nicklas |
54 |
int lib1 = TestExtract.test_create(rna1, "Lib.1", 0, 0, false); |
7837 |
28 Aug 20 |
nicklas |
55 |
int lib2 = TestExtract.test_create(rna2, "Lib.2", 0, 0, false); |
7837 |
28 Aug 20 |
nicklas |
56 |
int pool = TestExtract.test_create_pooled("Pool.1", lib1, lib2); |
7837 |
28 Aug 20 |
nicklas |
57 |
|
7837 |
28 Aug 20 |
nicklas |
// Create physical bioassay: Flow cell |
7837 |
28 Aug 20 |
nicklas |
59 |
int fc1 = TestPhysicalBioAssay.test_create(0, "FlowCell.1", 0, 1); |
7837 |
28 Aug 20 |
nicklas |
60 |
TestPhysicalBioAssay.test_add_extract(fc1, pool, 1, null); |
7837 |
28 Aug 20 |
nicklas |
61 |
|
7837 |
28 Aug 20 |
nicklas |
// Create derived bioassay: Sequencing, Alignment |
7837 |
28 Aug 20 |
nicklas |
63 |
int seqrun1 = TestDerivedBioAssay.test_create_root("SequencingRun.1", fc1, 0, 0, 0, 0, 0); |
7837 |
28 Aug 20 |
nicklas |
64 |
|
7837 |
28 Aug 20 |
nicklas |
65 |
int aligned1 = TestDerivedBioAssay.test_create_child("Aligned.1", lib1, 0, 0, 0, 0, seqrun1); |
7837 |
28 Aug 20 |
nicklas |
66 |
int aligned2 = TestDerivedBioAssay.test_create_child("Aligned.2", lib2, 0, 0, 0, 0, seqrun1); |
7837 |
28 Aug 20 |
nicklas |
67 |
|
7837 |
28 Aug 20 |
nicklas |
68 |
SourceItemTransformer tDown = create_transformer(Item.EXTRACT, Item.DERIVEDBIOASSAY, SourceItemTransform.PARENT_TO_CHILD); |
7837 |
28 Aug 20 |
nicklas |
69 |
SourceItemTransformer tUp = create_transformer(Item.DERIVEDBIOASSAY, Item.EXTRACT, SourceItemTransform.CHILD_TO_PARENT); |
7837 |
28 Aug 20 |
nicklas |
70 |
|
7837 |
28 Aug 20 |
nicklas |
// If we start at RNA level, we should get the Sequencing and only the Alignment that is linked |
7837 |
28 Aug 20 |
nicklas |
72 |
test_transform(tDown, new Integer[] { rna1 }, new Integer[] { seqrun1, aligned1 }); |
7837 |
28 Aug 20 |
nicklas |
73 |
test_transform(tDown, new Integer[] { rna2 }, new Integer[] { seqrun1, aligned2 }); |
7837 |
28 Aug 20 |
nicklas |
// If we start at the pool level, we should get all alignments |
7837 |
28 Aug 20 |
nicklas |
75 |
test_transform(tDown, new Integer[] { pool }, new Integer[] { seqrun1, aligned1, aligned2 }); |
7837 |
28 Aug 20 |
nicklas |
76 |
|
7837 |
28 Aug 20 |
nicklas |
// If we start at the Alignment level, we should get the pool and only the Library and RNA that is linked |
7837 |
28 Aug 20 |
nicklas |
78 |
test_transform(tUp, new Integer[] { aligned1 }, new Integer[] { pool, lib1, rna1 }); |
7837 |
28 Aug 20 |
nicklas |
79 |
test_transform(tUp, new Integer[] { aligned2 }, new Integer[] { pool, lib2, rna2 }); |
7837 |
28 Aug 20 |
nicklas |
// If we start at the Sequencing level, we should get all extracts |
7837 |
28 Aug 20 |
nicklas |
81 |
test_transform(tUp, new Integer[] { seqrun1 }, new Integer[] { pool, lib1, lib2, rna1, rna2 }); |
7837 |
28 Aug 20 |
nicklas |
82 |
|
7837 |
28 Aug 20 |
nicklas |
// Delete items |
7837 |
28 Aug 20 |
nicklas |
84 |
if (TestUtil.waitBeforeDelete()) TestUtil.waitForEnter(); |
7837 |
28 Aug 20 |
nicklas |
85 |
|
7837 |
28 Aug 20 |
nicklas |
86 |
TestDerivedBioAssay.test_delete(aligned1); |
7837 |
28 Aug 20 |
nicklas |
87 |
TestDerivedBioAssay.test_delete(aligned2); |
7837 |
28 Aug 20 |
nicklas |
88 |
TestDerivedBioAssay.test_delete(seqrun1); |
7837 |
28 Aug 20 |
nicklas |
89 |
TestPhysicalBioAssay.test_delete(fc1); |
7837 |
28 Aug 20 |
nicklas |
90 |
TestExtract.test_delete(pool); |
7837 |
28 Aug 20 |
nicklas |
91 |
TestExtract.test_delete(lib1); |
7837 |
28 Aug 20 |
nicklas |
92 |
TestExtract.test_delete(lib2); |
7837 |
28 Aug 20 |
nicklas |
93 |
TestExtract.test_delete(rna1); |
7837 |
28 Aug 20 |
nicklas |
94 |
TestExtract.test_delete(rna2); |
7837 |
28 Aug 20 |
nicklas |
95 |
|
7837 |
28 Aug 20 |
nicklas |
96 |
write("++Testing source item transformer "+(ok ? "OK" : "Failed")+"\n"); |
7837 |
28 Aug 20 |
nicklas |
97 |
return ok; |
7837 |
28 Aug 20 |
nicklas |
98 |
} |
7837 |
28 Aug 20 |
nicklas |
99 |
|
7837 |
28 Aug 20 |
nicklas |
100 |
|
7837 |
28 Aug 20 |
nicklas |
101 |
static void write(String message) |
7837 |
28 Aug 20 |
nicklas |
102 |
{ |
7837 |
28 Aug 20 |
nicklas |
103 |
System.out.println(message); |
7837 |
28 Aug 20 |
nicklas |
104 |
} |
7837 |
28 Aug 20 |
nicklas |
105 |
|
7837 |
28 Aug 20 |
nicklas |
106 |
static SourceItemTransformer create_transformer(Item sourceType, Item targetType, SourceItemTransform transform) |
7837 |
28 Aug 20 |
nicklas |
107 |
{ |
7837 |
28 Aug 20 |
nicklas |
108 |
return ListableUtil.getTransformerFactory(targetType).create(sourceType, transform); |
7837 |
28 Aug 20 |
nicklas |
109 |
} |
7837 |
28 Aug 20 |
nicklas |
110 |
|
7837 |
28 Aug 20 |
nicklas |
111 |
|
7837 |
28 Aug 20 |
nicklas |
112 |
static void test_transform(SourceItemTransformer transformer, Integer[] source, Integer[] expectedResult) |
7837 |
28 Aug 20 |
nicklas |
113 |
{ |
7837 |
28 Aug 20 |
nicklas |
114 |
DbControl dc = null; |
7837 |
28 Aug 20 |
nicklas |
115 |
try |
7837 |
28 Aug 20 |
nicklas |
116 |
{ |
7837 |
28 Aug 20 |
nicklas |
117 |
dc = TestUtil.getDbControl(); |
7837 |
28 Aug 20 |
nicklas |
118 |
|
7837 |
28 Aug 20 |
nicklas |
119 |
Set<Integer> srcSet = new HashSet<>(Arrays.asList(source)); |
7837 |
28 Aug 20 |
nicklas |
120 |
Set<Integer> expectedResultSet = new HashSet<>(Arrays.asList(expectedResult)); |
7837 |
28 Aug 20 |
nicklas |
121 |
Set<Integer> result = transformer.transform(new TransformContext(dc), srcSet); |
7837 |
28 Aug 20 |
nicklas |
122 |
|
7837 |
28 Aug 20 |
nicklas |
123 |
write_transform(dc, transformer, srcSet, result); |
7837 |
28 Aug 20 |
nicklas |
124 |
|
7837 |
28 Aug 20 |
nicklas |
125 |
if (!result.equals(expectedResultSet)) |
7837 |
28 Aug 20 |
nicklas |
126 |
{ |
7837 |
28 Aug 20 |
nicklas |
127 |
throw new BaseException("Expected "+expectedResultSet+" results, not "+result); |
7837 |
28 Aug 20 |
nicklas |
128 |
} |
7837 |
28 Aug 20 |
nicklas |
129 |
|
7837 |
28 Aug 20 |
nicklas |
130 |
write("--Transform " + transformer.getSourceItemType().name() + " -> " + transformer.getTargetItemType().name() + " OK"); |
7837 |
28 Aug 20 |
nicklas |
131 |
} |
7837 |
28 Aug 20 |
nicklas |
132 |
catch (Throwable ex) |
7837 |
28 Aug 20 |
nicklas |
133 |
{ |
7837 |
28 Aug 20 |
nicklas |
134 |
write("--Transform " + transformer.getSourceItemType().name() + " -> " + transformer.getTargetItemType().name() + " FAILED"); |
7837 |
28 Aug 20 |
nicklas |
135 |
ex.printStackTrace(); |
7837 |
28 Aug 20 |
nicklas |
136 |
ok = false; |
7837 |
28 Aug 20 |
nicklas |
137 |
} |
7837 |
28 Aug 20 |
nicklas |
138 |
finally |
7837 |
28 Aug 20 |
nicklas |
139 |
{ |
7837 |
28 Aug 20 |
nicklas |
140 |
if (dc != null) dc.close(); |
7837 |
28 Aug 20 |
nicklas |
141 |
} |
7837 |
28 Aug 20 |
nicklas |
142 |
|
7837 |
28 Aug 20 |
nicklas |
143 |
} |
7837 |
28 Aug 20 |
nicklas |
144 |
|
7837 |
28 Aug 20 |
nicklas |
145 |
|
7837 |
28 Aug 20 |
nicklas |
146 |
static void write_transform(DbControl dc, SourceItemTransformer transformer, Set<Integer> src, Set<Integer> result) |
7837 |
28 Aug 20 |
nicklas |
147 |
throws BaseException |
7837 |
28 Aug 20 |
nicklas |
148 |
{ |
7837 |
28 Aug 20 |
nicklas |
149 |
if (TestUtil.getSilent()) return; |
7837 |
28 Aug 20 |
nicklas |
150 |
|
7837 |
28 Aug 20 |
nicklas |
151 |
StringBuilder sb = new StringBuilder(); |
7837 |
28 Aug 20 |
nicklas |
152 |
sb.append(":"+transformer.getSourceItemType().name()+"\n"); |
7837 |
28 Aug 20 |
nicklas |
153 |
for (Integer id : src) |
7837 |
28 Aug 20 |
nicklas |
154 |
{ |
7837 |
28 Aug 20 |
nicklas |
155 |
Nameable item = transformer.getSourceItemType().getById(dc, id); |
7837 |
28 Aug 20 |
nicklas |
156 |
sb.append(" " + item.getName()+ "\t" + item.getId() +"\n"); |
7837 |
28 Aug 20 |
nicklas |
157 |
} |
7837 |
28 Aug 20 |
nicklas |
158 |
sb.append(":" + transformer.getTargetItemType().name()+"\n"); |
7837 |
28 Aug 20 |
nicklas |
159 |
for (Integer id : result) |
7837 |
28 Aug 20 |
nicklas |
160 |
{ |
7837 |
28 Aug 20 |
nicklas |
161 |
Nameable item = transformer.getTargetItemType().getById(dc, id); |
7837 |
28 Aug 20 |
nicklas |
162 |
sb.append(" " + item.getName()+"\t"+item.getId()+"\n"); |
7837 |
28 Aug 20 |
nicklas |
163 |
} |
7837 |
28 Aug 20 |
nicklas |
164 |
System.out.print(sb.toString()); |
7837 |
28 Aug 20 |
nicklas |
165 |
} |
7837 |
28 Aug 20 |
nicklas |
166 |
|
7837 |
28 Aug 20 |
nicklas |
167 |
|
7837 |
28 Aug 20 |
nicklas |
168 |
} |