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

com.actelion.research.chem.io.AbstractParser 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.Molecule3D;

import java.io.*;
import java.nio.charset.StandardCharsets;
import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Set;
import java.util.zip.GZIPInputStream;
import java.util.zip.GZIPOutputStream;
import java.util.zip.ZipInputStream;
import java.util.zip.ZipOutputStream;


/**
 * A parser is used to load and save molecules from the filesystem.
 * The save procedure is optional
 * 
 * @author freyssj
 */
@SuppressWarnings("resource")
public abstract class AbstractParser {

	protected static final String NEWLINE = System.getProperty("line.separator");

	protected List errors = new ArrayList();
	protected boolean optimize3D = true;
	
	/**
	 * 
	 * @param fileName
	 * @throws Exception
	 */
	public final List loadGroup(String fileName) throws Exception {
		Reader r = null;
		try {
			
			if(fileName.toUpperCase().endsWith(".GZ")) {
				GZIPInputStream is = new GZIPInputStream(new FileInputStream(fileName));
				r = new InputStreamReader(is, StandardCharsets.UTF_8);
			} else if(fileName.toUpperCase().endsWith(".ZIP")) {
				ZipInputStream is = new ZipInputStream(new FileInputStream(fileName));
				r = new InputStreamReader(is, StandardCharsets.UTF_8);
			} else {
				r = new BufferedReader(new FileReader(fileName)); 				
			}			
			List mols = loadGroup(fileName, r, -1, -1);
			int count = 0;
			for (Molecule3D mol: mols) {
				count++;
				if(mol!=null && (mol.getName()==null || mol.getName().length()==0)) mol.setName(new File(fileName).getName() + (mols.size()>1? "#"+ count:""));
			}			
			return mols;
		} finally {
			if(r!=null) try {r.close();} catch(Exception e){}
		}
	}
	
	public List loadGroup(String fileName, Reader in) throws Exception{
		return loadGroup(fileName, in, -1, -1);
	}
	public abstract List loadGroup(String fileName, Reader in, int from, int to) throws Exception;

	/**
	 *
	 * @param file
	 * @throws Exception
	 */
	public final Molecule3D load(File file) throws Exception {
		return load(file.getPath());
	}

	/**
	 * 
	 * @param fileName
	 * @throws Exception
	 */
	public final Molecule3D load(String fileName) throws Exception {
		List list = loadGroup(fileName);
		return list.size()>0? list.get(0): null;
	}

	/**
	 *
	 * @param fileName
	 * @param in
	 * @throws Exception
	 */
	public final Molecule3D load(String fileName, Reader in) throws Exception {
		List list = loadGroup(fileName, in, -1, -1);
		return list.size()>0? list.get(0): null;
	}
	
	

	/**
	 * 
	 * @param mol
	 * @param fileName
	 * @throws Exception
	 */ 
	public final void save(List mol, String fileName) throws Exception {
		Writer w = null;
		try {
			if(fileName.toUpperCase().endsWith(".GZ")) {
				GZIPOutputStream os = new GZIPOutputStream(new FileOutputStream(fileName));
				w = new OutputStreamWriter(os);				
			} else if(fileName.toUpperCase().endsWith(".ZIP")) {
				ZipOutputStream os = new ZipOutputStream(new FileOutputStream(fileName));
				w = new OutputStreamWriter(os);
			} else {
				w = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(fileName), StandardCharsets.UTF_8));
			}
			
			save(mol, w);
			w.close();
		} finally {
			if(w!=null) try {w.close();} catch(Exception e){}
		}
	}
	
		
	/**
	 * 
	 * @param mol
	 * @param fileName
	 * @throws Exception
	 */
	public final void save(Molecule3D mol, String fileName) throws Exception {
		Writer w = null;
		try {
			if(fileName.toUpperCase().endsWith(".GZ")) {
				GZIPOutputStream os = new GZIPOutputStream(new FileOutputStream(fileName));
				w = new OutputStreamWriter(os);				
			} else if(fileName.toUpperCase().endsWith(".ZIP")) {
				ZipOutputStream os = new ZipOutputStream(new FileOutputStream(fileName));
				w = new OutputStreamWriter(os);
			} else {
				w = new FileWriter(fileName); 
			}
			
			save(mol, w);
			w.close();
		} finally {
			if(w!=null) try {w.close();} catch(Exception e){}
		}
	}
	
	/**
	 * 
	 * @param mol
	 * @param writer
	 * @throws Exception
	 */
	public void save(Molecule3D mol, Writer writer) throws Exception {
		throw new IllegalAccessError("Not implemented");
	}

	
	/**
	 * If not subclassed, save the files separately
	 * @param mols
	 * @param writer
	 * @throws Exception
	 */
	public void save(List mols, Writer writer) throws Exception {
		if(mols==null || mols.size()==0) return;
		if(mols.size()==1) save(mols.get(0), writer);
		else {
			throw new IllegalAccessError("Cannot save more than one file in  this format");
		}
		
	}

	public static final void convertDataToPrimitiveTypes(List res) {
		//Convert the field values to integer or double if possible
		if(res.size()==0) return;
		Set fieldNames = new HashSet();
		for (Molecule3D m : res) {
			fieldNames.addAll(m.getAuxiliaryInfos().keySet());
		}
		loop: for(String fieldName: fieldNames) {
			int type = 0;
			String val = "";
			for (Molecule3D m : res) {
				Object o = m.getAuxiliaryInfos().get(fieldName);
				if(o==null) continue;
				if(!(o instanceof String)) {
					continue loop; 
				}
				val = (String) o; 
				if(type==0) try {Integer.parseInt(val);} catch (Exception e) {type++;}
				if(type==1) try {Double.parseDouble(val);} catch (Exception e) {type++;}
				if(type==2) break; //String
			}
			if(type==0) {
				for (Molecule3D m : res) {
					val = (String)m.getAuxiliaryInfos().get(fieldName);
					if(val!=null) m.getAuxiliaryInfos().put(fieldName, Integer.parseInt((String)m.getAuxiliaryInfos().get(fieldName)));
				}
			} else if(type==1) {
				for (Molecule3D m : res) {
					val = (String)m.getAuxiliaryInfos().get(fieldName);
					if(val!=null) m.getAuxiliaryInfos().put(fieldName, Double.parseDouble(val));
				}
			}
		}				
	}
	

	protected static void writeR(Writer writer, String data, int len) throws IOException {
		if(data==null) data = "";
		int l = Math.max(0, len - data.length());
		for (int i = 0; i < l; i++)
			writer.write(' ');
		writer.write(data);
	}

	protected static void writeL(Writer writer, String data, int len) throws IOException {
		if(data==null) data="";
		writer.write(data);
		int l = Math.max(0, len - data.length());
		for (int i = 0; i < l; i++)
			writer.write(' ');
	}

	public List getErrors() {
		return errors;
	}
	
	protected static boolean is3D(Molecule3D m) {
		for(int a=0; a0.1) return true;
		}
		return false;
	}

	public void setOptimize3D(boolean optimize3d) {
		optimize3D = optimize3d;
	}
	
	public boolean isOptimize3D() {
		return optimize3D;
	}

	
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy