com.actelion.research.chem.io.DWARFileCreator Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of openchemlib Show documentation
Show all versions of openchemlib Show documentation
Open Source Chemistry Library
/*
* Copyright (c) 1997 - 2016
* Actelion Pharmaceuticals Ltd.
* Gewerbestrasse 16
* CH-4123 Allschwil, Switzerland
*
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are met:
*
* 1. Redistributions of source code must retain the above copyright notice, this
* list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright notice,
* this list of conditions and the following disclaimer in the documentation
* and/or other materials provided with the distribution.
* 3. Neither the name of the the copyright holder nor the
* names of its contributors may be used to endorse or promote products
* derived from this software without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
* WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
* DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR
* ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
* (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
* LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
* ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
* SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*
*/
package com.actelion.research.chem.io;
import com.actelion.research.chem.Canonizer;
import com.actelion.research.chem.StereoMolecule;
import java.io.BufferedWriter;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Properties;
import java.util.TreeMap;
public class DWARFileCreator {
private BufferedWriter mWriter;
private String[] mRow;
private ArrayList mColumnTitleList;
private TreeMap mColumnPropertiesMap;
/**
* Use a DWARFileCreator for writing native DataWarrior files without a CompoundTableModel.
* (if you have a populated CompoundTableModel, use the CompoundTableSaver instead).
* To use the DWARFileCreator you need to follow these steps:
* - instantiate a new DWARFileCreator for every file
* - define individual columns with addStructureColumn(), addDescriptorColumn(), and addAlphanumericalColumn()
* - add custom column properties, if you need to with addColumnProperty()
* - call writeHeader() once to create the file and write file & table headers
* - for every row call setRowStructure() and setRowValue() for cell values and then writeCurrentRow()
* - call writeEnd() to close the file
*/
public DWARFileCreator(BufferedWriter writer) {
mWriter = writer;
mColumnTitleList = new ArrayList();
}
/**
* This adds a column to host canonical structure representations (idcodes).
* This call allocates the column and defines proper column properties.
* If you want DataWarrior to show structure in this column with the original
* atom coordinates, you need to also add a column for the encoded atom coordinates.
* Otherwise DataWarrior would generate 2D-coordinates for the structures atoms
* on the fly.
* @param name
* @param idColumnName null or column title of other column that hold a compound identifier
* @return new structure column index
*/
public int addStructureColumn(String name, String idColumnName) {
int structureColumn = mColumnTitleList.size();
String title = makeUnique(name);
mColumnTitleList.add(title);
addColumnProperty(structureColumn, "specialType", "idcode");
if (idColumnName != null)
addColumnProperty(structureColumn, "idColumn", idColumnName);
return structureColumn;
}
/**
* Creates a new column to hold encoded 2-dimensional atom coordinates for the structures
* stored in the associated structure column. A structure column may not have more than one
* associated 2-dimensional atom coordinate column.
* This call allocates the column and defines proper column properties.
* @param structureColumn
* @return new coordinate column index
*/
public int add2DCoordinatesColumn(int structureColumn) {
return addStructureChildColumn("idcoordinates2D", "idcoordinates2D", structureColumn);
}
/**
* Creates a new column to hold encoded 3-dimensional atom coordinates for the structures
* stored in the associated structure column. A structure column may have multiple
* associated 3-dimensional atom coordinate columns.
* This call allocates the column and defines proper column properties.
* @param name 3D-coordinate column names are used to distinguish multiple 3D-coordinate sets
* @param structureColumn
* @return new coordinate column index
*/
public int add3DCoordinatesColumn(String name, int structureColumn) {
return addStructureChildColumn("idcoordinates3D", name, structureColumn);
}
/**
* Creates a new column to hold a chemical descriptor for the structures
* stored in the associated structure column. A structure column may have multiple
* associated descriptor columns, provided that these have distinct types.
* This call allocates the column and defines proper column properties.
* @param descriptorShortName name used to identify the descriptor type, e.g. 'FragFp'
* @param structureColumn
* @return new descriptor column index
*/
public int addDescriptorColumn(String descriptorShortName, String descriptorVersion, int structureColumn) {
int column = addStructureChildColumn(descriptorShortName, descriptorShortName, structureColumn);
addColumnProperty(column, "version", descriptorVersion);
return column;
}
private int addStructureChildColumn(String specialType, String name, int structureColumn) {
int column = mColumnTitleList.size();
mColumnTitleList.add(makeUnique(name));
addColumnProperty(column, "parent", mColumnTitleList.get(structureColumn));
addColumnProperty(column, "specialType", specialType);
return column;
}
/**
* Creates a new standard column to hold any alphanumerical content.
* @param name
* @return
*/
public int addAlphanumericalColumn(String name) {
mColumnTitleList.add(makeUnique(name));
return mColumnTitleList.size()-1;
}
/**
* This method may be used to define column properties, e.g. to associate the column
* with an URL for identifier lookups, to define a data range, or other special DataWarrior
* related features.
* @param column
* @param key
* @param value
*/
public void addColumnProperty(int column, String key, String value) {
if (mColumnPropertiesMap == null)
mColumnPropertiesMap = new TreeMap();
Properties cp = mColumnPropertiesMap.get(column);
if (cp == null) {
cp = new Properties();
mColumnPropertiesMap.put(column, cp);
}
cp.setProperty(key, value);
}
/**
* Call this after defining columns and specifying column properties
* @param rowCount -1 if row count is not known
* @throws IOException
*/
public void writeHeader(int rowCount) throws IOException {
mWriter.write("");
mWriter.newLine();
mWriter.write("");
mWriter.newLine();
if (rowCount > 0) {
mWriter.write("");
mWriter.newLine();
}
mWriter.write(" ");
mWriter.newLine();
if (mColumnPropertiesMap != null) {
mWriter.write("");
mWriter.newLine();
for (int column:mColumnPropertiesMap.keySet()) {
mWriter.write("");
mWriter.newLine();
Properties cp = mColumnPropertiesMap.get(column);
for (String key:cp.stringPropertyNames()) {
String value = cp.getProperty(key);
mWriter.write("");
mWriter.newLine();
}
}
mWriter.write(" ");
mWriter.newLine();
}
boolean isFirst = true;
for (int i=0; i
© 2015 - 2025 Weber Informatics LLC | Privacy Policy