annotate icqsol_utils.py @ 0:1574fdcc16df draft default tip

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