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

Commit

Permalink
fix with some tests for issue #4, #3, #7, needs evaluation
Browse files Browse the repository at this point in the history
  • Loading branch information
tylerperyea committed Feb 12, 2019
1 parent b8a828c commit 455f04b
Show file tree
Hide file tree
Showing 2 changed files with 341 additions and 7 deletions.
124 changes: 123 additions & 1 deletion src/main/java/lychi/LyChIStandardizer.java
Original file line number Diff line number Diff line change
Expand Up @@ -1166,7 +1166,129 @@ else if (chiral != 0) {
int i = m.indexOf(me.getKey());
m.setChirality(i, me.getValue());
}

try{

Map<MolAtom,MolBond> nonChiralStereo = new LinkedHashMap<>();

for(int k=0;k<m.getBondCount();k++){
MolBond b = m.getBond(k);
int parity = b.getFlags() & MolBond.STEREO1_MASK;
MolAtom ma1=b.getAtom1();
if(chirality.get(ma1)==null){
if(parity!=0){
//some other parity assigned here
// if(parity==MolBond.UP)System.out.println("UP");
// if(parity==MolBond.DOWN)System.out.println("DOWN");
nonChiralStereo.put(ma1, b);
}
}
}

if(!nonChiralStereo.isEmpty()){
String igprop=m.getProperty("IGNORE_COMPLEX");

if(!"true".equals(igprop)){
m.setProperty("IGNORE_COMPLEX", "true");

Set<int[]> rings = new HashSet<int[]>();

int[][] sssr=m.getSSSR();
for(MolAtom ma:nonChiralStereo.keySet()){
//need to find all atoms in the ring
int im=m.indexOf(ma);
for(int[] ir:sssr){
for(int i=0;i<ir.length;i++){
if(ir[i]==im){
rings.add(ir);
}
}

}
}

for(int[] rr:rings){
Set<MolAtom> ratoms=Arrays.stream(rr)
.mapToObj(i->m.getAtom(i))
.collect(Collectors.toSet());

MolBond[] bonds=ratoms.stream()
.filter(a->!chirality.containsKey(a))
.flatMap(a->IntStream.range(0, a.getEdgeCount()).mapToObj(i->a.getEdge(i)))
.filter(e->!ratoms.contains(e.getNode1()) || !ratoms.contains(e.getNode2()))
.map(b->(MolBond)b)
.filter(b->b.getType()==1)
.peek(b->{
if(ratoms.contains(b.getAtom1()))b.swap();
})
.toArray(i->new MolBond[i]);

BitSet bs = new BitSet(bonds.length*2);
for(int i=0;i<bonds.length;i++){
MolBond b=bonds[i];
int parity = b.getFlags() & MolBond.STEREO1_MASK;
if(parity==MolBond.UP){
bs.set(i*2);
}else if(parity==MolBond.DOWN){
bs.set(i*2+1);
}else{
bs.set(i*2);
bs.set(i*2+1);
}
}

Set<String> allPossible = new HashSet<String>();
Set<String> currentPossible = new HashSet<String>();

for(int i=0;i<Math.pow(2, bonds.length);i++){
BitSet onOff = new BitSet(bonds.length*2);
for(int j=0;j<bonds.length;j++){
if((i>>j&1)==1){
onOff.set(j*2);
bonds[j].setFlags(MolBond.UP, MolBond.STEREO1_MASK);
}else{
onOff.set(j*2+1);
bonds[j].setFlags(MolBond.DOWN, MolBond.STEREO1_MASK);
}
}
Molecule mclone=m.cloneMolecule();
//(new LyChIStandardizer()).standardize(mclone);
String hash1=LyChIStandardizer.hashKey(mclone);
allPossible.add(hash1);
onOff.or(bs);

if(onOff.cardinality() == bs.cardinality()){
currentPossible.add(hash1);
}
}
if(allPossible.size()==currentPossible.size()){
for(int j=0;j<bonds.length;j++){
bonds[j].setFlags(0, MolBond.STEREO1_MASK);
}
}else{
for(int j=0;j<bonds.length;j++){
boolean isUp=bs.get(j*2);
boolean isDown=bs.get(j*2+1);

if(isUp && ! isDown){
bonds[j].setFlags(MolBond.UP, MolBond.STEREO1_MASK);
}else if(!isUp && isDown){
bonds[j].setFlags(MolBond.DOWN, MolBond.STEREO1_MASK);
}else{
bonds[j].setFlags(0, MolBond.STEREO1_MASK);
}
}
}
}



m.setProperty("IGNORE_COMPLEX", null);
}
}

}catch(Exception e){

}


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

0 comments on commit 455f04b

Please sign in to comment.