annotate icqsol_utils.py @ 0:6ff7cf6ec553 draft default tip

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