Remove prints that were being called in test code.
[htsworkflow.git] / htsworkflow / submission / results.py
1 """Help collect and process results for submission
2 """
3 from collections import MutableMapping
4 import os
5 import logging
6
7 from collections import namedtuple
8
9 LOGGER = logging.getLogger(__name__)
10
11 class ResultMap(MutableMapping):
12     """Store list of results
13     """
14     def __init__(self):
15         self.results_order = []
16         self.results = {}
17
18     def __iter__(self):
19         for item in self.results_order:
20             yield item
21
22     def __len__(self):
23         l = len(self.results)
24         assert l == len(self.results_order)
25         return l
26
27     def __setitem__(self, key, value):
28         self.results_order.append(key)
29         self.results[key] = value
30
31     def __getitem__(self, key):
32         return self.results[key]
33
34     def __delitem__(self, key):
35         del self.results[key]
36         i = self.results_order.index(key)
37         del self.results_order[i]
38
39     def add_results_from_file(self, filename):
40         pathname = os.path.abspath(filename)
41         basepath, name = os.path.split(pathname)
42         results = read_result_list(filename)
43         for lib_id, lib_path in results:
44             if not os.path.isabs(lib_path):
45                 lib_path = os.path.join(basepath, lib_path)
46             self[lib_id] = lib_path
47
48     def make_tree_from(self, source_path, destpath = None):
49         """Create a tree using data files from source path.
50         """
51         if destpath is None:
52             destpath = os.getcwd()
53
54         for lib_id in self.results_order:
55             lib_path = self.results[lib_id]
56             lib_destination = os.path.join(destpath, lib_path)
57             if not os.path.exists(lib_destination):
58                 LOGGER.info("Making dir {0}".format(lib_destination))
59                 os.mkdir(lib_destination)
60
61             source_rel_dir = os.path.join(source_path, lib_path)
62             source_lib_dir = os.path.abspath(source_rel_dir)
63
64             for filename in os.listdir(source_lib_dir):
65                 source_pathname = os.path.join(source_lib_dir, filename)
66                 target_pathname = os.path.join(lib_destination, filename)
67                 if not os.path.exists(source_pathname):
68                     raise IOError(
69                         "{0} does not exist".format(source_pathname))
70                 if not os.path.exists(target_pathname):
71                     os.symlink(source_pathname, target_pathname)
72                     LOGGER.info(
73                         'LINK {0} to {1}'.format(source_pathname,
74                                                  target_pathname))
75
76 def read_result_list(filename):
77     """
78     Read a file that maps library id to result directory.
79     Does not support spaces in filenames.
80
81     For example:
82       10000 result/foo/bar
83     """
84     stream = open(filename, 'r')
85     results = parse_result_list(stream)
86     stream.close()
87     return results
88
89
90 def parse_result_list(stream):
91     results = []
92     for line in stream:
93         line = line.rstrip()
94         if not line.startswith('#') and len(line) > 0:
95             library_id, result_dir = line.split()
96             results.append((library_id, result_dir))
97     return results