14
14
# See the License for the specific language governing permissions and
15
15
# limitations under the License.
16
16
17
-
18
17
from __future__ import print_function
19
18
import sys , gzip , json , uuid
20
19
from math import log10 , floor
21
20
from pipelines_utils .BasicObjectWriter import BasicObjectWriter
22
21
from pipelines_utils .TsvWriter import TsvWriter
23
22
23
+
24
24
def log (* args , ** kwargs ):
25
- """
26
- Log output to STDERR
25
+ """Log output to STDERR
27
26
"""
28
27
print (* args , file = sys .stderr , ** kwargs )
29
28
29
+
30
30
def round_sig (x , sig ):
31
31
"""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 )
68
33
69
34
70
35
def open_file (filename ):
@@ -75,18 +40,20 @@ def open_file(filename):
75
40
return open (filename , 'r' )
76
41
77
42
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."""
80
48
81
49
if not outputDef :
82
50
outputBase = defaultOutput
83
51
else :
84
52
outputBase = outputDef
85
53
86
54
if outputFormat == 'json' :
87
-
88
- write_squonk_datasetmetadata (outputBase , True , valueClassMappings , datasetMetaProps , fieldMetaProps )
89
-
55
+ write_squonk_datasetmetadata (outputBase , True , valueClassMappings ,
56
+ datasetMetaProps , fieldMetaProps )
90
57
return BasicObjectWriter (open_output (outputDef , 'data' , compress )), outputBase
91
58
92
59
elif outputFormat == 'tsv' :
@@ -95,6 +62,7 @@ def create_simple_writer(outputDef, defaultOutput, outputFormat, fieldNames, com
95
62
else :
96
63
raise ValueError ("Unsupported format: " + outputFormat )
97
64
65
+
98
66
def open_output (basename , ext , compress ):
99
67
if basename :
100
68
fname = basename + '.' + ext
@@ -110,6 +78,7 @@ def open_output(basename, ext, compress):
110
78
else :
111
79
return sys .stdout
112
80
81
+
113
82
def write_squonk_datasetmetadata (outputBase , thinOutput , valueClassMappings , datasetMetaProps , fieldMetaProps ):
114
83
"""This is a temp hack to write the minimal metadata that Squonk needs.
115
84
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
150
119
def write_metrics (baseName , values ):
151
120
"""Write the metrics data
152
121
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
154
124
:param values dictionary of values to write
155
125
"""
156
126
m = open (baseName + '_metrics.txt' , 'w' )
@@ -161,7 +131,8 @@ def write_metrics(baseName, values):
161
131
162
132
163
133
def 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
165
136
166
137
:param source: Molecules in molfile or smiles format
167
138
:param format: The format of the molecule. Either 'mol' or 'smiles'
0 commit comments