Skip to content

Commit 27a5799

Browse files
authored
Merge pull request #1058 from xcalibur91/master
Refactoring : Extract method getBackboneAtomArray changes to reduce the cyclomatic complexity
2 parents c573d5b + a135683 commit 27a5799

File tree

3 files changed

+57
-69
lines changed

3 files changed

+57
-69
lines changed

biojava-structure/src/main/java/org/biojava/nbio/structure/StructureTools.java

Lines changed: 25 additions & 48 deletions
Original file line numberDiff line numberDiff line change
@@ -167,6 +167,9 @@ public class StructureTools {
167167

168168
private static final Set<Element> hBondDonorAcceptors;
169169

170+
private static final Set<String> NUCLEOTIDE_BACKBONE_ATOMS;
171+
private static final Set<String> AMINOACID_BACKBONE_ATOMS;
172+
170173
static {
171174
nucleotides30 = new HashMap<String, Character>();
172175
nucleotides30.put("DA", 'A');
@@ -256,6 +259,9 @@ public class StructureTools {
256259
hBondDonorAcceptors.add(Element.O);
257260
hBondDonorAcceptors.add(Element.S);
258261

262+
NUCLEOTIDE_BACKBONE_ATOMS = new HashSet<>(Arrays.asList(C1_ATOM_NAME, C2_ATOM_NAME, C3_ATOM_NAME, C4_ATOM_NAME, O2_ATOM_NAME, O3_ATOM_NAME, O4_ATOM_NAME, O5_ATOM_NAME, OP1_ATOM_NAME, OP2_ATOM_NAME, P_ATOM_NAME));
263+
AMINOACID_BACKBONE_ATOMS = new HashSet<>(Arrays.asList(CA_ATOM_NAME, C_ATOM_NAME, N_ATOM_NAME, O_ATOM_NAME));
264+
259265
}
260266

261267
/**
@@ -1128,65 +1134,36 @@ public static Atom[] getRepresentativeAtomArray(Structure s) {
11281134
* @return an Atom[] array
11291135
*/
11301136
public static Atom[] getBackboneAtomArray(Structure s) {
1131-
11321137
List<Atom> atoms = new ArrayList<>();
1133-
11341138
for (Chain c : s.getChains()) {
11351139
for (Group g : c.getAtomGroups()) {
11361140
if (g.hasAminoAtoms()) {
1137-
// this means we will only take atoms grom groups that have
1138-
// complete backbones
1139-
for (Atom a : g.getAtoms()) {
1140-
switch (g.getType()) {
1141-
case NUCLEOTIDE:
1142-
// Nucleotide backbone
1143-
if (a.getName().equals(C1_ATOM_NAME))
1144-
atoms.add(a);
1145-
if (a.getName().equals(C2_ATOM_NAME))
1146-
atoms.add(a);
1147-
if (a.getName().equals(C3_ATOM_NAME))
1148-
atoms.add(a);
1149-
if (a.getName().equals(C4_ATOM_NAME))
1150-
atoms.add(a);
1151-
if (a.getName().equals(O2_ATOM_NAME))
1152-
atoms.add(a);
1153-
if (a.getName().equals(O3_ATOM_NAME))
1154-
atoms.add(a);
1155-
if (a.getName().equals(O4_ATOM_NAME))
1156-
atoms.add(a);
1157-
if (a.getName().equals(O5_ATOM_NAME))
1158-
atoms.add(a);
1159-
if (a.getName().equals(OP1_ATOM_NAME))
1160-
atoms.add(a);
1161-
if (a.getName().equals(OP2_ATOM_NAME))
1162-
atoms.add(a);
1163-
if (a.getName().equals(P_ATOM_NAME))
1164-
atoms.add(a);
1165-
// TODO Allow C4* names as well as C4'? -SB 3/2015
1166-
break;
1167-
case AMINOACID:
1168-
default:
1169-
// we do it this way instead of with g.getAtom() to
1170-
// be sure we always use the same order as original
1171-
if (a.getName().equals(CA_ATOM_NAME))
1172-
atoms.add(a);
1173-
if (a.getName().equals(C_ATOM_NAME))
1174-
atoms.add(a);
1175-
if (a.getName().equals(N_ATOM_NAME))
1176-
atoms.add(a);
1177-
if (a.getName().equals(O_ATOM_NAME))
1178-
atoms.add(a);
1179-
break;
1180-
}
1141+
if (g.getType() == GroupType.NUCLEOTIDE) {
1142+
addNucleotideAndAminoAtoms(atoms, g, NUCLEOTIDE_BACKBONE_ATOMS);
1143+
} else {
1144+
addNucleotideAndAminoAtoms(atoms, g, AMINOACID_BACKBONE_ATOMS);
11811145
}
11821146
}
11831147
}
1184-
11851148
}
1186-
11871149
return atoms.toArray(new Atom[0]);
11881150
}
11891151

1152+
/**
1153+
* This method will be used to add the Nucleotide and Amino atoms to the backbone Atom arrays based on the pre-defined Atom names.
1154+
* @param atoms
1155+
* @param g
1156+
* @param atomNames
1157+
*/
1158+
private static void addNucleotideAndAminoAtoms(List<Atom> atoms, Group g, Set<String> atomNames) {
1159+
for (Atom a : g.getAtoms()) {
1160+
if (atomNames.contains(a.getName())) {
1161+
atoms.add(a);
1162+
}
1163+
}
1164+
}
1165+
1166+
11901167
/**
11911168
* Convert three character amino acid codes into single character e.g.
11921169
* convert CYS to C. Valid 3-letter codes will be those of the standard 20

biojava-structure/src/main/java/org/biojava/nbio/structure/contact/StructureInterface.java

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -616,10 +616,7 @@ public double getContactOverlapScore(StructureInterface other, boolean invert) {
616616
Pair<EntityInfo> thisCompounds = new Pair<EntityInfo>(thisChains.getFirst().getEntityInfo(), thisChains.getSecond().getEntityInfo());
617617
Pair<EntityInfo> otherCompounds = new Pair<EntityInfo>(otherChains.getFirst().getEntityInfo(), otherChains.getSecond().getEntityInfo());
618618

619-
if ( ( (thisCompounds.getFirst().getMolId() == otherCompounds.getFirst().getMolId()) &&
620-
(thisCompounds.getSecond().getMolId() == otherCompounds.getSecond().getMolId()) ) ||
621-
( (thisCompounds.getFirst().getMolId() == otherCompounds.getSecond().getMolId()) &&
622-
(thisCompounds.getSecond().getMolId() == otherCompounds.getFirst().getMolId()) ) ) {
619+
if (checkMolIdMatch(thisCompounds,otherCompounds)) {
623620

624621
int common = 0;
625622
GroupContactSet thisContacts = getGroupContacts();
@@ -653,6 +650,17 @@ public double getContactOverlapScore(StructureInterface other, boolean invert) {
653650
}
654651
}
655652

653+
/**
654+
* This method check if two compounds have same MolIds or not.
655+
* @param thisCompounds
656+
* @param otherCompounds
657+
* @return
658+
*/
659+
private boolean checkMolIdMatch(Pair<EntityInfo> thisCompounds, Pair<EntityInfo> otherCompounds){
660+
boolean firstMatch = thisCompounds.getFirst().getMolId() == otherCompounds.getFirst().getMolId() && thisCompounds.getSecond().getMolId() == otherCompounds.getSecond().getMolId();
661+
boolean secondMatch = thisCompounds.getFirst().getMolId() == otherCompounds.getSecond().getMolId() && thisCompounds.getSecond().getMolId() == otherCompounds.getFirst().getMolId();
662+
return firstMatch || secondMatch;
663+
}
656664
public GroupContactSet getGroupContacts() {
657665
if (groupContacts==null) {
658666
this.groupContacts = new GroupContactSet(contacts);

biojava-structure/src/main/java/org/biojava/nbio/structure/xtal/CrystalTransform.java

Lines changed: 20 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -180,26 +180,29 @@ public boolean isIdentity() {
180180
*/
181181
public boolean isPureTranslation() {
182182
if (isPureCrystalTranslation()) return true;
183-
if (SpaceGroup.deltaComp(matTransform.m00,1,SpaceGroup.DELTA) &&
184-
SpaceGroup.deltaComp(matTransform.m01,0,SpaceGroup.DELTA) &&
185-
SpaceGroup.deltaComp(matTransform.m02,0,SpaceGroup.DELTA) &&
186-
187-
SpaceGroup.deltaComp(matTransform.m10,0,SpaceGroup.DELTA) &&
188-
SpaceGroup.deltaComp(matTransform.m11,1,SpaceGroup.DELTA) &&
189-
SpaceGroup.deltaComp(matTransform.m12,0,SpaceGroup.DELTA) &&
190-
191-
SpaceGroup.deltaComp(matTransform.m20,0,SpaceGroup.DELTA) &&
192-
SpaceGroup.deltaComp(matTransform.m21,0,SpaceGroup.DELTA) &&
193-
SpaceGroup.deltaComp(matTransform.m22,1,SpaceGroup.DELTA) &&
194-
( Math.abs(matTransform.m03-0.0)>SpaceGroup.DELTA ||
195-
Math.abs(matTransform.m13-0.0)>SpaceGroup.DELTA ||
196-
Math.abs(matTransform.m23-0.0)>SpaceGroup.DELTA)) {
197-
return true;
198-
}
199-
183+
if (isPureMatrixTranslation()) return true;
200184
return false;
201185
}
202186

187+
/**
188+
* This method will help check if the matrix translation is pure or not.
189+
* @return boolean
190+
*/
191+
private boolean isPureMatrixTranslation(){
192+
return SpaceGroup.deltaComp(matTransform.m00,1,SpaceGroup.DELTA) &&
193+
SpaceGroup.deltaComp(matTransform.m01,0,SpaceGroup.DELTA) &&
194+
SpaceGroup.deltaComp(matTransform.m02,0,SpaceGroup.DELTA) &&
195+
196+
SpaceGroup.deltaComp(matTransform.m10,0,SpaceGroup.DELTA) &&
197+
SpaceGroup.deltaComp(matTransform.m11,1,SpaceGroup.DELTA) &&
198+
SpaceGroup.deltaComp(matTransform.m12,0,SpaceGroup.DELTA) &&
199+
200+
SpaceGroup.deltaComp(matTransform.m20,0,SpaceGroup.DELTA) &&
201+
SpaceGroup.deltaComp(matTransform.m21,0,SpaceGroup.DELTA) &&
202+
SpaceGroup.deltaComp(matTransform.m22,1,SpaceGroup.DELTA) &&
203+
(Math.abs(matTransform.m03-0.0)>SpaceGroup.DELTA || Math.abs(matTransform.m13-0.0)>SpaceGroup.DELTA || Math.abs(matTransform.m23-0.0)>SpaceGroup.DELTA);
204+
}
205+
203206
/**
204207
* Tells whether this transformation contains a fractional translational
205208
* component (whatever its rotational component). A fractional translation

0 commit comments

Comments
 (0)