5 from pprint import pformat
8 from StringIO import StringIO
13 from htsworkflow.util.rdfhelp import \
25 from htsworkflow.util.hashfile import make_md5sum
27 logger = logging.getLogger(__name__)
29 DAF_VARIABLE_NAMES = ("variables", "extraVariables")
30 VARIABLES_TERM_NAME = 'variables'
31 DAF_PRE_VARIABLES = ['files', 'view']
32 DAF_POST_VARIABLES = [ 'labExpId', 'md5sum']
35 class ModelException(RuntimeError):
36 """Assumptions about the RDF model failed"""
40 class MetadataLookupException(RuntimeError):
41 """Problem accessing metadata"""
50 def parse_into_model(model, subject, filename):
51 """Read a DAF into RDF Model
53 requires a subject node to attach statements to
55 attributes = parse(filename)
56 add_to_model(model, attributes, subject)
59 def fromstream_into_model(model, subject, daf_stream):
60 """Load daf stream into model attached to node subject
62 attributes = parse_stream(daf_stream)
63 add_to_model(model, attributes, subject)
66 def fromstring_into_model(model, subject, daf_string):
67 """Read a string containing a DAF into RDF Model
69 requires a short submission name
71 attributes = fromstring(daf_string)
72 add_to_model(model, attributes, subject)
76 """Parse daf from a file
78 stream = open(filename, 'r')
79 attributes = parse_stream(stream)
84 def fromstring(daf_string):
85 """Parse UCSC daf from a provided string"""
86 stream = StringIO(daf_string)
87 return parse_stream(stream)
90 def parse_stream(stream):
91 """Parse UCSC dat stored in a stream"""
92 comment_re = re.compile("#.*$")
95 attributes = {'views': {}}
100 line = comment_re.sub("", line)
101 nstop = _extract_name_index(line)
103 sstop = _consume_whitespace(line, start=nstop)
104 vstop = _extract_value_index(line, start=sstop)
105 value = line[sstop:vstop]
107 if value.lower() in ('yes',):
109 elif value.lower() in ('no',):
113 if view_name is not None:
114 attributes['views'][view_name] = view_attributes
118 elif state == DAF_HEADER and name in DAF_VARIABLE_NAMES:
119 attributes[name] = [x.strip() for x in value.split(',')]
120 elif state == DAF_HEADER and name == 'view':
122 view_attributes['view'] = value
124 elif state == DAF_HEADER:
125 attributes[name] = value
126 elif state == DAF_VIEW:
127 view_attributes[name] = value
130 if view_name is not None:
131 attributes['views'][view_name] = view_attributes
133 logger.debug("DAF Attributes" + pformat(attributes))
137 def _consume_whitespace(line, start=0):
138 """return index of next non whitespace character
140 returns length of string if it can't find anything
142 for i in xrange(start, len(line)):
143 if line[i] not in string.whitespace:
149 def _extract_name_index(line, start=0):
150 """Used to find end of word by looking for a whitespace character
152 returns length of string if nothing matches
154 for i in xrange(start, len(line)):
155 if line[i] in string.whitespace:
161 def _extract_value_index(line, start=0):
162 """Returns position of last non-whitespace character
164 shortline = line.rstrip()
165 return len(shortline)
168 def convert_to_rdf_statements(attributes, subject):
169 """Convert dictionary of DAF attributes into rdf statements
171 The statements are attached to the provided subject node
173 variables_term = dafTermOntology[VARIABLES_TERM_NAME]
175 for daf_key in attributes:
176 predicate = dafTermOntology[daf_key]
177 if daf_key == 'views':
178 statements.extend(_views_to_statements(subject,
180 attributes[daf_key]))
181 elif daf_key in DAF_VARIABLE_NAMES:
182 for var in attributes.get(daf_key, []):
183 obj = toTypedNode(var)
184 statements.append(RDF.Statement(subject, variables_term, obj))
186 value = attributes[daf_key]
187 obj = toTypedNode(value)
188 statements.append(RDF.Statement(subject, predicate, obj))
193 def _views_to_statements(subject, dafNS, views):
194 """Attach view attributes to new view nodes atached to provided subject
196 viewNS = get_view_namespace(subject)
199 for view_name in views:
200 view_attributes = views[view_name]
201 viewSubject = viewNS[view_name]
202 statements.append(RDF.Statement(subject, dafNS['views'], viewSubject))
204 RDF.Statement(viewSubject, dafNS['name'], toTypedNode(view_name)))
205 for view_attribute_name in view_attributes:
206 predicate = dafNS[view_attribute_name]
207 obj = toTypedNode(view_attributes[view_attribute_name])
208 statements.append(RDF.Statement(viewSubject, predicate, obj))
210 #statements.extend(convert_to_rdf_statements(view, viewNode))
214 def add_to_model(model, attributes, subject):
215 for statement in convert_to_rdf_statements(attributes, subject):
216 model.add_statement(statement)
219 def get_submission_uri(name):
220 return submissionLog[name].uri
223 def submission_uri_to_string(submission_uri):
224 if isinstance(submission_uri, RDF.Node):
225 submission_uri = str(submission_uri.uri)
226 elif isinstance(submission_uri, RDF.Uri):
227 submission_uri = str(submission_uri)
228 if submission_uri[-1] != '/':
229 submission_uri += '/'
230 return submission_uri
233 def get_view_namespace(submission_uri):
234 submission_uri = submission_uri_to_string(submission_uri)
235 view_uri = urlparse.urljoin(submission_uri, 'view/')
236 viewNS = RDF.NS(view_uri)
240 class DAFMapper(object):
241 """Convert filenames to views in the UCSC Daf
243 def __init__(self, name, daf_file=None, model=None):
244 """Construct a RDF backed model of a UCSC DAF
247 name (str): the name of this submission (used to construct DAF url)
248 daf_file (str, stream, or None):
249 if str, use as filename
250 if stream, parse as stream
251 if none, don't attempt to load the DAF into our model
252 model (RDF.Model or None):
253 if None, construct a memory backed model
254 otherwise specifies model to use
256 if daf_file is None and model is None:
257 logger.error("We need a DAF or Model containing a DAF to work")
260 self.submissionSet = get_submission_uri(self.name)
261 self.viewNS = get_view_namespace(self.submissionSet)
263 if model is not None:
266 self.model = get_model()
268 if hasattr(daf_file, 'next'):
269 # its some kind of stream
270 fromstream_into_model(self.model, self.submissionSet, daf_file)
273 parse_into_model(self.model, self.submissionSet, daf_file)
275 self.libraryNS = RDF.NS('http://jumpgate.caltech.edu/library/')
276 self.submissionSetNS = RDF.NS(str(self.submissionSet) + '/')
277 self.__view_map = None
279 def add_pattern(self, view_name, filename_pattern):
280 """Map a filename regular expression to a view name
282 obj = toTypedNode(filename_pattern)
283 self.model.add_statement(
284 RDF.Statement(self.viewNS[view_name],
285 dafTermOntology['filename_re'],
288 def import_submission_dir(self, submission_dir, library_id):
289 """Import a submission directories and update our model as needed
291 #attributes = get_filename_attribute_map(paired)
292 libNode = self.libraryNS[library_id + "/"]
294 self._add_library_details_to_model(libNode)
296 submission_files = os.listdir(submission_dir)
297 for filename in submission_files:
298 self.construct_track_attributes(submission_dir, libNode, filename)
300 def construct_track_attributes(self, submission_dir, libNode, pathname):
301 """Looking for the best extension
302 The 'best' is the longest match
305 filename (str): the filename whose extention we are about to examine
307 path, filename = os.path.split(pathname)
309 logger.debug("Searching for view")
310 view = self.find_view(filename)
312 logger.warn("Unrecognized file: {0}".format(pathname))
314 if str(view) == str(libraryOntology['ignore']):
317 submission_name = self.make_submission_name(submission_dir)
318 submissionNode = self.get_submission_node(submission_dir)
319 submission_uri = str(submissionNode.uri)
320 view_name = fromTypedNode(self.model.get_target(view,
321 dafTermOntology['name']))
322 if view_name is None:
323 errmsg = 'Could not find view name for {0}'
324 logger.warning(errmsg.format(str(view)))
327 view_name = str(view_name)
328 submissionView = RDF.Node(RDF.Uri(submission_uri + '/' + view_name))
330 self.model.add_statement(
331 RDF.Statement(self.submissionSet,
332 dafTermOntology['has_submission'],
334 logger.debug("Adding statements to {0}".format(str(submissionNode)))
335 self.model.add_statement(RDF.Statement(submissionNode,
336 submissionOntology['has_view'],
338 self.model.add_statement(RDF.Statement(submissionNode,
339 submissionOntology['name'],
340 toTypedNode(submission_name)))
341 self.model.add_statement(
342 RDF.Statement(submissionNode,
344 submissionOntology['submission']))
345 self.model.add_statement(RDF.Statement(submissionNode,
346 submissionOntology['library'],
349 logger.debug("Adding statements to {0}".format(str(submissionView)))
350 # add track specific information
351 self.model.add_statement(
352 RDF.Statement(submissionView, dafTermOntology['view'], view))
353 self.model.add_statement(
354 RDF.Statement(submissionView,
355 dafTermOntology['paired'],
356 toTypedNode(self._is_paired(libNode))))
357 self.model.add_statement(
358 RDF.Statement(submissionView,
359 dafTermOntology['submission'],
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 logger.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 results.extend([v for v in DAF_PRE_VARIABLES if v not in results])
404 results = DAF_PRE_VARIABLES[:]
405 if self.need_replicate() and 'replicate' not in results:
406 results.append('replicate')
408 for obj in self.model.get_targets(self.submissionSet, variables_term):
409 value = str(fromTypedNode(obj))
410 if value not in results:
411 results.append(value)
412 results.extend([v for v in DAF_POST_VARIABLES if v not in results])
415 def make_submission_name(self, submission_dir):
416 submission_dir = os.path.normpath(submission_dir)
417 submission_dir_name = os.path.split(submission_dir)[1]
418 if len(submission_dir_name) == 0:
420 "Submission dir name too short: {0}".format(submission_dir))
421 return submission_dir_name
423 def get_submission_node(self, submission_dir):
424 """Convert a submission directory name to a submission node
426 submission_name = self.make_submission_name(submission_dir)
427 return self.submissionSetNS[submission_name]
429 def _get_library_attribute(self, libNode, attribute):
430 if not isinstance(attribute, RDF.Node):
431 attribute = libraryOntology[attribute]
433 targets = list(self.model.get_targets(libNode, attribute))
435 return self._format_library_attribute(targets)
439 #targets = self._search_same_as(libNode, attribute)
440 #if targets is not None:
441 # return self._format_library_attribute(targets)
443 # we don't know anything about this attribute
444 self._add_library_details_to_model(libNode)
446 targets = list(self.model.get_targets(libNode, attribute))
448 return self._format_library_attribute(targets)
452 def _format_library_attribute(self, targets):
453 if len(targets) == 0:
455 elif len(targets) == 1:
456 return fromTypedNode(targets[0])
457 elif len(targets) > 1:
458 return [fromTypedNode(t) for t in targets]
460 def _search_same_as(self, subject, predicate):
461 # look for alternate names
462 other_predicates = self.model.get_targets(predicate, owlNS['sameAs'])
463 for other in other_predicates:
464 targets = list(self.model.get_targets(subject, other))
469 def find_view(self, filename):
470 """Search through potential DAF filename patterns
472 if self.__view_map is None:
473 self.__view_map = self._get_filename_view_map()
476 for pattern, view in self.__view_map.items():
477 if re.match(pattern, filename):
481 msg = "%s matched multiple views %s" % (
483 [str(x) for x in results])
484 raise ModelException(msg)
485 elif len(results) == 1:
490 def get_view_name(self, view):
491 view_term = submissionOntology['view_name']
492 names = list(self.model.get_targets(view, view_term))
494 return fromTypedNode(names[0])
496 msg = "Found wrong number of view names for {0} len = {1}"
497 msg = msg.format(str(view), len(names))
499 raise RuntimeError(msg)
501 def _get_filename_view_map(self):
502 """Query our model for filename patterns
504 return a dictionary of compiled regular expressions to view names
506 filename_query = RDF.Statement(
507 None, dafTermOntology['filename_re'], None)
510 for s in self.model.find_statements(filename_query):
511 view_name = s.subject
512 literal_re = s.object.literal_value['string']
513 logger.debug("Found: %s" % (literal_re,))
515 filename_re = re.compile(literal_re)
517 logger.error("Unable to compile: %s" % (literal_re,))
518 patterns[literal_re] = view_name
521 def _get_library_url(self):
522 return str(self.libraryNS[''].uri)
524 def _set_library_url(self, value):
525 self.libraryNS = RDF.NS(str(value))
527 library_url = property(_get_library_url, _set_library_url)
529 def _is_paired(self, libNode):
530 """Determine if a library is paired end"""
531 library_type = self._get_library_attribute(libNode, 'library_type')
532 if library_type is None:
533 errmsg = "%s doesn't have a library type"
534 raise ModelException(errmsg % (str(libNode),))
536 single = ['CSHL (lacking last nt)',
537 'Single End (non-multiplexed)',
538 'Small RNA (non-multiplexed)',]
539 paired = ['Barcoded Illumina',
542 'Paired End (non-multiplexed)',]
543 if library_type in single:
545 elif library_type in paired:
548 raise MetadataLookupException(
549 "Unrecognized library type %s for %s" % \
550 (library_type, str(libNode)))
552 def need_replicate(self):
553 viewTerm = dafTermOntology['views']
554 replicateTerm = dafTermOntology['hasReplicates']
556 views = self.model.get_targets(self.submissionSet, viewTerm)
559 replicate = self.model.get_target(view, replicateTerm)
560 if fromTypedNode(replicate):
565 if __name__ == "__main__":
566 example_daf = """# Lab and general info
570 variables cell, antibody,sex,age,strain,control
571 compositeSuffix CaltechHistone
574 validationSettings validateFiles.bam:mismatches=2,bamPercent=99.9;validateFiles.fastq:quick=1000
576 # Track/view definition
578 longLabelPrefix Caltech Fastq Read 1
584 longLabelPrefix Caltech Histone Signal
590 example_daf_stream = StringIO(example_daf)
592 mapper = DAFMapper(name, daf_file = example_daf_stream, model=model)