annotate icqsol_utils.py @ 0:88af59c4a2e5 draft default tip

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