2828import java .io .FileWriter ;
2929import java .io .IOException ;
3030
31+ import org .biojava .nbio .structure .Atom ;
3132import org .biojava .nbio .structure .Chain ;
3233import org .biojava .nbio .structure .Structure ;
3334import org .biojava .nbio .structure .StructureException ;
3435import org .biojava .nbio .structure .StructureIO ;
36+ import org .biojava .nbio .structure .StructureTools ;
3537import org .biojava .nbio .structure .align .util .AtomCache ;
3638import org .biojava .nbio .structure .io .mmcif .MMCIFFileTools ;
3739import org .biojava .nbio .structure .io .mmcif .MMcifParser ;
@@ -45,59 +47,8 @@ public class TestMMCIFWriting {
4547
4648 @ Test
4749 public void test1SMT () throws IOException , StructureException {
48- AtomCache cache = new AtomCache ();
49-
50- StructureIO .setAtomCache (cache );
51-
52- cache .setUseMmCif (true );
53-
54- FileParsingParameters params = new FileParsingParameters ();
55- params .setAlignSeqRes (true );
56- cache .setFileParsingParams (params );
57-
58- Structure originalStruct = StructureIO .getStructure ("1SMT" );
59-
60- File outputFile = File .createTempFile ("biojava_testing_" , ".cif" );
61- outputFile .deleteOnExit ();
62-
63- FileWriter fw = new FileWriter (outputFile );
64- fw .write (originalStruct .toMMCIF ());
65- fw .close ();
66-
67-
68- MMcifParser parser = new SimpleMMcifParser ();
69-
70- SimpleMMcifConsumer consumer = new SimpleMMcifConsumer ();
71-
72- FileParsingParameters fileParsingParams = new FileParsingParameters ();
73- fileParsingParams .setAlignSeqRes (true );
74-
75- consumer .setFileParsingParameters (fileParsingParams );
76-
77- parser .addMMcifConsumer (consumer );
78-
79- //parser.parse(new BufferedReader(new FileReader(new File("/home/duarte_j/test.cif"))));
80- parser .parse (new BufferedReader (new FileReader (outputFile )));
81-
82- Structure readStruct = consumer .getStructure ();
83-
84- assertNotNull (readStruct );
85-
86- assertEquals (originalStruct .getChains ().size (), readStruct .getChains ().size ());
87-
88- for (int i =0 ;i <originalStruct .getChains ().size ();i ++) {
89- assertEquals (originalStruct .getChains ().get (i ).getAtomGroups ().size (),
90- readStruct .getChains ().get (i ).getAtomGroups ().size ());
91-
92- Chain origChain = originalStruct .getChains ().get (i );
93- Chain readChain = readStruct .getChains ().get (i );
94-
95- assertEquals (origChain .getAtomGroups ().size (), readChain .getAtomGroups ().size ());
96- //assertEquals(origChain.getSeqResGroups().size(), readChain.getSeqResGroups().size());
97- }
98-
99- // Test cell and symmetry
100- assertEquals (originalStruct .getCrystallographicInfo ().getSpaceGroup (),readStruct .getCrystallographicInfo ().getSpaceGroup ());
50+ // an x-ray structure
51+ testRoundTrip ("1SMT" );
10152 }
10253
10354 /**
@@ -107,71 +58,57 @@ public void test1SMT() throws IOException, StructureException {
10758 */
10859 @ Test
10960 public void test2N3J () throws IOException , StructureException {
110- AtomCache cache = new AtomCache ();
111-
112- StructureIO .setAtomCache (cache );
113-
114- cache .setUseMmCif (true );
115-
116- FileParsingParameters params = new FileParsingParameters ();
117- params .setAlignSeqRes (true );
118- cache .setFileParsingParams (params );
119-
120- Structure originalStruct = StructureIO .getStructure ("2N3J" );
121-
122- File outputFile = File .createTempFile ("biojava_testing_" , ".cif" );
123- outputFile .deleteOnExit ();
124-
125-
126- FileWriter fw = new FileWriter (outputFile );
127- fw .write (originalStruct .toMMCIF ());
128- fw .close ();
129-
130-
131- MMcifParser parser = new SimpleMMcifParser ();
132-
133- SimpleMMcifConsumer consumer = new SimpleMMcifConsumer ();
134-
135- FileParsingParameters fileParsingParams = new FileParsingParameters ();
136- fileParsingParams .setAlignSeqRes (true );
137-
138- consumer .setFileParsingParameters (fileParsingParams );
139-
140- parser .addMMcifConsumer (consumer );
141-
142- //parser.parse(new BufferedReader(new FileReader(new File("/home/duarte_j/test.cif"))));
143- parser .parse (new BufferedReader (new FileReader (outputFile )));
144-
145- Structure readStruct = consumer .getStructure ();
146-
147- assertNotNull (readStruct );
148-
149- assertEquals (originalStruct .getChains ().size (), readStruct .getChains ().size ());
150-
151- assertEquals (originalStruct .nrModels (), readStruct .nrModels ());
61+ // an NMR structure (multimodel) with 2 chains
62+ testRoundTrip ("2N3J" );
63+ }
64+
65+ @ Test
66+ public void test1A2C () throws IOException , StructureException {
67+ // a structure with insertion codes
68+ testRoundTrip ("1A2C" );
69+ }
70+
71+ private static class DemoBean {
72+ @ IgnoreField
73+ String not_a_field ;
74+
75+ @ SuppressWarnings ("unused" )//used by reflection
76+ String default_field ;
77+
78+ @ CIFLabel (label ="custom_label" )
79+ String custom_field ;
15280
153- for ( int i = 0 ; i < originalStruct . nrModels (); i ++ ) {
154- assertEquals ( originalStruct . getModel ( i ). size (), readStruct . getModel ( i ). size ()) ;
81+ public void setNot_a_field ( String not_a_field ) {
82+ this . not_a_field = not_a_field ;
15583 }
156-
157- for (int i =0 ;i <originalStruct .getChains ().size ();i ++) {
158- assertEquals (originalStruct .getChains ().get (i ).getAtomGroups ().size (),
159- readStruct .getChains ().get (i ).getAtomGroups ().size ());
160-
161- Chain origChain = originalStruct .getChains ().get (i );
162- Chain readChain = readStruct .getChains ().get (i );
163-
164- assertEquals (origChain .getAtomGroups ().size (), readChain .getAtomGroups ().size ());
165- //assertEquals(origChain.getSeqResGroups().size(), readChain.getSeqResGroups().size());
84+ public void setDefault_field (String default_field ) {
85+ this .default_field = default_field ;
86+ }
87+ public void setCustom_field (String custom_field ) {
88+ this .custom_field = custom_field ;
16689 }
167-
16890 }
169-
91+
17092 @ Test
171- public void test1A2C () throws IOException , StructureException {
93+ public void testBeanAnnotations () {
94+ DemoBean bean = new DemoBean ();
95+ bean .setCustom_field ("custom_field" );
96+ bean .setDefault_field (null );
97+ bean .setNot_a_field ("not_a_field" );
17298
173- // a structure with insertion codes
17499
100+ // Test (1) should have custom_label (@CIFLabel)
101+ // (2) shouldn't have not_a_field (@IgnoreField)
102+ String newline = System .getProperty ("line.separator" );
103+ String mmcif = MMCIFFileTools .toMMCIF ("_demo" , bean );
104+ String expected =
105+ "_demo.default_field ?" + newline
106+ + "_demo.custom_label custom_field" + newline
107+ + "#" + newline ;
108+ assertEquals (expected , mmcif );
109+ }
110+
111+ private static void testRoundTrip (String pdbId ) throws IOException , StructureException {
175112 AtomCache cache = new AtomCache ();
176113
177114 StructureIO .setAtomCache (cache );
@@ -182,7 +119,7 @@ public void test1A2C() throws IOException, StructureException {
182119 params .setAlignSeqRes (true );
183120 cache .setFileParsingParams (params );
184121
185- Structure originalStruct = StructureIO .getStructure ("1A2C" );
122+ Structure originalStruct = StructureIO .getStructure (pdbId );
186123
187124 File outputFile = File .createTempFile ("biojava_testing_" , ".cif" );
188125 outputFile .deleteOnExit ();
@@ -204,7 +141,6 @@ public void test1A2C() throws IOException, StructureException {
204141
205142 parser .addMMcifConsumer (consumer );
206143
207- //parser.parse(new BufferedReader(new FileReader(new File("/home/duarte_j/test.cif"))));
208144 parser .parse (new BufferedReader (new FileReader (outputFile )));
209145
210146 Structure readStruct = consumer .getStructure ();
@@ -213,56 +149,60 @@ public void test1A2C() throws IOException, StructureException {
213149
214150 assertEquals (originalStruct .getChains ().size (), readStruct .getChains ().size ());
215151
216- for (int i =0 ;i <originalStruct .getChains ().size ();i ++) {
217- assertEquals (originalStruct .getChains ().get (i ).getAtomGroups ().size (),
218- readStruct .getChains ().get (i ).getAtomGroups ().size ());
219-
220- Chain origChain = originalStruct .getChains ().get (i );
221- Chain readChain = readStruct .getChains ().get (i );
152+ assertEquals (originalStruct .nrModels (), readStruct .nrModels ());
222153
223- assertEquals ( origChain . getAtomGroups (). size (), readChain . getAtomGroups (). size ());
224- // assertEquals(origChain.getSeqResGroups( ).size(), readChain.getSeqResGroups( ).size());
154+ for ( int i = 0 ; i < originalStruct . nrModels (); i ++) {
155+ assertEquals (originalStruct . getModel ( i ).size (), readStruct . getModel ( i ).size ());
225156 }
226157
227- }
228-
229- private static class DemoBean {
230- @ IgnoreField
231- String not_a_field ;
232158
233- @ SuppressWarnings ("unused" )//used by reflection
234- String default_field ;
235159
236- @ CIFLabel (label ="custom_label" )
237- String custom_field ;
160+ for (int modelIdx =0 ;modelIdx <originalStruct .nrModels ();modelIdx ++) {
161+
162+ for (int i =0 ;i <originalStruct .getModel (modelIdx ).size ();i ++) {
163+ assertEquals (originalStruct .getChains ().get (i ).getAtomGroups ().size (),
164+ readStruct .getChains ().get (i ).getAtomGroups ().size ());
165+
166+ Chain origChain = originalStruct .getModel (modelIdx ).get (i );
167+ Chain readChain = readStruct .getModel (modelIdx ).get (i );
168+
169+ assertEquals (origChain .getAtomGroups ().size (), readChain .getAtomGroups ().size ());
170+ //assertEquals(origChain.getSeqResGroups().size(), readChain.getSeqResGroups().size());
171+ assertEquals (origChain .getChainID (), readChain .getChainID ());
172+
173+ Atom [] origAtoms = StructureTools .getAllAtomArray (origChain );
174+ Atom [] readAtoms = StructureTools .getAllAtomArray (readChain );
175+
176+ assertEquals (origAtoms .length , readAtoms .length );
177+
178+ for (int atomIdx =0 ;atomIdx <origAtoms .length ;atomIdx ++) {
179+
180+ assertEquals ("atom serials don't match for atom " +origAtoms [atomIdx ].toString (),
181+ origAtoms [atomIdx ].getPDBserial (), readAtoms [atomIdx ].getPDBserial ());
182+
183+ assertEquals ("atom names don't match for atom " +origAtoms [atomIdx ].toString (),
184+ origAtoms [atomIdx ].getName (), readAtoms [atomIdx ].getName ());
185+
186+ assertEquals ("atom elements don't match for atom " +origAtoms [atomIdx ].toString (),
187+ origAtoms [atomIdx ].getElement (), readAtoms [atomIdx ].getElement ());
188+
189+ assertEquals ("x values don't match for atom " +origAtoms [atomIdx ].toString (),
190+ origAtoms [atomIdx ].getX (), readAtoms [atomIdx ].getX (),0.0001 );
191+
192+ assertEquals ("y values don't match for atom " +origAtoms [atomIdx ].toString (),
193+ origAtoms [atomIdx ].getY (), readAtoms [atomIdx ].getY (),0.0001 );
194+
195+ assertEquals ("z values don't match for atom " +origAtoms [atomIdx ].toString (),
196+ origAtoms [atomIdx ].getZ (), readAtoms [atomIdx ].getZ (),0.0001 );
197+ }
198+ }
238199
239- public void setNot_a_field (String not_a_field ) {
240- this .not_a_field = not_a_field ;
241- }
242- public void setDefault_field (String default_field ) {
243- this .default_field = default_field ;
244- }
245- public void setCustom_field (String custom_field ) {
246- this .custom_field = custom_field ;
247200 }
248- }
249201
250- @ Test
251- public void testBeanAnnotations () {
252- DemoBean bean = new DemoBean ();
253- bean .setCustom_field ("custom_field" );
254- bean .setDefault_field (null );
255- bean .setNot_a_field ("not_a_field" );
256-
257-
258- // Test (1) should have custom_label (@CIFLabel)
259- // (2) shouldn't have not_a_field (@IgnoreField)
260- String newline = System .getProperty ("line.separator" );
261- String mmcif = MMCIFFileTools .toMMCIF ("_demo" , bean );
262- String expected =
263- "_demo.default_field ?" + newline
264- + "_demo.custom_label custom_field" + newline
265- + "#" + newline ;
266- assertEquals (expected , mmcif );
202+ // Test cell and symmetry
203+ assertEquals (originalStruct .getCrystallographicInfo ().getSpaceGroup (),
204+ readStruct .getCrystallographicInfo ().getSpaceGroup ());
205+
206+
267207 }
268208}
0 commit comments