Skip to content

Commit 16945e7

Browse files
authored
Merge pull request biojava#623 from andreasprlic/fix622
biojava#622 removal of a few loops.
2 parents a743727 + 498fe51 commit 16945e7

File tree

3 files changed

+73
-7
lines changed

3 files changed

+73
-7
lines changed

biojava-structure/src/main/java/org/biojava/nbio/structure/io/mmcif/ChemicalComponentDictionary.java

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -127,6 +127,16 @@ public void addChemComp(ChemComp comp){
127127
}
128128
}
129129

130+
/** Returns the number of ChemComps in this dictionary
131+
*
132+
* @return nr. of ChemComps
133+
*/
134+
public int size(){
135+
136+
return dictionary.size();
137+
138+
}
139+
130140
public ChemComp getChemComp(String id){
131141
return dictionary.get(id);
132142
}

biojava-structure/src/main/java/org/biojava/nbio/structure/io/mmcif/chem/PolymerType.java

Lines changed: 27 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -22,10 +22,7 @@
2222
package org.biojava.nbio.structure.io.mmcif.chem;
2323

2424
import java.io.Serializable;
25-
import java.util.Arrays;
26-
import java.util.Collections;
27-
import java.util.HashSet;
28-
import java.util.Set;
25+
import java.util.*;
2926

3027
/**
3128
* Enumerates the classification of polymers.
@@ -38,7 +35,6 @@
3835
public enum PolymerType implements Serializable
3936
{
4037

41-
4238
/**
4339
* polypeptide(L)
4440
*/
@@ -94,6 +90,19 @@ public enum PolymerType implements Serializable
9490
*/
9591
unknown(null);
9692

93+
static Map<String,PolymerType> lookupTable = new HashMap<>();
94+
95+
static {
96+
97+
for (PolymerType rt : PolymerType.values() ) {
98+
if ( rt == unknown)
99+
continue;
100+
lookupTable.put(rt.entity_poly_type,rt);
101+
lookupTable.put(rt.entity_poly_type.toLowerCase(),rt);
102+
}
103+
}
104+
105+
97106
PolymerType(String entity_poly_type)
98107
{
99108
this.entity_poly_type = entity_poly_type;
@@ -102,6 +111,19 @@ public enum PolymerType implements Serializable
102111

103112
public static PolymerType polymerTypeFromString(String polymerType)
104113
{
114+
115+
if ( polymerType.equalsIgnoreCase(peptide.entity_poly_type))
116+
return peptide;
117+
118+
PolymerType ptype = lookupTable.get(polymerType);
119+
if ( ptype != null)
120+
return ptype;
121+
122+
ptype = lookupTable.get(polymerType.toLowerCase());
123+
if ( ptype != null)
124+
return ptype;
125+
126+
105127
for(PolymerType pt : PolymerType.values())
106128
{
107129
if(polymerType.equals(pt.entity_poly_type))

biojava-structure/src/main/java/org/biojava/nbio/structure/io/mmcif/chem/ResidueType.java

Lines changed: 36 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,8 @@
2222
package org.biojava.nbio.structure.io.mmcif.chem;
2323

2424
import java.io.Serializable;
25+
import java.util.HashMap;
26+
import java.util.Map;
2527

2628

2729
/**
@@ -72,10 +74,21 @@ public enum ResidueType implements Serializable {
7274
nonPolymer(null, "non-polymer"),
7375
otherChemComp(null, "other");
7476

77+
78+
static Map<String,ResidueType> lookupTable = new HashMap<>();
79+
80+
static {
81+
82+
for (ResidueType rt : ResidueType.values() ) {
83+
lookupTable.put(rt.chem_comp_type,rt);
84+
lookupTable.put(rt.chem_comp_type.toLowerCase(),rt);
85+
}
86+
}
7587
ResidueType(PolymerType pt, String chem_comp_type)
7688
{
7789
this.polymerType = pt;
7890
this.chem_comp_type = chem_comp_type;
91+
7992
}
8093

8194
/**
@@ -94,12 +107,33 @@ public enum ResidueType implements Serializable {
94107
*/
95108
public final String chem_comp_type;
96109

110+
/** Get ResidueType by chem_comp_type
111+
*
112+
* @param chem_comp_type e.g. L-peptide linking
113+
* @return
114+
*/
97115
public static ResidueType getResidueTypeFromString(String chem_comp_type)
98116
{
99117

100-
chem_comp_type = chem_comp_type.replaceAll("'", "");
101-
chem_comp_type = chem_comp_type.replaceAll("\"", "");
118+
// Almost all calls to this method are for L-peptide linking. Use this knowledge for a shortcut.
119+
120+
if ( chem_comp_type.equalsIgnoreCase(lPeptideLinking.chem_comp_type) )
121+
return lPeptideLinking;
122+
123+
ResidueType rtype = lookupTable.get(chem_comp_type);
124+
if ( rtype != null)
125+
return rtype;
126+
127+
/** Unfortunately it can be guaranteed that chem_comp_type case sensitivity is preserved.
128+
* E.g. mmtf has it all upper-case. As such we need to do a second check
129+
*/
130+
rtype = lookupTable.get(chem_comp_type.toLowerCase());
131+
if ( rtype != null)
132+
return rtype;
133+
134+
102135

136+
// preserving previous behaviour. Not sure if this is really necessary?
103137
for(ResidueType rt : ResidueType.values())
104138
{
105139
if(rt.chem_comp_type.equalsIgnoreCase(chem_comp_type))

0 commit comments

Comments
 (0)