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

com.actelion.research.chem.io.Mol2FileParser Maven / Gradle / Ivy

There is a newer version: 2024.11.2
Show newest version
/*
 * Created on Dec 20, 2004
 *
 */
package com.actelion.research.chem.io;

import com.actelion.research.chem.*;

import java.io.LineNumberReader;
import java.io.Reader;
import java.io.Writer;
import java.text.DecimalFormat;
import java.text.NumberFormat;
import java.util.*;

/**
 * 
 * @author freyssj
 */
public class Mol2FileParser extends AbstractParser {
	
	public static final int iNO_CHARGES = 0; 
	public static final int iDEL_RE = 1;
	public static final int iGASTEIGER = 2;
	public static final int iGAST_HUCK = 3;
	public static final int iHUCKEL = 4;
	public static final int iPULLMAN = 5;
	public static final int iGAUSS80_CHARGES = 6; 
	public static final int iAMPAC_CHARGES = 7;
	public static final int iMULLIKEN_CHARGES = 8;
	public static final int iDICT_CHARGES = 9;
	public static final int iMMFF94_CHARGES = 10;
	public static final int iUSER_CHARGES = 11;
	
	private static final NumberFormat NF_PARTIAL_CHARGES = new DecimalFormat(" 0.0000;-0.0000");

	private static final String sNO_CHARGES = "NO_CHARGES"; 
	private static final String sDEL_RE = "DEL_RE";
	private static final String sGASTEIGER = "GASTEIGER";
	private static final String sGAST_HUCK = "GAST_HUCK";
	private static final String sHUCKEL = "HUCKEL";
	private static final String sPULLMAN = "PULLMAN";
	private static final String sGAUSS80_CHARGES = "GAUSS80_CHARGES"; 
	private static final String sAMPAC_CHARGES = "AMPAC_CHARGES";
	private static final String sMULLIKEN_CHARGES = "MULLIKEN_CHARGES";
	private static final String sDICT_CHARGES = "DICT_CHARGES";
	private static final String sMMFF94_CHARGES = "MMFF94_CHARGES";
	private static final String sUSER_CHARGES = "USER_CHARGES";
	
	private static HashMap hmIndex_CHARGETYPE;
	
	private boolean isLoadHydrogen = true;

	private void addMol(List res, Molecule3D m, Set aromaticAtoms, Set aromaticBonds) {
		if(m==null || m.getAllAtoms()==0) return;
		
		if(true) {
			//Use AromaticityResolver		
			ExtendedMolecule mol = new StereoMolecule(m);
			int[] bondMap = mol.getHandleHydrogenBondMap();
			int j =0;
			//for (int b : aromaticBonds) {
			//	if(mol.getAtomicNo(mol.getBondAtom(0, b))!=8 && mol.getAtomicNo(mol.getBondAtom(1, b))!=8) {
			//		mol.setBondType(b, ExtendedMolecule.cBondTypeDelocalized);
			//	}
			//}
			new AromaticityResolver(mol).locateDelocalizedDoubleBonds(null,true,true);
			
	
			for (int i=0; i();
			hmIndex_CHARGETYPE.put(iNO_CHARGES, sNO_CHARGES);
			hmIndex_CHARGETYPE.put(iDEL_RE, sDEL_RE);
			hmIndex_CHARGETYPE.put(iGASTEIGER, sGASTEIGER);
			hmIndex_CHARGETYPE.put(iGAST_HUCK, sGAST_HUCK);
			hmIndex_CHARGETYPE.put(iHUCKEL, sHUCKEL);
			hmIndex_CHARGETYPE.put(iPULLMAN, sPULLMAN);
			hmIndex_CHARGETYPE.put(iGAUSS80_CHARGES, sGAUSS80_CHARGES);
			hmIndex_CHARGETYPE.put(iAMPAC_CHARGES, sAMPAC_CHARGES);
			hmIndex_CHARGETYPE.put(iMULLIKEN_CHARGES, sMULLIKEN_CHARGES);
			hmIndex_CHARGETYPE.put(iDICT_CHARGES, sDICT_CHARGES);
			hmIndex_CHARGETYPE.put(iMMFF94_CHARGES, sMMFF94_CHARGES);
			hmIndex_CHARGETYPE.put(iUSER_CHARGES, sUSER_CHARGES);
		}
		
		return hmIndex_CHARGETYPE.get(type);
	}
	
	
	/**
	 * @see com.actelion.research.chem.io.AbstractParser#loadGroup(String, java.io.Reader, int, int)
	 */
	@Override
	public List loadGroup(String fileName, Reader in, int from, int to) throws Exception {
		List res = new ArrayList();
		String line;

		Molecule3D m = new Molecule3D();
		Set aromaticAtoms = new HashSet();
		Set aromaticBonds = new HashSet();
		Map nToA = new TreeMap();
		LineNumberReader reader = new LineNumberReader(in);
		
		int state = 0;
		int lineNo = 0;
		int count = -1;
		while ((line = reader.readLine()) != null) {
			if (line.startsWith("@")) lineNo = 0;
			
			if (line.startsWith("@MOLECULE")) state = 0;
			else if (line.startsWith("@ATOM")) state = 1;
			else if (line.startsWith("@BOND")) state = 2;
			else if (line.startsWith("@")) state = -1;
			else {
				lineNo++;
				switch (state) {
				case 0: {
					if(lineNo==1) {
						//We have a new molecule, first process the last molecule and add it to the list
						count++;
						if(from>count) continue;
						if(to>=0 && to aromaticAtomsMapped = new HashSet();
		Set aromaticBondsMapped = new HashSet();
		aromaticAtoms.stream().forEach(aa -> aromaticAtomsMapped.add(atomMap[aa]));
		aromaticBonds.stream().forEach(ab -> aromaticBondsMapped.add(bondMap[ab]));
		if(from>count) {}
		else if(to>=0 && to mols, Writer writer) throws Exception {
		save(mols, iNO_CHARGES, writer);
	}

	/**
	 * Writes also the partial charges
	 * @param mols
	 * @param chargeType
	 * @param writer
	 * @throws Exception
	 */
	public void save(List mols, int chargeType, Writer writer) throws Exception {
		DecimalFormat df = new DecimalFormat("0.0000");

		for (Molecule3D mol : mols) {
			
			//Count the number of bonds and atoms (except lone pairs)
			int nAtoms = 0;
			int nBonds = 0;
			for (int i = 0; i < mol.getAllAtoms(); i++) {
				if(mol.getAtomicNo(i)<=0) continue;
				nAtoms++;
			}
			for (int i = 0; i < mol.getAllBonds(); i++) {
				if(mol.getAtomicNo(mol.getBondAtom(0, i))<=0) continue;
				if(mol.getAtomicNo(mol.getBondAtom(1, i))<=0) continue;
				nBonds++;
			}

			// Write the molecule
			writer.write("@MOLECULE" + NEWLINE);
			writer.write(mol.getName() + NEWLINE);
			writeR(writer, "" + nAtoms, 5);
			writeR(writer, "" + nBonds, 6);
			writer.write(NEWLINE);
			writer.write("SMALL" + NEWLINE);
			writer.write(getChargeType(chargeType) + NEWLINE);
	
			// Write the atoms
			writer.write("@ATOM" + NEWLINE); 			
			for (int i = 0; i < mol.getAllAtoms(); i++) {
				if(mol.getAtomicNo(i)<=0) continue;
				writeR(writer, "" + (i+1), 7);
				writer.write(" ");
				
				String desc = mol.getAtomName(i);
				if(desc==null || desc.length()==0) {
					desc = "" + ExtendedMolecule.cAtomLabel[mol.getAtomicNo(i)];
				}
				
				writeL(writer, desc, 8);
				writeR(writer, df.format(mol.getAtomX(i)), 10);
				writeR(writer, df.format(mol.getAtomY(i)), 10);
				writeR(writer, df.format(mol.getAtomZ(i)), 10);
				writer.write(" ");
				writeL(writer, ExtendedMolecule.cAtomLabel[mol.getAtomicNo(i)] + (mol.isAromaticAtom(i)?".ar":""), 8);
				writeR(writer, mol.getAtomChainId(i)!=null && mol.getAtomChainId(i).length()>0? mol.getAtomChainId(i): "1", 5);
				writer.write("  ");
				writeL(writer, mol.getAtomAmino(i)!=null && mol.getAtomAmino(i).length()>0? mol.getAtomAmino(i): "1", 11);
				writer.write(NF_PARTIAL_CHARGES.format(mol.getPartialCharge(i)));
	
				writer.write(NEWLINE);
			}
	
			// Write the bonds
			writer.write("@BOND" + NEWLINE);
			for (int i = 0; i < mol.getAllBonds(); i++) {
				if(mol.getAtomicNo(mol.getBondAtom(0, i))<=0) continue;
				if(mol.getAtomicNo(mol.getBondAtom(1, i))<=0) continue;
				writeR(writer, "" + (i+1), 6);
				writeR(writer, "" + (mol.getBondAtom(0, i)+1), 5);
				writeR(writer, "" + (mol.getBondAtom(1, i)+1), 5);
				writer.write(" ");
				writeL(writer, "" + mol.getBondOrder(i), 5);
				writer.write(NEWLINE);
			}
		}
	}

	
	public static void main(String[] args) throws Exception {
		List res = new Mol2FileParser().loadGroup("c:/mopac11971.mol2");
		//List res = new Mol2FileParser().loadGroup("c:/orig_pose_min.mol2");
		System.out.println("models=" +res.size()+" atm="+res.get(0).getAllAtoms()+" "+res.get(0).getAtoms());
		//new Mol2FileParser().save(res.get(0), "c:/t.mol2");
	}
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy