7 from StringIO import StringIO
12 from htsworkflow.util.rdfhelp import \
23 from htsworkflow.util.hashfile import make_md5sum
25 logger = logging.getLogger(__name__)
27 DAF_VARIABLE_NAMES = ("variables", "extraVariables")
28 VARIABLES_TERM_NAME = 'variables'
30 class ModelException(RuntimeError):
31 """Assumptions about the RDF model failed"""
35 class MetadataLookupException(RuntimeError):
36 """Problem accessing metadata"""
45 def parse_into_model(model, subject, filename):
46 """Read a DAF into RDF Model
48 requires a subject node to attach statements to
50 attributes = parse(filename)
51 add_to_model(model, attributes, subject)
54 def fromstream_into_model(model, subject, daf_stream):
55 """Load daf stream into model attached to node subject
57 attributes = parse_stream(daf_stream)
58 add_to_model(model, attributes, subject)
61 def fromstring_into_model(model, subject, daf_string):
62 """Read a string containing a DAF into RDF Model
64 requires a short submission name
66 attributes = fromstring(daf_string)
67 add_to_model(model, attributes, subject)
71 """Parse daf from a file
73 stream = open(filename, 'r')
74 attributes = parse_stream(stream)
79 def fromstring(daf_string):
80 """Parse UCSC daf from a provided string"""
81 stream = StringIO(daf_string)
82 return parse_stream(stream)
85 def parse_stream(stream):
86 """Parse UCSC dat stored in a stream"""
87 comment_re = re.compile("#.*$")
90 attributes = {'views': {}}
95 line = comment_re.sub("", line)
96 nstop = _extract_name_index(line)
98 sstop = _consume_whitespace(line, start=nstop)
99 vstop = _extract_value_index(line, start=sstop)
100 value = line[sstop:vstop]
102 if value.lower() in ('yes',):
104 elif value.lower() in ('no',):
108 if view_name is not None:
109 attributes['views'][view_name] = view_attributes
113 elif state == DAF_HEADER and name in DAF_VARIABLE_NAMES:
114 attributes[name] = [x.strip() for x in value.split(',')]
115 elif state == DAF_HEADER and name == 'view':
117 view_attributes['view'] = value
119 elif state == DAF_HEADER:
120 attributes[name] = value
121 elif state == DAF_VIEW:
122 view_attributes[name] = value
125 if view_name is not None:
126 attributes['views'][view_name] = view_attributes
131 def _consume_whitespace(line, start=0):
132 """return index of next non whitespace character
134 returns length of string if it can't find anything
136 for i in xrange(start, len(line)):
137 if line[i] not in string.whitespace:
143 def _extract_name_index(line, start=0):
144 """Used to find end of word by looking for a whitespace character
146 returns length of string if nothing matches
148 for i in xrange(start, len(line)):
149 if line[i] in string.whitespace:
155 def _extract_value_index(line, start=0):
156 """Returns position of last non-whitespace character
158 shortline = line.rstrip()
159 return len(shortline)
162 def convert_to_rdf_statements(attributes, subject):
163 """Convert dictionary of DAF attributes into rdf statements
165 The statements are attached to the provided subject node
167 variables_term = dafTermOntology[VARIABLES_TERM_NAME]
169 for daf_key in attributes:
170 predicate = dafTermOntology[daf_key]
171 if daf_key == 'views':
172 statements.extend(_views_to_statements(subject,
174 attributes[daf_key]))
175 elif daf_key in DAF_VARIABLE_NAMES:
176 for var in attributes.get(daf_key, []):
177 obj = toTypedNode(var)
178 statements.append(RDF.Statement(subject, variables_term, obj))
180 value = attributes[daf_key]
181 obj = toTypedNode(value)
182 statements.append(RDF.Statement(subject, predicate, obj))
187 def _views_to_statements(subject, dafNS, views):
188 """Attach view attributes to new view nodes atached to provided subject
190 viewNS = get_view_namespace(subject)
193 for view_name in views:
194 view_attributes = views[view_name]
195 viewSubject = viewNS[view_name]
196 statements.append(RDF.Statement(subject, dafNS['views'], viewSubject))
198 RDF.Statement(viewSubject, dafNS['name'], toTypedNode(view_name)))
199 for view_attribute_name in view_attributes:
200 predicate = dafNS[view_attribute_name]
201 obj = toTypedNode(view_attributes[view_attribute_name])
202 statements.append(RDF.Statement(viewSubject, predicate, obj))
204 #statements.extend(convert_to_rdf_statements(view, viewNode))
208 def add_to_model(model, attributes, subject):
209 for statement in convert_to_rdf_statements(attributes, subject):
210 model.add_statement(statement)
213 def get_submission_uri(name):
214 return submissionLog[name].uri
217 def submission_uri_to_string(submission_uri):
218 if isinstance(submission_uri, RDF.Node):
219 submission_uri = str(submission_uri.uri)
220 elif isinstance(submission_uri, RDF.Uri):
221 submission_uri = str(submission_uri)
222 if submission_uri[-1] != '/':
223 submission_uri += '/'
224 return submission_uri
227 def get_view_namespace(submission_uri):
228 submission_uri = submission_uri_to_string(submission_uri)
229 view_uri = urlparse.urljoin(submission_uri, 'view/')
230 viewNS = RDF.NS(view_uri)
234 class DAFMapper(object):
235 """Convert filenames to views in the UCSC Daf
237 def __init__(self, name, daf_file=None, model=None):
238 """Construct a RDF backed model of a UCSC DAF
241 name (str): the name of this submission (used to construct DAF url)
242 daf_file (str, stream, or None):
243 if str, use as filename
244 if stream, parse as stream
245 if none, don't attempt to load the DAF into our model
246 model (RDF.Model or None):
247 if None, construct a memory backed model
248 otherwise specifies model to use
250 if daf_file is None and model is None:
251 logger.error("We need a DAF or Model containing a DAF to work")
254 self.submissionSet = get_submission_uri(self.name)
255 self.viewNS = get_view_namespace(self.submissionSet)
257 if model is not None:
260 self.model = get_model()
262 if hasattr(daf_file, 'next'):
263 # its some kind of stream
264 fromstream_into_model(self.model, self.submissionSet, daf_file)
267 parse_into_model(self.model, self.submissionSet, daf_file)
269 self.libraryNS = RDF.NS('http://jumpgate.caltech.edu/library/')
270 self.submissionSetNS = RDF.NS(str(self.submissionSet) + '/')
271 self.__view_map = None
273 def add_pattern(self, view_name, filename_pattern):
274 """Map a filename regular expression to a view name
276 obj = toTypedNode(filename_pattern)
277 self.model.add_statement(
278 RDF.Statement(self.viewNS[view_name],
279 dafTermOntology['filename_re'],
282 def import_submission_dir(self, submission_dir, library_id):
283 """Import a submission directories and update our model as needed
285 #attributes = get_filename_attribute_map(paired)
286 libNode = self.libraryNS[library_id + "/"]
288 self._add_library_details_to_model(libNode)
290 submission_files = os.listdir(submission_dir)
291 for filename in submission_files:
292 self.construct_track_attributes(submission_dir, libNode, filename)
294 def construct_track_attributes(self, submission_dir, libNode, pathname):
295 """Looking for the best extension
296 The 'best' is the longest match
299 filename (str): the filename whose extention we are about to examine
301 path, filename = os.path.split(pathname)
303 logger.debug("Searching for view")
304 view = self.find_view(filename)
306 logger.warn("Unrecognized file: {0}".format(pathname))
308 if str(view) == str(libraryOntology['ignore']):
311 submission_name = self.make_submission_name(submission_dir)
312 submissionNode = self.get_submission_node(submission_dir)
313 submission_uri = str(submissionNode.uri)
314 view_name = fromTypedNode(self.model.get_target(view,
315 dafTermOntology['name']))
316 if view_name is None:
317 errmsg = 'Could not find view name for {0}'
318 logging.warning(errmsg.format(str(view)))
321 view_name = str(view_name)
322 submissionView = RDF.Node(RDF.Uri(submission_uri + '/' + view_name))
324 self.model.add_statement(
325 RDF.Statement(self.submissionSet,
326 dafTermOntology['has_submission'],
328 logger.debug("Adding statements to {0}".format(str(submissionNode)))
329 self.model.add_statement(RDF.Statement(submissionNode,
330 submissionOntology['has_view'],
332 self.model.add_statement(RDF.Statement(submissionNode,
333 submissionOntology['name'],
334 toTypedNode(submission_name)))
335 self.model.add_statement(
336 RDF.Statement(submissionNode,
338 submissionOntology['submission']))
339 self.model.add_statement(RDF.Statement(submissionNode,
340 submissionOntology['library'],
343 logger.debug("Adding statements to {0}".format(str(submissionView)))
344 # add track specific information
345 self.model.add_statement(
346 RDF.Statement(submissionView, dafTermOntology['view'], view))
347 self.model.add_statement(
348 RDF.Statement(submissionView,
349 dafTermOntology['paired'],
350 toTypedNode(self._is_paired(libNode))))
351 self.model.add_statement(
352 RDF.Statement(submissionView,
353 dafTermOntology['submission'],
357 terms = [dafTermOntology['type'],
358 dafTermOntology['filename_re'],
360 terms.extend((dafTermOntology[v] for v in self.get_daf_variables()))
362 # add file specific information
363 self.create_file_attributes(filename, submissionView, submission_uri, submission_dir)
365 logger.debug("Done.")
367 def create_file_attributes(self, filename, submissionView, submission_uri, submission_dir):
368 # add file specific information
369 logger.debug("Updating file md5sum")
370 fileNode = RDF.Node(RDF.Uri(submission_uri + '/' + filename))
371 submission_pathname = os.path.join(submission_dir, filename)
372 self.model.add_statement(
373 RDF.Statement(submissionView,
374 dafTermOntology['has_file'],
376 self.model.add_statement(
377 RDF.Statement(fileNode,
378 dafTermOntology['filename'],
381 md5 = make_md5sum(submission_pathname)
383 errmsg = "Unable to produce md5sum for {0}"
384 logging.warning(errmsg.format(submission_pathname))
386 self.model.add_statement(
387 RDF.Statement(fileNode, dafTermOntology['md5sum'], md5))
389 def _add_library_details_to_model(self, libNode):
390 parser = RDF.Parser(name='rdfa')
391 new_statements = parser.parse_as_stream(libNode.uri)
392 for s in new_statements:
393 # don't override things we already have in the model
394 targets = list(self.model.get_targets(s.subject, s.predicate))
395 if len(targets) == 0:
398 def get_daf_variables(self):
399 """Returns simple variables names that to include in the ddf
401 variables_term = dafTermOntology[VARIABLES_TERM_NAME]
403 if self.need_replicate():
404 results.append('replicate')
406 for obj in self.model.get_targets(self.submissionSet, variables_term):
407 value = str(fromTypedNode(obj))
408 results.append(value)
409 results.append('labVersion')
412 def make_submission_name(self, submission_dir):
413 submission_dir = os.path.normpath(submission_dir)
414 submission_dir_name = os.path.split(submission_dir)[1]
415 if len(submission_dir_name) == 0:
417 "Submission dir name too short: {0}".format(submission_dir))
418 return submission_dir_name
420 def get_submission_node(self, submission_dir):
421 """Convert a submission directory name to a submission node
423 submission_name = self.make_submission_name(submission_dir)
424 return self.submissionSetNS[submission_name]
426 def _get_library_attribute(self, libNode, attribute):
427 if not isinstance(attribute, RDF.Node):
428 attribute = libraryOntology[attribute]
430 targets = list(self.model.get_targets(libNode, attribute))
432 return self._format_library_attribute(targets)
436 #targets = self._search_same_as(libNode, attribute)
437 #if targets is not None:
438 # return self._format_library_attribute(targets)
440 # we don't know anything about this attribute
441 self._add_library_details_to_model(libNode)
443 targets = list(self.model.get_targets(libNode, attribute))
445 return self._format_library_attribute(targets)
449 def _format_library_attribute(self, targets):
450 if len(targets) == 0:
452 elif len(targets) == 1:
453 return fromTypedNode(targets[0])
454 elif len(targets) > 1:
455 return [fromTypedNode(t) for t in targets]
457 def _search_same_as(self, subject, predicate):
458 # look for alternate names
459 other_predicates = self.model.get_targets(predicate, owlNS['sameAs'])
460 for other in other_predicates:
461 targets = list(self.model.get_targets(subject, other))
466 def find_view(self, filename):
467 """Search through potential DAF filename patterns
469 if self.__view_map is None:
470 self.__view_map = self._get_filename_view_map()
473 for pattern, view in self.__view_map.items():
474 if re.match(pattern, filename):
478 msg = "%s matched multiple views %s" % (
480 [str(x) for x in results])
481 raise ModelException(msg)
482 elif len(results) == 1:
487 def get_view_name(self, view):
488 view_term = submissionOntology['view_name']
489 names = list(self.model.get_targets(view, view_term))
491 return fromTypedNode(names[0])
493 msg = "Found wrong number of view names for {0} len = {1}"
494 msg = msg.format(str(view), len(names))
496 raise RuntimeError(msg)
498 def _get_filename_view_map(self):
499 """Query our model for filename patterns
501 return a dictionary of compiled regular expressions to view names
503 filename_query = RDF.Statement(
504 None, dafTermOntology['filename_re'], None)
507 for s in self.model.find_statements(filename_query):
508 view_name = s.subject
509 literal_re = s.object.literal_value['string']
510 logger.debug("Found: %s" % (literal_re,))
512 filename_re = re.compile(literal_re)
514 logger.error("Unable to compile: %s" % (literal_re,))
515 patterns[literal_re] = view_name
518 def _get_library_url(self):
519 return str(self.libraryNS[''].uri)
521 def _set_library_url(self, value):
522 self.libraryNS = RDF.NS(str(value))
524 library_url = property(_get_library_url, _set_library_url)
526 def _is_paired(self, libNode):
527 """Determine if a library is paired end"""
528 library_type = self._get_library_attribute(libNode, 'library_type')
529 if library_type is None:
530 errmsg = "%s doesn't have a library type"
531 raise ModelException(errmsg % (str(libNode),))
534 single = ['Single End', 'Small RNA', 'CSHL (lacking last nt)']
535 paired = ['Paired End', 'Multiplexing', 'Barcoded']
536 if library_type in single:
538 elif library_type in paired:
541 raise MetadataLookupException(
542 "Unrecognized library type %s for %s" % \
543 (library_type, str(libNode)))
545 def need_replicate(self):
546 viewTerm = dafTermOntology['views']
547 replicateTerm = dafTermOntology['hasReplicates']
549 views = self.model.get_targets(self.submissionSet, viewTerm)
552 replicate = self.model.get_target(view, replicateTerm)
553 if fromTypedNode(replicate):