Skip to content

Commit bf40264

Browse files
author
Sreejith Nair
committed
Refactoring : Extract method changes to reduce the cyclomatic complexity of getBackboneAtomArray
1 parent a83058a commit bf40264

File tree

1 file changed

+21
-47
lines changed

1 file changed

+21
-47
lines changed

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

Lines changed: 21 additions & 47 deletions
Original file line numberDiff line numberDiff line change
@@ -1128,65 +1128,39 @@ public static Atom[] getRepresentativeAtomArray(Structure s) {
11281128
* @return an Atom[] array
11291129
*/
11301130
public static Atom[] getBackboneAtomArray(Structure s) {
1131-
11321131
List<Atom> atoms = new ArrayList<>();
1132+
Set<String> nucleotideAtomNames = 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));
1133+
Set<String> aminoAcidAtomNames = new HashSet<>(Arrays.asList(CA_ATOM_NAME, C_ATOM_NAME, N_ATOM_NAME, O_ATOM_NAME));
11331134

11341135
for (Chain c : s.getChains()) {
11351136
for (Group g : c.getAtomGroups()) {
11361137
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-
}
1138+
if (g.getType() == GroupType.NUCLEOTIDE) {
1139+
addNucleotideAndAminoAtoms(atoms, g, nucleotideAtomNames);
1140+
} else {
1141+
addNucleotideAndAminoAtoms(atoms, g, aminoAcidAtomNames);
11811142
}
11821143
}
11831144
}
1184-
11851145
}
1186-
11871146
return atoms.toArray(new Atom[0]);
11881147
}
11891148

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

0 commit comments

Comments
 (0)