All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.actelion.research.chem.chemicalspaces.ptree.PharmTreeSynthonReactionHelper Maven / Gradle / Ivy

There is a newer version: 2024.11.2
Show newest version
package com.actelion.research.chem.chemicalspaces.ptree;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.stream.Collectors;

import com.actelion.research.chem.Molecule;
import com.actelion.research.chem.RingCollection;
import com.actelion.research.chem.SmilesCreator;
import com.actelion.research.chem.StereoMolecule;
import com.actelion.research.chem.chemicalspaces.synthon.SynthonReactor;

/**
 * reactor class that can handle reactions that are defined by synthons
 * @author joel
 *
 */


public class PharmTreeSynthonReactionHelper  {
	
	private List synthons;
	private Map> ringWithLinks;
	private Map> ringReactantIndeces;
	private Map> reactantsWithLinkers;
	private StereoMolecule genericProduct;
	private List synthonsToGenericProductMap;
	private List deletionMap;
	private List> productRings;
	private int[] bonds; // bond orders formed by linkers
	
	/**
	 * a reaction can be fully characterized by a list of synthons defining the bonds that are formed during the reaction
	 * special attention is given to ring systems that are formed ruing the reactions
	 * @param synthons
	 */
	public PharmTreeSynthonReactionHelper(List synthons) {
		this.synthons = synthons;
		ringWithLinks = new HashMap>();
		ringReactantIndeces = new HashMap>();
		bonds = new int[4];
		classifyReaction();
		
	}
	
	
	private void classifyReaction() {
		reactantsWithLinkers = new HashMap>();
		for(StereoMolecule reactant : synthons) {

			for(int a=0;a());
			StereoMolecule reactant = synthons.get(r);
			for(int a=0;a bondLabels = new HashMap();
		synthonsToGenericProductMap = new ArrayList();
		deletionMap = new ArrayList();
		genericProduct = synthonReaction(synthons, synthonsToGenericProductMap, deletionMap,bondLabels);

		/*
		 * to do: get formed rings as StereoMolecules, so that they later can be represented as separate building block
		 * or try first: merge all functionalities of the rings into one node attached to one BB 
		 */
				
		
		RingCollection rc = genericProduct.getRingSet();
		productRings = new ArrayList>();
		for(int r=0;r>();
		ringReactantIndeces = new HashMap>(); //information on which formed ring contains which reactants  
		for(int r=0;r=SynthonReactor.CONNECTOR_OFFSET)
					continue;
				for(int fr=0;fr());
						ringReactantIndeces.get(fr).add(r);
					}
				}

					
			}
		}

		for(int r=0;r());
					ringWithLinks.get(r).add(bondLabels.get(b));
				}
			}
		}
		
	}
	
	/**
	 * forms the product from the given building blocks and the generic reactants that fully
	 * define the reaction, also calculates that mapping from the reactant indeces to the product indeces 
	 * the bond that is formed by connecting the two atoms adjacent to R1 is labeled 1, 2 for R2, etc.
	 * @param buildingBlocks
	 * @param genericReactants
	 * @param atomMap
	 * @return
	 */
	private  StereoMolecule synthonReaction(List bbs,  List atomMap, List deletionMap,
			Map bondLabels) {
		List buildingBlocks = new ArrayList();
		bbs.stream().forEach(e -> buildingBlocks.add(new StereoMolecule(e)));
		buildingBlocks.forEach(e -> e.ensureHelperArrays(Molecule.cHelperCIP));
		HashMap> rgrps = new HashMap>();
		for(int m=0;m());
					rgrps.get(1).add(new int[]{m,bb.getConnAtom(a, 0)});
					bb.markAtomForDeletion(a);
				}
				else if(bb.getAtomLabel(a).equals("Np")) {
					rgrps.putIfAbsent(2, new ArrayList());
					rgrps.get(2).add(new int[]{m,bb.getConnAtom(a, 0)});
					bb.markAtomForDeletion(a);
				}
				else if(bb.getAtomLabel(a).equals("Pu")) {
					rgrps.putIfAbsent(3, new ArrayList());
					rgrps.get(3).add(new int[]{m,bb.getConnAtom(a, 0)});
					bb.markAtomForDeletion(a);
				}
				else if(bb.getAtomLabel(a).equals("Am")) {
					rgrps.putIfAbsent(4, new ArrayList());
					rgrps.get(4).add(new int[]{m,bb.getConnAtom(a, 0)});
					bb.markAtomForDeletion(a);
				}
				
			}
		}
		// remove R groups
		for(int m=0;m {
				v.stream().forEach(e -> {
					if(e[0]==m_)
						e[1] = map[e[1]];
				});
				
			});
			
		}

		StereoMolecule product = new StereoMolecule();
		atomMap.add(product.addMolecule(buildingBlocks.get(0)));
		for(int m=1;m {
				v.stream().forEach(e -> {
					if(e[0]==m_)
						e[1] = map[e[1]];
				});
				
			});
		
		}
		rgrps.forEach((k,v) -> {
			int atom1 = v.get(0)[1];
			int atom2 = v.get(1)[1];
			int bond = product.addBond(atom1, atom2);
			product.setBondOrder(bond, bonds[k-1]);
			bondLabels.put(bond, k);
		});
		int[] map = product.getHandleHydrogenMap();

		atomMap.stream().forEach(e -> {
			Arrays.stream(e).map(i -> map[i]);});

		product.ensureHelperArrays(Molecule.cHelperCIP);
		return product;
		
	}
	



	public List getSynthons() {
		return synthons;
	}


	public Map> getRingWithLinks() {
		return ringWithLinks;
	}


	public Map> getRingReactantIndeces() {
		return ringReactantIndeces;
	}


	public StereoMolecule getGenericProduct() {
		return genericProduct;
	}


	public List getSynthonsToGenericProductMap() {
		return synthonsToGenericProductMap;
	}
	
	public int mapSynthonToGenericProductIndex(int synthonID, int index) {
		int newIndex = deletionMap.get(synthonID)[index];
		return synthonsToGenericProductMap.get(synthonID)[newIndex];
	}


	public List> getFormedRings() {
		return productRings;
	}


	public Map> getReactantsWithLinkers() {
		return reactantsWithLinkers;
	}
	
	public int[] getBondOrders() {
		return bonds;
	}
	

}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy