tech.molecules.leet.chem.ChemUtils Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of leet-chem-core Show documentation
Show all versions of leet-chem-core Show documentation
Cheminformatics and computational chemistry algorithms and workflows.
package tech.molecules.leet.chem;
import com.actelion.research.chem.*;
import com.actelion.research.chem.coords.CoordinateInventor;
import com.actelion.research.gui.JStructureView;
import tech.molecules.leet.chem.shredder.SynthonShredder;
import javax.swing.*;
import javax.swing.border.LineBorder;
import java.awt.*;
import java.util.*;
import java.util.List;
public class ChemUtils {
public static String idcodeToSmiles(String idcode) {
StereoMolecule m = new StereoMolecule();
IDCodeParser p = new IDCodeParser();
p.parse(m,idcode);
String smiles = "exception_in_smiles_creator";
try{
IsomericSmilesCreator smiles_creator = new IsomericSmilesCreator(m);
smiles=smiles_creator.getSmiles();
}
catch(Exception ex) {
System.out.println("Exception in idcodeToSmiles..");
}
return smiles;
}
public static String stereoMoleculeToSmiles(StereoMolecule sm) {
IsomericSmilesCreator smiles_creator = new IsomericSmilesCreator(sm);
return smiles_creator.getSmiles();
}
public static StereoMolecule parseIDCode(String idc) {
IDCodeParser icp = new IDCodeParser();
StereoMolecule mi = new StereoMolecule();
icp.parse(mi,idc);
mi.ensureHelperArrays(Molecule.cHelperCIP);
return mi;
}
public static List parseIDCodes(List idcodes) {
List parsed = new ArrayList<>();
IDCodeParser icp = new IDCodeParser();
for(String si : idcodes) {
StereoMolecule mi = new StereoMolecule();
icp.parse(mi, si);
mi.ensureHelperArrays(Molecule.cHelperCIP);
parsed.add(mi);
}
return parsed;
}
public static BitSet findAtomsWithAtomicNo(StereoMolecule mi, int ano) {
BitSet bs = new BitSet();
mi.ensureHelperArrays(Molecule.cHelperNeighbours);
for(int zi=0;zi findNeighbors(StereoMolecule mi, int a) {
List neighbors = new ArrayList<>();
//mi.ensureHelperArrays(Molecule.cHelperNeighbours);
if(mi.getAtoms()<=a) {
return new ArrayList<>();
}
for(int za=0;za= 88 will not be considered except if they are seed_atoms
*
* @return
*/
public static StereoMolecule createProximalFragment(StereoMolecule mi_pre, List seed_atoms, int region_size, boolean omit_connectors, boolean[] neglectAtom) {
StereoMolecule mi_conn = new StereoMolecule(mi_pre);
//mi_pre.setFragment(true);
//mi_conn.removeQueryFeatures(); // !! Remove query features
// REMOVE ONLY NARROWING QUERY FEATURES:
//QueryFeatureUtils.removeNarrowingQueryFeatures(mi_conn);
mi_conn.ensureHelperArrays(Molecule.cHelperCIP);
// 2. cut out the connector region:
boolean keep_atoms[] = new boolean[mi_conn.getAtoms()];
for(int zi=0;zi=88) {
continue;
}
}
// NOTE! this returns -1 if no path is found within connector_region_size (I think..)
int path_length = mi_conn.getPathLength(ci,zi,region_size,neglectAtom);
if ( path_length>=0 ){
if(path_length<=region_size ) {
keep_atoms[zi] = true;
}
}
}
}
StereoMolecule mi_cut = new StereoMolecule();
mi_cut.setFragment(true);
mi_conn.copyMoleculeByAtoms(mi_cut,keep_atoms,true,null);
mi_cut.ensureHelperArrays(Molecule.cHelperCIP);
if(false) {
System.out.println("CPF: " + mi_cut.getIDCode());
}
return mi_cut;
}
public static int hac(StereoMolecule m) {
m.ensureHelperArrays(Molecule.cHelperNeighbours);
return m.getAtoms();
}
public static int countRingAtoms(StereoMolecule m) {
m.ensureHelperArrays(Molecule.cHelperNeighbours);
int ra = 0;
for(int zi=0;zi atomic_numbers) {
m.ensureHelperArrays(Molecule.cHelperNeighbours);
int ra = 0;
for(int zi=0;zi=0) {
ra++;
}
}
}
return ra;
}
public static boolean checkIfAllAtomsAreInSameFragment(StereoMolecule mi, List atoms) {
mi.ensureHelperArrays(Molecule.cHelperCIP);
if(atoms.size()==0) {return true;} // hmm.. or false? I dont know.. :)
if(atoms.size()==1) {return true;} // this one is clear I think :)
int[] frag = mi.getFragmentAtoms(atoms.get(0));
BitSet bsi = toBitSet(frag);
BitSet bs_atoms = toBitSet( atoms );
BitSet bsi_and_bsa = (BitSet) bs_atoms.clone();
bsi_and_bsa.and(bs_atoms);
return bs_atoms.cardinality()==bsi_and_bsa.cardinality();
}
public static List getSelectedAtoms(StereoMolecule m) {
ArrayList sel = new ArrayList<>();
m.ensureHelperArrays(Molecule.cHelperCIP);
for(int zi=0;zi getSelectedBonds(StereoMolecule m) {
ArrayList sel = new ArrayList<>();
m.ensureHelperArrays(Molecule.cHelperCIP);
for(int zi=0;zi=0){bs.set(arr[zi],true);} }
return bs;
}
public static BitSet toBitSet(List val) {
BitSet bsi = new BitSet();
for(int zi=0;zi list) {
int[] arr = new int[list.size()];
for(int zi=0;zi toIntList(BitSet bsi) {
List li = new ArrayList<>();
bsi.stream().forEach( ii -> li.add(ii) );
return li;
}
public static Map inverseMap(int[] map) {
Map inv = new HashMap<>();
for(int zi=0;zi idcodes, int sx, int sy) {
plotMolecules(title,idcodes.stream().toArray(String[]::new),sx,sy);
}
public static void plotMolecules(String title, String idcodes[], int sx, int sy) {
plotMolecules(title,parseIDCodes(Arrays.asList(idcodes)).toArray(new StereoMolecule[0]),sx,sy);
}
public static void plotMolecules(String title, StereoMolecule mols[], int sx, int sy) {
plotMolecules(title,mols,sx,sy,true);
}
public static void plotMolecules(String title, StereoMolecule mols[], int sx, int sy, boolean inventCoordinates) {
if (frame == null) {
JFrame f = new JFrame();
frame = f;
tp = new JTabbedPane();
f.getContentPane().setLayout(new BorderLayout());
f.getContentPane().add(tp);
f.setVisible(true);
int w = Math.min(800, sx * 300);
int h = Math.min(800, sx * 300);
f.setSize(w, h);
f.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
}
JScrollPane sp = new JScrollPane();
JPanel pi = new JPanel();
pi.setLayout(new GridLayout(sx, sy));
sp.setViewportView(pi);
CoordinateInventor ci = new CoordinateInventor();
for (int zi = 0; zi < mols.length; zi++) {
StereoMolecule si = mols[zi];
if(inventCoordinates) {
ci.invent(si);
}
JStructureView sv = new JStructureView(si);
//sv.setIDCode(si.getIDCode());
//sv.set
pi.add(sv);
sv.setBorder(new LineBorder(Color.black));
}
tp.add(title, pi);
}
public static void main(String args[]) {
String sa = "didHPD@zxHR[Y^FZZX@`";
String sb = "dmuHPHF`neNdefuQfjjj`B";
String idcodes[] = new String[]{sa, sb};
plotMolecules("Out_A", idcodes, 2, 2);
plotMolecules("Out_b", idcodes, 2, 2);
plotMolecules("Out_c", idcodes, 2, 2);
}
}
}