Skip to content

Commit

Permalink
[hyperspace-core] some additions to reparametrization code
Browse files Browse the repository at this point in the history
  • Loading branch information
lithom committed Feb 24, 2024
1 parent 06b7992 commit 26dc375
Show file tree
Hide file tree
Showing 3 changed files with 170 additions and 7 deletions.
Original file line number Diff line number Diff line change
@@ -1,14 +1,21 @@
package com.idorsia.research.chem.hyperspace.reparametrization;

import com.actelion.research.chem.IDCodeParser;
import com.actelion.research.chem.Molecule;
import com.actelion.research.chem.StereoMolecule;
import com.idorsia.research.chem.hyperspace.CachedDescriptorProvider;
import com.idorsia.research.chem.hyperspace.FastSubstructureSearcher;
import com.idorsia.research.chem.hyperspace.SynthonSpace;
import com.actelion.research.chem.chemicalspaces.synthon.SynthonReactor;
import com.idorsia.research.chem.hyperspace.*;
import org.apache.commons.io.input.CountingInputStream;
import org.apache.commons.lang3.tuple.Pair;

import java.io.BufferedInputStream;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.util.*;
import java.util.concurrent.ConcurrentHashMap;
import java.util.stream.Collectors;
import java.util.zip.GZIPInputStream;

public class RealizedRxnReparametrization {

Expand All @@ -19,19 +26,99 @@ public class RealizedRxnReparametrization {

private Map<Integer, RealizedSynthonSetReparametrization.SynthonSetAnalysis> analysis;

public static class RxnScaffold {

}

public RealizedRxnReparametrization(RxnReparametrization reparametrization, SynthonSpace space, String rxn_id) {
this.reparametrization = reparametrization;
this.space = space;
this.rxn_id = rxn_id;
}

public static interface ScaffoldEnumerationCriterion {
public boolean considerScaffold(List<Pair<String,List<SynthonSpace.FragId>>> scaffoldParts);
}

public List<SynthonSpaceScaffold> enumerateCombinatorialScaffolds(ScaffoldEnumerationCriterion scaffoldEnumerationCriterion) {
List<SynthonSpaceScaffold> scaffolds = new ArrayList<>();
List<List<Pair<String,List<SynthonSpace.FragId>>>> scaffoldParts = new ArrayList<>();
for( RealizedSynthonSetReparametrization.SynthonSetAnalysis ai : analysis.values()) {
scaffoldParts.add( ai.pfrags_sorted_filtered.stream().map( xi -> Pair.of(xi.getKey(),xi.getValue())).collect(Collectors.toList()) );
}
List<List<Pair<String,List<SynthonSpace.FragId>>>> tuples = generateTuples( scaffoldParts );

for( List<Pair<String,List<SynthonSpace.FragId>>> scaffold_i : tuples ) {
boolean consider = true;
if( scaffoldEnumerationCriterion!=null ) {
consider = scaffoldEnumerationCriterion.considerScaffold(scaffold_i);
}
if(!consider) {
System.out.println("Skip scaffold..");
continue;
}
else {
System.out.println("Construct scaffold.. size="+scaffold_i.stream().mapToLong(xi -> xi.getRight().size()).reduce((x,y)->x*y)+" , "+scaffold_i.stream().map(xi -> ""+xi.getRight().size()).reduce((x,y)->x+","+y));
List<StereoMolecule> scaffoldParts_i = scaffold_i.stream().map( xi -> HyperspaceUtils.parseIDCode(xi.getKey())).collect(Collectors.toList());
StereoMolecule reactedScaffold = SynthonReactor.react(scaffoldParts_i);
reactedScaffold.ensureHelperArrays(Molecule.cHelperCIP);
if( reactedScaffold.getFragments().length > 1 ) {
System.out.println("[WARN] assembly incomplete..");
}
String rxn_i = scaffold_i.get(0).getRight().get(0).rxn_id;
if(scaffold_i.size()==2) {
SynthonSpaceScaffold scaffoldData_i = new SynthonSpaceScaffold(rxn_i, reactedScaffold.getIDCode(),
scaffoldParts_i.get(0).getIDCode(),scaffoldParts_i.get(1).getIDCode(),"",
scaffold_i.get(0).getRight().stream().map(xi -> xi.idcode).collect(Collectors.toList()),
scaffold_i.get(1).getRight().stream().map(xi -> xi.idcode).collect(Collectors.toList()),
new ArrayList<>());
scaffolds.add(scaffoldData_i);
}
else if (scaffold_i.size()==3) {
SynthonSpaceScaffold scaffoldData_i = new SynthonSpaceScaffold(rxn_i, reactedScaffold.getIDCode(),
scaffoldParts_i.get(0).getIDCode(),scaffoldParts_i.get(1).getIDCode(),scaffoldParts_i.get(2).getIDCode(),
scaffold_i.get(0).getRight().stream().map(xi -> xi.idcode).collect(Collectors.toList()),
scaffold_i.get(1).getRight().stream().map(xi -> xi.idcode).collect(Collectors.toList()),
scaffold_i.get(2).getRight().stream().map(xi -> xi.idcode).collect(Collectors.toList()));
scaffolds.add(scaffoldData_i);
}
}
}

return scaffolds;
}

private static <T> List<List<T>> generateTuples(List<List<T>> lists) {
List<List<T>> result = new ArrayList<>();
generateTuplesHelper(lists, 0, new ArrayList<>(), result);
return result;
}

private static <T> void generateTuplesHelper(List<List<T>> lists, int currentIndex, List<T> currentTuple, List<List<T>> result) {
if (currentIndex == lists.size()) {
// Base case: Add the current tuple to the result
result.add(new ArrayList<>(currentTuple));
return;
}

List<T> currentList = lists.get(currentIndex);

for (int i = 0; i < currentList.size(); i++) {
currentTuple.add(currentList.get(i));
generateTuplesHelper(lists, currentIndex + 1, currentTuple, result);
currentTuple.remove(currentTuple.size() - 1); // Backtrack
}
}

/**
* first stage of analysis, applies the synthon set
* first stage of analysis, applies the synthon set reparametrization and sorts
* synthons accordingly.
*
* @param threads
* @throws InterruptedException
*/
public void processSynthonSets(int threads) throws InterruptedException {
this.analysis = new HashMap<>();

FastSubstructureSearcher fss = null;
CachedDescriptorProvider cdh = new CachedDescriptorProvider("FragFp");
Expand Down Expand Up @@ -60,7 +147,7 @@ public void processSynthonSets(int threads) throws InterruptedException {

List<SynthonSpace.FragId> frags = space.getSynthonSet(rxn,fzi);
num_frags.add(frags.size());
RealizedSynthonSetReparametrization sspi = new RealizedSynthonSetReparametrization(new StandardSynthonSetReparametrization(),frags);
RealizedSynthonSetReparametrization sspi = new RealizedSynthonSetReparametrization( reparametrization.getSynthonSetReparametrizatino(fzi) , frags );//new RealizedSynthonSetReparametrization(new StandardSynthonSetReparametrization(),frags);
sspi.computeAnalysis(processed_synthon_cache_a,threads);
RealizedSynthonSetReparametrization.SynthonSetAnalysis analysis = sspi.getAnalysisResult();
//SynthonSetAnalysis analysis_b = process_synthon_set(processed_synthon_cache_b,frags,4,false);
Expand All @@ -87,7 +174,52 @@ public void processSynthonSets(int threads) throws InterruptedException {


public static void main(String args[]) {

//String inputfile = "v2_s2_32k_FragFp.data";
String inputfile = "s2_small_01_FragFp.data";

ObjectInputStream in = null;
SynthonSpace space = null;
try {
FileInputStream file_in = new FileInputStream(inputfile);
//counting_in_stream = new CountingInputStream(new BufferedInputStream(file_in));
//counting_in_stream = new CountingInputStream( new GZIPInputStream( new BufferedInputStream(file_in)));
CountingInputStream counting_in_stream = new CountingInputStream( new BufferedInputStream(file_in));
in = new ObjectInputStream( new GZIPInputStream( counting_in_stream ) );
SynthonSpace space_a = null;
space_a = (SynthonSpace) in.readObject();
space_a.initAfterJavaDeserialization();
space = space_a;
} catch (IOException e) {
e.printStackTrace();
} catch (ClassNotFoundException e) {
e.printStackTrace();
}
System.out.println("Loaded space: "+space.getSpaceInfoString());
List<SynthonSpaceScaffold> scaffolds = new ArrayList<>();

for(String rxn_id : space.getRxnIds()) {
SynthonSetReparametrization synthonSetReparametrization = new StandardSynthonSetReparametrization(true, 3);
RxnReparametrization standardReparametrization = new RxnReparametrization(space, rxn_id, synthonSetReparametrization);
RealizedRxnReparametrization reparametrization = new RealizedRxnReparametrization(standardReparametrization, space, rxn_id);
try {
reparametrization.processSynthonSets(12);
List<SynthonSpaceScaffold> scaffolds_i = reparametrization.enumerateCombinatorialScaffolds(new ScaffoldEnumerationCriterion() {
@Override
public boolean considerScaffold(List<Pair<String, List<SynthonSpace.FragId>>> scaffoldParts) {
return scaffoldParts.stream().filter(xi -> xi.getRight().size() > 8).collect(Collectors.toList()).size() >= 2;
//return true;
}
});
scaffolds.addAll(scaffolds_i);
System.out.println("mkay");
} catch (InterruptedException e) {
throw new RuntimeException(e);
}
}
System.out.println("Analysis: total scaffolds returned: "+scaffolds.size());
for(SynthonSpaceScaffold sci : scaffolds) {
System.out.println(sci.toCSV_short());
}
}

}
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
package com.idorsia.research.chem.hyperspace.reparametrization;

import com.idorsia.research.chem.hyperspace.SynthonSpace;

import java.util.HashMap;
import java.util.Map;

/**
Expand All @@ -8,5 +11,24 @@
*
*/
public class RxnReparametrization {
private Map<Integer,SynthonSetReparametrization> synthonReparametrization;
private Map<Integer,SynthonSetReparametrization> synthonReparametrization = new HashMap<>();

public SynthonSetReparametrization getSynthonSetReparametrizatino(int synthonSet) {
return synthonReparametrization.get(synthonSet);
}

/**
* Uses the sam synthon set reparametrization for all synthon sets
*
* @param space
* @param rxn_id
* @param reparametrizatino
*/
public RxnReparametrization(SynthonSpace space, String rxn_id, SynthonSetReparametrization reparametrizatino) {
synthonReparametrization = new HashMap<>();
for(Map.Entry<Integer, SynthonSpace.FragType> e1 : space.getFragTypes(rxn_id).entrySet()) {
synthonReparametrization.put(e1.getKey(),reparametrizatino);
}
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,15 @@ public class StandardSynthonSetReparametrization implements SynthonSetReparametr
private boolean completeRingSystem = true;
private int distance = 4;

public StandardSynthonSetReparametrization() {

}

public StandardSynthonSetReparametrization(boolean completeRingSystem, int distance) {
this.completeRingSystem = completeRingSystem;
this.distance = distance;
}

@Override
public StereoMolecule processSynthon(StereoMolecule mi) {
StereoMolecule mi2 = new StereoMolecule(mi);
Expand Down

0 comments on commit 26dc375

Please sign in to comment.