1414# See the License for the specific language governing permissions and
1515# limitations under the License.
1616
17-
1817from __future__ import print_function
1918import sys , gzip , json , uuid
2019from math import log10 , floor
2120from pipelines_utils .BasicObjectWriter import BasicObjectWriter
2221from pipelines_utils .TsvWriter import TsvWriter
2322
23+
2424def log (* args , ** kwargs ):
25- """
26- Log output to STDERR
25+ """Log output to STDERR
2726 """
2827 print (* args , file = sys .stderr , ** kwargs )
2928
29+
3030def round_sig (x , sig ):
3131 """Round the number to the specified number of significant figures"""
32- return round (x , sig - int (floor (log10 (abs (x ))))- 1 )
33-
34- def add_default_input_args (parser ):
35- parser .add_argument ('-i' , '--input' , help = "Input file, if not defined the STDIN is used" )
36- parser .add_argument ('-if' , '--informat' , choices = ['sdf' , 'json' ], help = "Input format. When using STDIN this must be specified." )
37-
38- def add_default_output_args (parser ):
39- parser .add_argument ('-o' , '--output' , help = "Base name for output file (no extension). If not defined then SDTOUT is used for the structures and output is used as base name of the other files." )
40- parser .add_argument ('-of' , '--outformat' , choices = ['sdf' , 'json' ], help = "Output format. Defaults to 'sdf'." )
41- parser .add_argument ('--meta' , action = 'store_true' , help = 'Write metadata and metrics files' )
42-
43- def add_default_io_args (parser ):
44- add_default_input_args (parser )
45- add_default_output_args (parser )
46-
47-
48- def default_open_input_output (inputDef , inputFormat , outputDef , defaultOutput , outputFormat , thinOutput = False , valueClassMappings = None ,
49- datasetMetaProps = None , fieldMetaProps = None ):
50- """Default approach to handling the inputs and outputs"""
51- input , suppl = default_open_input (inputDef , inputFormat )
52- output ,writer ,outputBase = default_open_output (outputDef , defaultOutput , outputFormat , thinOutput = thinOutput ,
53- valueClassMappings = valueClassMappings , datasetMetaProps = datasetMetaProps , fieldMetaProps = fieldMetaProps )
54- return input ,output ,suppl ,writer ,outputBase
55-
56-
57- def default_open_input (inputDef , inputFormat ):
58- if not inputDef and not inputFormat :
59- raise ValueError ('Must specify either an input file name or an input format (or both)' )
60- elif inputFormat == 'sdf' or (inputDef and (inputDef .lower ().endswith ('.sdf' ) or inputDef .lower ().endswith ('.sdf.gz' ))):
61- input , suppl = default_open_input_sdf (inputDef )
62- elif inputFormat == 'json' or (inputDef and (inputDef .lower ().endswith ('.data' ) or inputDef .lower ().endswith ('.data.gz' ))):
63- input , suppl = default_open_input_json (inputDef )
64- else :
65- raise ValueError ('Unsupported input format' )
66-
67- return input , suppl
32+ return round (x , sig - int (floor (log10 (abs (x )))) - 1 )
6833
6934
7035def open_file (filename ):
@@ -75,18 +40,20 @@ def open_file(filename):
7540 return open (filename , 'r' )
7641
7742
78- def create_simple_writer (outputDef , defaultOutput , outputFormat , fieldNames , compress = True , valueClassMappings = None , datasetMetaProps = None , fieldMetaProps = None ):
79- """Create a simple writer suitable for writing flat data e.g. as BasicObject or TSV"""
43+ def create_simple_writer (outputDef , defaultOutput , outputFormat , fieldNames ,
44+ compress = True , valueClassMappings = None ,
45+ datasetMetaProps = None , fieldMetaProps = None ):
46+ """Create a simple writer suitable for writing flat data
47+ e.g. as BasicObject or TSV."""
8048
8149 if not outputDef :
8250 outputBase = defaultOutput
8351 else :
8452 outputBase = outputDef
8553
8654 if outputFormat == 'json' :
87-
88- write_squonk_datasetmetadata (outputBase , True , valueClassMappings , datasetMetaProps , fieldMetaProps )
89-
55+ write_squonk_datasetmetadata (outputBase , True , valueClassMappings ,
56+ datasetMetaProps , fieldMetaProps )
9057 return BasicObjectWriter (open_output (outputDef , 'data' , compress )), outputBase
9158
9259 elif outputFormat == 'tsv' :
@@ -95,6 +62,7 @@ def create_simple_writer(outputDef, defaultOutput, outputFormat, fieldNames, com
9562 else :
9663 raise ValueError ("Unsupported format: " + outputFormat )
9764
65+
9866def open_output (basename , ext , compress ):
9967 if basename :
10068 fname = basename + '.' + ext
@@ -110,6 +78,7 @@ def open_output(basename, ext, compress):
11078 else :
11179 return sys .stdout
11280
81+
11382def write_squonk_datasetmetadata (outputBase , thinOutput , valueClassMappings , datasetMetaProps , fieldMetaProps ):
11483 """This is a temp hack to write the minimal metadata that Squonk needs.
11584 Will needs to be replaced with something that allows something more complete to be written.
@@ -150,7 +119,8 @@ def write_squonk_datasetmetadata(outputBase, thinOutput, valueClassMappings, dat
150119def write_metrics (baseName , values ):
151120 """Write the metrics data
152121
153- :param baseName: The base name of the output files. e.g. extensions will be appended to this base name
122+ :param baseName: The base name of the output files.
123+ e.g. extensions will be appended to this base name
154124 :param values dictionary of values to write
155125 """
156126 m = open (baseName + '_metrics.txt' , 'w' )
@@ -161,7 +131,8 @@ def write_metrics(baseName, values):
161131
162132
163133def generate_molecule_object_dict (source , format , values ):
164- """Generate a dictionary that represents a Squonk MoleculeObject when writen as JSON
134+ """Generate a dictionary that represents a Squonk MoleculeObject when
135+ writen as JSON
165136
166137 :param source: Molecules in molfile or smiles format
167138 :param format: The format of the molecule. Either 'mol' or 'smiles'
0 commit comments