annotate icqsol_utils.py @ 0:2294ae1842d5 draft default tip

Uploaded
author iuc
date Tue, 23 Aug 2016 15:04:45 -0400
parents
children
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
0
2294ae1842d5 Uploaded
iuc
parents:
diff changeset
1 import os
2294ae1842d5 Uploaded
iuc
parents:
diff changeset
2 import sys
2294ae1842d5 Uploaded
iuc
parents:
diff changeset
3 import tempfile
2294ae1842d5 Uploaded
iuc
parents:
diff changeset
4
2294ae1842d5 Uploaded
iuc
parents:
diff changeset
5 from icqsol.shapes.icqShapeManager import ShapeManager
2294ae1842d5 Uploaded
iuc
parents:
diff changeset
6 from icqsol.bem.icqLaplaceSolver import LaplaceSolver
2294ae1842d5 Uploaded
iuc
parents:
diff changeset
7
2294ae1842d5 Uploaded
iuc
parents:
diff changeset
8 PLY = 'ply'
2294ae1842d5 Uploaded
iuc
parents:
diff changeset
9 POLYDATA = 'POLYDATA'
2294ae1842d5 Uploaded
iuc
parents:
diff changeset
10 VTK = 'vtk'
2294ae1842d5 Uploaded
iuc
parents:
diff changeset
11
2294ae1842d5 Uploaded
iuc
parents:
diff changeset
12
2294ae1842d5 Uploaded
iuc
parents:
diff changeset
13 def asbool(val):
2294ae1842d5 Uploaded
iuc
parents:
diff changeset
14 return str(val).lower() in ['yes', 'true']
2294ae1842d5 Uploaded
iuc
parents:
diff changeset
15
2294ae1842d5 Uploaded
iuc
parents:
diff changeset
16
2294ae1842d5 Uploaded
iuc
parents:
diff changeset
17 def get_format_and_type(galaxy_ext):
2294ae1842d5 Uploaded
iuc
parents:
diff changeset
18 # Define the output file format and type.
2294ae1842d5 Uploaded
iuc
parents:
diff changeset
19 format = None
2294ae1842d5 Uploaded
iuc
parents:
diff changeset
20 datatype = None
2294ae1842d5 Uploaded
iuc
parents:
diff changeset
21 if galaxy_ext in ['vtkascii', 'vtkbinary']:
2294ae1842d5 Uploaded
iuc
parents:
diff changeset
22 format = VTK
2294ae1842d5 Uploaded
iuc
parents:
diff changeset
23 elif galaxy_ext in ['plyascii', 'plybinary']:
2294ae1842d5 Uploaded
iuc
parents:
diff changeset
24 format = PLY
2294ae1842d5 Uploaded
iuc
parents:
diff changeset
25 if galaxy_ext in ['vtkascii', 'plyascii']:
2294ae1842d5 Uploaded
iuc
parents:
diff changeset
26 datatype = 'ascii'
2294ae1842d5 Uploaded
iuc
parents:
diff changeset
27 elif galaxy_ext in ['vtkbinary', 'plybinary']:
2294ae1842d5 Uploaded
iuc
parents:
diff changeset
28 datatype = 'binary'
2294ae1842d5 Uploaded
iuc
parents:
diff changeset
29 return format, datatype
2294ae1842d5 Uploaded
iuc
parents:
diff changeset
30
2294ae1842d5 Uploaded
iuc
parents:
diff changeset
31
2294ae1842d5 Uploaded
iuc
parents:
diff changeset
32 def get_input_file_path(tmp_dir, input_file, format):
2294ae1842d5 Uploaded
iuc
parents:
diff changeset
33 """
2294ae1842d5 Uploaded
iuc
parents:
diff changeset
34 iCqSol uses file extensions (e.g., .ply, .vtk) when reading and
2294ae1842d5 Uploaded
iuc
parents:
diff changeset
35 writing files, so the Galaxy dataset naming convention of
2294ae1842d5 Uploaded
iuc
parents:
diff changeset
36 setting all file extensions as .dat must be handled.
2294ae1842d5 Uploaded
iuc
parents:
diff changeset
37 """
2294ae1842d5 Uploaded
iuc
parents:
diff changeset
38 file_path = get_temporary_file_path(tmp_dir, format)
2294ae1842d5 Uploaded
iuc
parents:
diff changeset
39 # Remove the file so we can create a symlink.
2294ae1842d5 Uploaded
iuc
parents:
diff changeset
40 os.remove(file_path)
2294ae1842d5 Uploaded
iuc
parents:
diff changeset
41 os.symlink(input_file, file_path)
2294ae1842d5 Uploaded
iuc
parents:
diff changeset
42 return file_path
2294ae1842d5 Uploaded
iuc
parents:
diff changeset
43
2294ae1842d5 Uploaded
iuc
parents:
diff changeset
44
2294ae1842d5 Uploaded
iuc
parents:
diff changeset
45 def get_laplace_solver(shape_data, max_edge_length=float('inf')):
2294ae1842d5 Uploaded
iuc
parents:
diff changeset
46 return LaplaceSolver(shape_data, max_edge_length=max_edge_length)
2294ae1842d5 Uploaded
iuc
parents:
diff changeset
47
2294ae1842d5 Uploaded
iuc
parents:
diff changeset
48
2294ae1842d5 Uploaded
iuc
parents:
diff changeset
49 def get_shape_manager(format=None, dataset_type=None):
2294ae1842d5 Uploaded
iuc
parents:
diff changeset
50 # Instantiate a ShapeManager.
2294ae1842d5 Uploaded
iuc
parents:
diff changeset
51 return ShapeManager(file_format=format, vtk_dataset_type=dataset_type)
2294ae1842d5 Uploaded
iuc
parents:
diff changeset
52
2294ae1842d5 Uploaded
iuc
parents:
diff changeset
53
2294ae1842d5 Uploaded
iuc
parents:
diff changeset
54 def get_temp_dir(prefix='tmp-vtk-', dir=None):
2294ae1842d5 Uploaded
iuc
parents:
diff changeset
55 """
2294ae1842d5 Uploaded
iuc
parents:
diff changeset
56 Return a temporary directory.
2294ae1842d5 Uploaded
iuc
parents:
diff changeset
57 """
2294ae1842d5 Uploaded
iuc
parents:
diff changeset
58 return tempfile.mkdtemp(prefix=prefix, dir=dir)
2294ae1842d5 Uploaded
iuc
parents:
diff changeset
59
2294ae1842d5 Uploaded
iuc
parents:
diff changeset
60
2294ae1842d5 Uploaded
iuc
parents:
diff changeset
61 def get_tempfilename(dir=None, suffix=None):
2294ae1842d5 Uploaded
iuc
parents:
diff changeset
62 """
2294ae1842d5 Uploaded
iuc
parents:
diff changeset
63 Return a temporary file name.
2294ae1842d5 Uploaded
iuc
parents:
diff changeset
64 """
2294ae1842d5 Uploaded
iuc
parents:
diff changeset
65 if suffix is None:
2294ae1842d5 Uploaded
iuc
parents:
diff changeset
66 s = None
2294ae1842d5 Uploaded
iuc
parents:
diff changeset
67 elif suffix.startswith('.'):
2294ae1842d5 Uploaded
iuc
parents:
diff changeset
68 s = suffix
2294ae1842d5 Uploaded
iuc
parents:
diff changeset
69 else:
2294ae1842d5 Uploaded
iuc
parents:
diff changeset
70 s = '.%s' % suffix
2294ae1842d5 Uploaded
iuc
parents:
diff changeset
71 fd, name = tempfile.mkstemp(suffix=s, dir=dir)
2294ae1842d5 Uploaded
iuc
parents:
diff changeset
72 os.close(fd)
2294ae1842d5 Uploaded
iuc
parents:
diff changeset
73 return name
2294ae1842d5 Uploaded
iuc
parents:
diff changeset
74
2294ae1842d5 Uploaded
iuc
parents:
diff changeset
75
2294ae1842d5 Uploaded
iuc
parents:
diff changeset
76 def get_temporary_file_path(tmp_dir, file_extension):
2294ae1842d5 Uploaded
iuc
parents:
diff changeset
77 """
2294ae1842d5 Uploaded
iuc
parents:
diff changeset
78 Return the path to a temporary file with a valid VTK format
2294ae1842d5 Uploaded
iuc
parents:
diff changeset
79 file extension.
2294ae1842d5 Uploaded
iuc
parents:
diff changeset
80 """
2294ae1842d5 Uploaded
iuc
parents:
diff changeset
81 return get_tempfilename(tmp_dir, file_extension)
2294ae1842d5 Uploaded
iuc
parents:
diff changeset
82
2294ae1842d5 Uploaded
iuc
parents:
diff changeset
83
2294ae1842d5 Uploaded
iuc
parents:
diff changeset
84 def stop_err(msg):
2294ae1842d5 Uploaded
iuc
parents:
diff changeset
85 sys.stderr.write("%s\n" % msg)
2294ae1842d5 Uploaded
iuc
parents:
diff changeset
86 sys.exit()