|
| 1 | +package org.biojava.nbio.structure.mmtf; |
| 2 | + |
| 3 | + |
| 4 | +import org.junit.Test; |
| 5 | + |
| 6 | +import static org.junit.Assert.*; |
| 7 | + |
| 8 | +import java.io.IOException; |
| 9 | +import java.util.ArrayList; |
| 10 | +import java.util.HashSet; |
| 11 | +import java.util.List; |
| 12 | +import java.util.Set; |
| 13 | + |
| 14 | +import org.biojava.nbio.structure.Atom; |
| 15 | +import org.biojava.nbio.structure.Chain; |
| 16 | +import org.biojava.nbio.structure.Group; |
| 17 | +import org.biojava.nbio.structure.Structure; |
| 18 | +import org.biojava.nbio.structure.StructureException; |
| 19 | +import org.biojava.nbio.structure.StructureIO; |
| 20 | +import org.biojava.nbio.structure.io.mmtf.MmtfUtils; |
| 21 | + |
| 22 | +public class TestMmtfUtils { |
| 23 | + |
| 24 | + /** |
| 25 | + * Integration test to see that the microheterogenity is being dealt with correctly. |
| 26 | + * @throws IOException |
| 27 | + * @throws StructureException |
| 28 | + */ |
| 29 | + @Test |
| 30 | + public void microHeterogenity() throws IOException, StructureException { |
| 31 | + MmtfUtils.setUpBioJava(); |
| 32 | + Structure inputStructure = StructureIO.getStructure("4ck4"); |
| 33 | + // Count the number of groups |
| 34 | + Group before = inputStructure.getChains().get(0).getAtomGroup(17); |
| 35 | + assertTrue(inputStructure.getChains().get(0).getAtomGroup(17).hasAltLoc()); |
| 36 | + List<Atom> totalAtoms = new ArrayList<>(MmtfUtils.getAllAtoms(inputStructure)); |
| 37 | + int totGroups = 0; |
| 38 | + int totAtomsCounter = 0; |
| 39 | + Set<Atom> totAtoms = new HashSet<>(); |
| 40 | + for (Chain c : inputStructure.getChains()) { |
| 41 | + totGroups += c.getAtomGroups().size(); |
| 42 | + for (Group g: c.getAtomGroups() ){ |
| 43 | + totAtomsCounter+=g.getAtoms().size(); |
| 44 | + totAtoms.addAll(g.getAtoms()); |
| 45 | + for (Group alt : g.getAltLocs()) { |
| 46 | + totAtomsCounter+=alt.getAtoms().size(); |
| 47 | + totAtoms.addAll(alt.getAtoms()); |
| 48 | + } |
| 49 | + } |
| 50 | + } |
| 51 | + // Now "fix" the microheterogenity |
| 52 | + MmtfUtils.fixMicroheterogenity(inputStructure); |
| 53 | + assertEquals(before, inputStructure.getChains().get(0).getAtomGroup(17)); |
| 54 | + assertFalse(inputStructure.getChains().get(0).getAtomGroup(17).hasAltLoc()); |
| 55 | + assertFalse(inputStructure.getChains().get(0).getAtomGroup(18).hasAltLoc()); |
| 56 | + int totGroupsAfter = 0; |
| 57 | + int totAtomsCounterAfter = 0; |
| 58 | + Set<Atom> totAtomsAfter = new HashSet<>(); |
| 59 | + for (Chain c : inputStructure.getChains()) { |
| 60 | + totGroupsAfter += c.getAtomGroups().size(); |
| 61 | + for (Group g: c.getAtomGroups() ){ |
| 62 | + totAtomsCounterAfter+=g.getAtoms().size(); |
| 63 | + totAtomsAfter.addAll(g.getAtoms()); |
| 64 | + for (Group alt : g.getAltLocs()) { |
| 65 | + totAtomsAfter.addAll(alt.getAtoms()); |
| 66 | + totAtomsCounterAfter+=alt.getAtoms().size(); |
| 67 | + } |
| 68 | + } |
| 69 | + } |
| 70 | + // Find the atoms after the fix. |
| 71 | + List<Atom> totalAtomsAfter = new ArrayList<>(MmtfUtils.getAllAtoms(inputStructure)); |
| 72 | + // Get all of the duplicate atoms |
| 73 | + Set<Atom> duplicates = findDuplicates(totalAtomsAfter); |
| 74 | + for (Atom a : duplicates) { |
| 75 | + System.out.println(a); |
| 76 | + } |
| 77 | + // There should be no duplicates |
| 78 | + assertEquals(duplicates.size(), 0); |
| 79 | + assertEquals(totalAtoms.size(), totalAtomsAfter.size()); |
| 80 | + // Check there are two more groups afterwards |
| 81 | + assertEquals(totGroupsAfter-2, totGroups); |
| 82 | + // Check there are no more atoms afterwards |
| 83 | + assertEquals(totAtomsAfter.size(), totAtoms.size()); |
| 84 | + // Check the counter are the same too |
| 85 | + assertEquals(totAtomsCounterAfter, totAtomsCounter); |
| 86 | + |
| 87 | + } |
| 88 | + |
| 89 | + |
| 90 | +//TODO ADD TESTS FOR THESE FUNCTIONS |
| 91 | +// getAllAtoms |
| 92 | +// |
| 93 | +// getAtomsForGroup |
| 94 | +// |
| 95 | +// calculateDsspSecondaryStructure |
| 96 | +// |
| 97 | +// setHeaderInfo |
| 98 | +// |
| 99 | +// generateSerializableBioAssembly |
| 100 | +// |
| 101 | +// getChainIdToIndexMap |
| 102 | + |
| 103 | + private Set<Atom> findDuplicates(List<Atom> listContainingDuplicates) |
| 104 | + { |
| 105 | + final Set<Atom> setToReturn = new HashSet<>(); |
| 106 | + final Set<Atom> set1 = new HashSet<>(); |
| 107 | + |
| 108 | + for (Atom yourInt : listContainingDuplicates) |
| 109 | + { |
| 110 | + if (!set1.add(yourInt)) |
| 111 | + { |
| 112 | + setToReturn.add(yourInt); |
| 113 | + } |
| 114 | + } |
| 115 | + return setToReturn; |
| 116 | + } |
| 117 | +} |
| 118 | + |
0 commit comments