Skip to content

Commit

Permalink
fixes for query atoms getMass() and getStereo on cloned atoms
Browse files Browse the repository at this point in the history
  • Loading branch information
dkatzel-ncats committed Oct 2, 2020
1 parent 47f7360 commit 3b99d97
Show file tree
Hide file tree
Showing 3 changed files with 239 additions and 43 deletions.
2 changes: 1 addition & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@
<dependency>
<groupId>gov.nih.ncats</groupId>
<artifactId>molwitch</artifactId>
<version>0.6.0</version>
<version>0.6.0-SNAPSHOT</version>
</dependency>
<dependency>
<groupId>gov.nih.ncats</groupId>
Expand Down
92 changes: 54 additions & 38 deletions src/main/java/gov/nih/ncats/molwitch/cdk/CdkChemicalImpl.java
Original file line number Diff line number Diff line change
Expand Up @@ -210,7 +210,9 @@ public class CdkChemicalImpl implements ChemicalImpl<CdkChemicalImpl>{
// we only really set it if it's tetrahedral CARBON
// right now,
boolean bailout=false;
for(IBond ib:ai.bonds()){
//we have to go through the container because some atom implementations
//throw unsupport operation exceptions if we ask them for their bonds
for(IBond ib:container.getConnectedBondsList(ai)){
if(!Order.SINGLE.equals(ib.getOrder())){
bailout=true;
break;
Expand Down Expand Up @@ -560,7 +562,7 @@ CdkBond getCdkBondFor(IBond bond){

List<CdkBond> getBondsFor(IAtom atom){
List<CdkBond> bonds = new ArrayList<>();
for(IBond bond : container.bonds()){
for(IBond bond : container.getConnectedBondsList(atom)){
if(bond.contains(atom)){
bonds.add(getCdkBondFor(bond));
}
Expand All @@ -582,48 +584,62 @@ public double getMass() {
if(recomputeHydrogens){
setImplicitHydrogens();
}
double d=AtomContainerManipulator.getMass(container, AtomContainerManipulator.MolWeight);

if(Double.isNaN(d) || (d<0.01 && this.getAtomCount()>0)){

double off=0;

Map<IAtom, Integer> realAtomicNums = new HashMap<IAtom,Integer>();


for(IAtom a : container.atoms()){
int an= a.getAtomicNumber();
if(cdkMissing.get(an)){
double d=Double.NaN;
try {
d = AtomContainerManipulator.getMass(container, AtomContainerManipulator.MolWeight);
}catch(Exception e){

}
if(Double.isNaN(d) || (d<0.01 && this.getAtomCount()>0)) {

double off = 0;

Map<IAtom, Integer> realAtomicNums = new HashMap<IAtom, Integer>();


for (IAtom a : container.atoms()) {
Integer an = a.getAtomicNumber();
if (an == null) {
a.setAtomicNumber(2);
realAtomicNums.put(a, an);
continue;
}
if (cdkMissing.get(an)) {
double m = NISTIsotopeFactory.INSTANCE.getMostAbundant(an)
.filter(mm->mm.getIsotopicComposition()!=null)
.map(aa->aa.getRelativeAtomicMass().getValue().doubleValue())
.orElseGet(()->{

if(mostStable[an]!=0)return (double)mostStable[an];
List<Isotope> ilist= NISTIsotopeFactory.INSTANCE.getIsotopesFor(an).stream()
.sorted(Comparator.comparing(an1->an1.getRelativeAtomicMass().getValue().doubleValue()))
.collect(Collectors.toList());;
if(ilist.isEmpty())return 0.0;
Isotope iso = ilist.get(ilist.size()/2);

return (double)Math.round(iso.getRelativeAtomicMass().getValue().doubleValue());
.filter(mm -> mm.getIsotopicComposition() != null)
.map(aa -> aa.getRelativeAtomicMass().getValue().doubleValue())
.orElseGet(() -> {

if (mostStable[an] != 0) return (double) mostStable[an];
List<Isotope> ilist = NISTIsotopeFactory.INSTANCE.getIsotopesFor(an).stream()
.sorted(Comparator.comparing(an1 -> an1.getRelativeAtomicMass().getValue().doubleValue()))
.collect(Collectors.toList());
;
if (ilist.isEmpty()) return 0.0;
Isotope iso = ilist.get(ilist.size() / 2);

return (double) Math.round(iso.getRelativeAtomicMass().getValue().doubleValue());
});
off+=m;

off += m;
a.setSymbol("R");
realAtomicNums.put(a, an);
}
}
double base=AtomContainerManipulator.getMass(container, AtomContainerManipulator.MolWeight);
realAtomicNums.forEach((a,i)->{
a.setAtomicNumber(i);
});

d=base+off;

//this means there's an isotope issue
//the fix, for now, is this ugly one

try {
double base = AtomContainerManipulator.getMass(container, AtomContainerManipulator.MolWeight);


d = base + off;

//this means there's an isotope issue
//the fix, for now, is this ugly one

} finally {
realAtomicNums.forEach((a, i) -> {
a.setAtomicNumber(i);
});
}
}
return d;
}
Expand Down
188 changes: 184 additions & 4 deletions src/test/java/TestParseQueryMol.java
Original file line number Diff line number Diff line change
Expand Up @@ -19,9 +19,6 @@
* Boston, MA 02111-1307 USA
*/

import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertTrue;
import static org.junit.Assert.assertFalse;
import java.io.IOException;
import java.util.ArrayList;
import java.util.BitSet;
Expand All @@ -41,6 +38,8 @@
import gov.nih.ncats.molwitch.inchi.InChiResult;
import gov.nih.ncats.molwitch.search.MolSearcherFactory;

import static org.junit.Assert.*;

public class TestParseQueryMol {

@Test
Expand Down Expand Up @@ -703,6 +702,187 @@ public void testQueryAtomMolfileHasWriteAtoms2() throws Exception {


}


@Test
public void legacyAtomList() throws Exception{
String mol = "\n" +
" ACCLDraw10012012192D\n" +
"\n" +
" 10 10 3 0 0 0 0 0 0 0999 V2000\n" +
" 12.8286 -7.3697 0.0000 C 0 0 0 0 0 0 0 0 0 0 0 0\n" +
" 12.0309 -7.3697 0.0000 C 0 0 0 0 0 0 0 0 0 0 0 0\n" +
" 13.2300 -8.0490 0.0000 C 0 0 0 0 0 0 0 0 0 0 0 0\n" +
" 11.5986 -6.6106 0.0000 L 0 0 0 0 0 0 0 0 0 0 0 0\n" +
" 11.5909 -8.0335 0.0000 L 0 0 0 0 0 0 0 0 0 0 0 0\n" +
" 12.8363 -8.7514 0.0000 L 0 0 0 0 0 0 0 0 0 0 0 0\n" +
" 13.2300 -6.6260 0.0000 C 0 0 0 0 0 0 0 0 0 0 0 0\n" +
" 14.0379 -8.0490 0.0000 C 0 0 0 0 0 0 0 0 0 0 0 0\n" +
" 14.0714 -6.6260 0.0000 C 0 0 0 0 0 0 0 0 0 0 0 0\n" +
" 14.4779 -7.3439 0.0000 C 0 0 0 0 0 0 0 0 0 0 0 0\n" +
" 2 1 1 0 0 0 0\n" +
" 3 1 2 0 0 0 0\n" +
" 4 2 2 0 0 0 0\n" +
" 5 2 1 0 0 0 0\n" +
" 6 3 1 0 0 0 0\n" +
" 7 1 1 0 0 0 0\n" +
" 8 3 1 0 0 0 0\n" +
" 9 7 2 0 0 0 0\n" +
" 10 9 1 0 0 0 0\n" +
" 10 8 2 0 0 0 0\n" +
" 4 F 2 8 7\n" +
" 5 F 2 7 8\n" +
" 6 F 2 7 8\n" +
"M END\n";

Chemical c = Chemical.parse(mol);
assertTrue(c.getAtom(5).isQueryAtom());
}

@Test
public void legacyAtomListGetStereoCentersDoentErrorOut() throws Exception{
String mol = "\n" +
" ACCLDraw10012012192D\n" +
"\n" +
" 10 10 3 0 0 0 0 0 0 0999 V2000\n" +
" 12.8286 -7.3697 0.0000 C 0 0 0 0 0 0 0 0 0 0 0 0\n" +
" 12.0309 -7.3697 0.0000 C 0 0 0 0 0 0 0 0 0 0 0 0\n" +
" 13.2300 -8.0490 0.0000 C 0 0 0 0 0 0 0 0 0 0 0 0\n" +
" 11.5986 -6.6106 0.0000 L 0 0 0 0 0 0 0 0 0 0 0 0\n" +
" 11.5909 -8.0335 0.0000 L 0 0 0 0 0 0 0 0 0 0 0 0\n" +
" 12.8363 -8.7514 0.0000 L 0 0 0 0 0 0 0 0 0 0 0 0\n" +
" 13.2300 -6.6260 0.0000 C 0 0 0 0 0 0 0 0 0 0 0 0\n" +
" 14.0379 -8.0490 0.0000 C 0 0 0 0 0 0 0 0 0 0 0 0\n" +
" 14.0714 -6.6260 0.0000 C 0 0 0 0 0 0 0 0 0 0 0 0\n" +
" 14.4779 -7.3439 0.0000 C 0 0 0 0 0 0 0 0 0 0 0 0\n" +
" 2 1 1 0 0 0 0\n" +
" 3 1 2 0 0 0 0\n" +
" 4 2 2 0 0 0 0\n" +
" 5 2 1 0 0 0 0\n" +
" 6 3 1 0 0 0 0\n" +
" 7 1 1 0 0 0 0\n" +
" 8 3 1 0 0 0 0\n" +
" 9 7 2 0 0 0 0\n" +
" 10 9 1 0 0 0 0\n" +
" 10 8 2 0 0 0 0\n" +
" 4 F 2 8 7\n" +
" 5 F 2 7 8\n" +
" 6 F 2 7 8\n" +
"M END\n";

Chemical c = Chemical.parse(mol);
assertEquals(0, c.getAllStereocenters().size());
}

@Test
public void modernAtomListGetStereoCentersDoentErrorOut() throws Exception{
String mol = "\n" +
" ACCLDraw10012012192D\n" +
"\n" +
" 10 10 3 0 0 0 0 0 0 0999 V2000\n" +
" 12.8286 -7.3697 0.0000 C 0 0 0 0 0 0 0 0 0 0 0 0\n" +
" 12.0309 -7.3697 0.0000 C 0 0 0 0 0 0 0 0 0 0 0 0\n" +
" 13.2300 -8.0490 0.0000 C 0 0 0 0 0 0 0 0 0 0 0 0\n" +
" 11.5986 -6.6106 0.0000 L 0 0 0 0 0 0 0 0 0 0 0 0\n" +
" 11.5909 -8.0335 0.0000 L 0 0 0 0 0 0 0 0 0 0 0 0\n" +
" 12.8363 -8.7514 0.0000 L 0 0 0 0 0 0 0 0 0 0 0 0\n" +
" 13.2300 -6.6260 0.0000 C 0 0 0 0 0 0 0 0 0 0 0 0\n" +
" 14.0379 -8.0490 0.0000 C 0 0 0 0 0 0 0 0 0 0 0 0\n" +
" 14.0714 -6.6260 0.0000 C 0 0 0 0 0 0 0 0 0 0 0 0\n" +
" 14.4779 -7.3439 0.0000 C 0 0 0 0 0 0 0 0 0 0 0 0\n" +
" 2 1 1 0 0 0 0\n" +
" 3 1 2 0 0 0 0\n" +
" 4 2 2 0 0 0 0\n" +
" 5 2 1 0 0 0 0\n" +
" 6 3 1 0 0 0 0\n" +
" 7 1 1 0 0 0 0\n" +
" 8 3 1 0 0 0 0\n" +
" 9 7 2 0 0 0 0\n" +
" 10 9 1 0 0 0 0\n" +
" 10 8 2 0 0 0 0\n" +
"M ALS 4 2 F O N \n" +
"M ALS 5 2 F N O \n" +
"M ALS 6 2 F N O \n" +
"M END\n";

Chemical c = Chemical.parse(mol);
assertEquals(0, c.getAllStereocenters().size());
}

@Test
public void mixOfModernAtomListGetStereoCentersDoentErrorOut() throws Exception{
String mol = "\n" +
" ACCLDraw10012012192D\n" +
"\n" +
" 10 10 3 0 0 0 0 0 0 0999 V2000\n" +
" 12.8286 -7.3697 0.0000 C 0 0 0 0 0 0 0 0 0 0 0 0\n" +
" 12.0309 -7.3697 0.0000 C 0 0 0 0 0 0 0 0 0 0 0 0\n" +
" 13.2300 -8.0490 0.0000 C 0 0 0 0 0 0 0 0 0 0 0 0\n" +
" 11.5986 -6.6106 0.0000 L 0 0 0 0 0 0 0 0 0 0 0 0\n" +
" 11.5909 -8.0335 0.0000 L 0 0 0 0 0 0 0 0 0 0 0 0\n" +
" 12.8363 -8.7514 0.0000 L 0 0 0 0 0 0 0 0 0 0 0 0\n" +
" 13.2300 -6.6260 0.0000 C 0 0 0 0 0 0 0 0 0 0 0 0\n" +
" 14.0379 -8.0490 0.0000 C 0 0 0 0 0 0 0 0 0 0 0 0\n" +
" 14.0714 -6.6260 0.0000 C 0 0 0 0 0 0 0 0 0 0 0 0\n" +
" 14.4779 -7.3439 0.0000 C 0 0 0 0 0 0 0 0 0 0 0 0\n" +
" 2 1 1 0 0 0 0\n" +
" 3 1 2 0 0 0 0\n" +
" 4 2 2 0 0 0 0\n" +
" 5 2 1 0 0 0 0\n" +
" 6 3 1 0 0 0 0\n" +
" 7 1 1 0 0 0 0\n" +
" 8 3 1 0 0 0 0\n" +
" 9 7 2 0 0 0 0\n" +
" 10 9 1 0 0 0 0\n" +
" 10 8 2 0 0 0 0\n" +
" 4 F 2 8 7\n" +
" 5 F 2 7 8\n" +
" 6 F 2 7 8\n" +
"M ALS 4 2 F O N \n" +
"M ALS 5 2 F N O \n" +
"M ALS 6 2 F N O \n" +
"M END\n";

Chemical c = Chemical.parse(mol);
assertEquals(0, c.getAllStereocenters().size());
}

@Test
public void clonedMixOfModernAtomListGetStereoCentersDoentErrorOut() throws Exception{
String mol = "\n" +
" ACCLDraw10012012192D\n" +
"\n" +
" 10 10 3 0 0 0 0 0 0 0999 V2000\n" +
" 12.8286 -7.3697 0.0000 C 0 0 0 0 0 0 0 0 0 0 0 0\n" +
" 12.0309 -7.3697 0.0000 C 0 0 0 0 0 0 0 0 0 0 0 0\n" +
" 13.2300 -8.0490 0.0000 C 0 0 0 0 0 0 0 0 0 0 0 0\n" +
" 11.5986 -6.6106 0.0000 L 0 0 0 0 0 0 0 0 0 0 0 0\n" +
" 11.5909 -8.0335 0.0000 L 0 0 0 0 0 0 0 0 0 0 0 0\n" +
" 12.8363 -8.7514 0.0000 L 0 0 0 0 0 0 0 0 0 0 0 0\n" +
" 13.2300 -6.6260 0.0000 C 0 0 0 0 0 0 0 0 0 0 0 0\n" +
" 14.0379 -8.0490 0.0000 C 0 0 0 0 0 0 0 0 0 0 0 0\n" +
" 14.0714 -6.6260 0.0000 C 0 0 0 0 0 0 0 0 0 0 0 0\n" +
" 14.4779 -7.3439 0.0000 C 0 0 0 0 0 0 0 0 0 0 0 0\n" +
" 2 1 1 0 0 0 0\n" +
" 3 1 2 0 0 0 0\n" +
" 4 2 2 0 0 0 0\n" +
" 5 2 1 0 0 0 0\n" +
" 6 3 1 0 0 0 0\n" +
" 7 1 1 0 0 0 0\n" +
" 8 3 1 0 0 0 0\n" +
" 9 7 2 0 0 0 0\n" +
" 10 9 1 0 0 0 0\n" +
" 10 8 2 0 0 0 0\n" +
" 4 F 2 8 7\n" +
" 5 F 2 7 8\n" +
" 6 F 2 7 8\n" +
"M ALS 4 2 F O N \n" +
"M ALS 5 2 F N O \n" +
"M ALS 6 2 F N O \n" +
"M END\n";

Chemical c = Chemical.parse(mol).copy();
assertEquals(0, c.getAllStereocenters().size());
assertNotNull(c.getMass());
}

}

0 comments on commit 3b99d97

Please sign in to comment.