Skip to content
This repository was archived by the owner on Jul 10, 2024. It is now read-only.

Commit 455f04b

Browse files
committed
fix with some tests for issue #4, #3, #7, needs evaluation
1 parent b8a828c commit 455f04b

File tree

2 files changed

+341
-7
lines changed

2 files changed

+341
-7
lines changed

src/main/java/lychi/LyChIStandardizer.java

+123-1
Original file line numberDiff line numberDiff line change
@@ -1166,7 +1166,129 @@ else if (chiral != 0) {
11661166
int i = m.indexOf(me.getKey());
11671167
m.setChirality(i, me.getValue());
11681168
}
1169-
1169+
try{
1170+
1171+
Map<MolAtom,MolBond> nonChiralStereo = new LinkedHashMap<>();
1172+
1173+
for(int k=0;k<m.getBondCount();k++){
1174+
MolBond b = m.getBond(k);
1175+
int parity = b.getFlags() & MolBond.STEREO1_MASK;
1176+
MolAtom ma1=b.getAtom1();
1177+
if(chirality.get(ma1)==null){
1178+
if(parity!=0){
1179+
//some other parity assigned here
1180+
// if(parity==MolBond.UP)System.out.println("UP");
1181+
// if(parity==MolBond.DOWN)System.out.println("DOWN");
1182+
nonChiralStereo.put(ma1, b);
1183+
}
1184+
}
1185+
}
1186+
1187+
if(!nonChiralStereo.isEmpty()){
1188+
String igprop=m.getProperty("IGNORE_COMPLEX");
1189+
1190+
if(!"true".equals(igprop)){
1191+
m.setProperty("IGNORE_COMPLEX", "true");
1192+
1193+
Set<int[]> rings = new HashSet<int[]>();
1194+
1195+
int[][] sssr=m.getSSSR();
1196+
for(MolAtom ma:nonChiralStereo.keySet()){
1197+
//need to find all atoms in the ring
1198+
int im=m.indexOf(ma);
1199+
for(int[] ir:sssr){
1200+
for(int i=0;i<ir.length;i++){
1201+
if(ir[i]==im){
1202+
rings.add(ir);
1203+
}
1204+
}
1205+
1206+
}
1207+
}
1208+
1209+
for(int[] rr:rings){
1210+
Set<MolAtom> ratoms=Arrays.stream(rr)
1211+
.mapToObj(i->m.getAtom(i))
1212+
.collect(Collectors.toSet());
1213+
1214+
MolBond[] bonds=ratoms.stream()
1215+
.filter(a->!chirality.containsKey(a))
1216+
.flatMap(a->IntStream.range(0, a.getEdgeCount()).mapToObj(i->a.getEdge(i)))
1217+
.filter(e->!ratoms.contains(e.getNode1()) || !ratoms.contains(e.getNode2()))
1218+
.map(b->(MolBond)b)
1219+
.filter(b->b.getType()==1)
1220+
.peek(b->{
1221+
if(ratoms.contains(b.getAtom1()))b.swap();
1222+
})
1223+
.toArray(i->new MolBond[i]);
1224+
1225+
BitSet bs = new BitSet(bonds.length*2);
1226+
for(int i=0;i<bonds.length;i++){
1227+
MolBond b=bonds[i];
1228+
int parity = b.getFlags() & MolBond.STEREO1_MASK;
1229+
if(parity==MolBond.UP){
1230+
bs.set(i*2);
1231+
}else if(parity==MolBond.DOWN){
1232+
bs.set(i*2+1);
1233+
}else{
1234+
bs.set(i*2);
1235+
bs.set(i*2+1);
1236+
}
1237+
}
1238+
1239+
Set<String> allPossible = new HashSet<String>();
1240+
Set<String> currentPossible = new HashSet<String>();
1241+
1242+
for(int i=0;i<Math.pow(2, bonds.length);i++){
1243+
BitSet onOff = new BitSet(bonds.length*2);
1244+
for(int j=0;j<bonds.length;j++){
1245+
if((i>>j&1)==1){
1246+
onOff.set(j*2);
1247+
bonds[j].setFlags(MolBond.UP, MolBond.STEREO1_MASK);
1248+
}else{
1249+
onOff.set(j*2+1);
1250+
bonds[j].setFlags(MolBond.DOWN, MolBond.STEREO1_MASK);
1251+
}
1252+
}
1253+
Molecule mclone=m.cloneMolecule();
1254+
//(new LyChIStandardizer()).standardize(mclone);
1255+
String hash1=LyChIStandardizer.hashKey(mclone);
1256+
allPossible.add(hash1);
1257+
onOff.or(bs);
1258+
1259+
if(onOff.cardinality() == bs.cardinality()){
1260+
currentPossible.add(hash1);
1261+
}
1262+
}
1263+
if(allPossible.size()==currentPossible.size()){
1264+
for(int j=0;j<bonds.length;j++){
1265+
bonds[j].setFlags(0, MolBond.STEREO1_MASK);
1266+
}
1267+
}else{
1268+
for(int j=0;j<bonds.length;j++){
1269+
boolean isUp=bs.get(j*2);
1270+
boolean isDown=bs.get(j*2+1);
1271+
1272+
if(isUp && ! isDown){
1273+
bonds[j].setFlags(MolBond.UP, MolBond.STEREO1_MASK);
1274+
}else if(!isUp && isDown){
1275+
bonds[j].setFlags(MolBond.DOWN, MolBond.STEREO1_MASK);
1276+
}else{
1277+
bonds[j].setFlags(0, MolBond.STEREO1_MASK);
1278+
}
1279+
}
1280+
}
1281+
}
1282+
1283+
1284+
1285+
m.setProperty("IGNORE_COMPLEX", null);
1286+
}
1287+
}
1288+
1289+
}catch(Exception e){
1290+
1291+
}
11701292

11711293

11721294
for (Map.Entry<MolAtom, Integer> me : chirality.entrySet()) {

0 commit comments

Comments
 (0)