|
| 1 | +/* |
| 2 | + * Copyright 2023, Seqera Labs |
| 3 | + * |
| 4 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | + * you may not use this file except in compliance with the License. |
| 6 | + * You may obtain a copy of the License at |
| 7 | + * |
| 8 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | + * |
| 10 | + * Unless required by applicable law or agreed to in writing, software |
| 11 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | + * See the License for the specific language governing permissions and |
| 14 | + * limitations under the License. |
| 15 | + */ |
| 16 | + |
| 17 | +package nextflow.prov |
| 18 | + |
| 19 | +import java.nio.file.Files |
| 20 | +import java.nio.file.Path |
| 21 | +import java.time.format.DateTimeFormatter |
| 22 | + |
| 23 | +import groovy.json.JsonOutput |
| 24 | +import groovy.transform.CompileStatic |
| 25 | +import nextflow.Session |
| 26 | +import nextflow.exception.AbortOperationException |
| 27 | +import nextflow.processor.TaskRun |
| 28 | +import nextflow.script.WorkflowMetadata |
| 29 | +import nextflow.util.CacheHelper |
| 30 | + |
| 31 | +/** |
| 32 | + * Renderer for the BioCompute Object (BCO) format. |
| 33 | + * |
| 34 | + * @author Ben Sherman <[email protected]> |
| 35 | + */ |
| 36 | +@CompileStatic |
| 37 | +class BcoRenderer implements Renderer { |
| 38 | + |
| 39 | + private URL repository |
| 40 | + |
| 41 | + private String commitId |
| 42 | + |
| 43 | + private String launchDir |
| 44 | + |
| 45 | + private String projectDir |
| 46 | + |
| 47 | + private String workDir |
| 48 | + |
| 49 | + /** |
| 50 | + * Normalize local paths to remove environment-specific directories. |
| 51 | + * |
| 52 | + * @param path |
| 53 | + */ |
| 54 | + private String normalizePath(Path path) { |
| 55 | + normalizePath(path.toUriString()) |
| 56 | + } |
| 57 | + |
| 58 | + private String normalizePath(String path) { |
| 59 | + // replace work directory with relative path |
| 60 | + if( path.startsWith(workDir) ) |
| 61 | + return path.replace(workDir, 'work') |
| 62 | + |
| 63 | + // replace project directory with source URL (if applicable) |
| 64 | + if( repository && path.startsWith(projectDir) ) |
| 65 | + return getProjectSourceUrl(path) |
| 66 | + |
| 67 | + // replace launch directory with relative path |
| 68 | + if( path.startsWith(launchDir) ) |
| 69 | + return path.replace(launchDir + '/', '') |
| 70 | + |
| 71 | + return path |
| 72 | + } |
| 73 | + |
| 74 | + /** |
| 75 | + * Get the source URL for a project asset. |
| 76 | + * |
| 77 | + * @param path |
| 78 | + */ |
| 79 | + private String getProjectSourceUrl(String path) { |
| 80 | + // TODO: add other git providers |
| 81 | + if( repository.host == 'github.com' ) |
| 82 | + return path.replace(projectDir, "${repository}/tree/${commitId}") |
| 83 | + else |
| 84 | + return path |
| 85 | + } |
| 86 | + |
| 87 | + @Override |
| 88 | + void render(Session session, Set<TaskRun> tasks, Map<Path,Path> workflowOutputs, Path path) { |
| 89 | + // get workflow inputs |
| 90 | + final taskLookup = ProvHelper.getTaskLookup(tasks) |
| 91 | + final workflowInputs = ProvHelper.getWorkflowInputs(tasks, taskLookup) |
| 92 | + |
| 93 | + // get workflow metadata |
| 94 | + final metadata = session.workflowMetadata |
| 95 | + final manifest = metadata.manifest |
| 96 | + final nextflowMeta = metadata.nextflow |
| 97 | + this.repository = metadata.repository ? new URL(metadata.repository) : null |
| 98 | + this.commitId = metadata.commitId |
| 99 | + this.projectDir = metadata.projectDir.toUriString() |
| 100 | + this.launchDir = metadata.launchDir.toUriString() |
| 101 | + this.workDir = metadata.workDir.toUriString() |
| 102 | + |
| 103 | + final dateCreated = DateTimeFormatter.ISO_OFFSET_DATE_TIME.format(metadata.start) |
| 104 | + final authors = (manifest.author ?: '').tokenize(',')*.trim() |
| 105 | + final nextflowVersion = nextflowMeta.version.toString() |
| 106 | + final params = session.config.params as Map |
| 107 | + |
| 108 | + // create BCO manifest |
| 109 | + final bco = [ |
| 110 | + "object_id": null, |
| 111 | + "spec_version": null, |
| 112 | + "etag": null, |
| 113 | + "provenance_domain": [ |
| 114 | + "name": manifest.name ?: "", |
| 115 | + "version": manifest.version ?: "", |
| 116 | + "created": dateCreated, |
| 117 | + "modified": dateCreated, |
| 118 | + "contributors": authors.collect( name -> [ |
| 119 | + "contribution": ["authoredBy"], |
| 120 | + "name": name |
| 121 | + ] ), |
| 122 | + "license": "" |
| 123 | + ], |
| 124 | + "usability_domain": [], |
| 125 | + "extension_domain": [], |
| 126 | + "description_domain": [ |
| 127 | + "keywords": [], |
| 128 | + "platform": ["Nextflow"], |
| 129 | + "pipeline_steps": tasks.sort( (task) -> task.id ).collect { task -> [ |
| 130 | + "step_number": task.id, |
| 131 | + "name": task.hash.toString(), |
| 132 | + "description": task.name, |
| 133 | + "input_list": task.getInputFilesMap().collect { name, source -> [ |
| 134 | + "uri": normalizePath(source) |
| 135 | + ] }, |
| 136 | + "output_list": ProvHelper.getTaskOutputs(task).collect { source -> [ |
| 137 | + "uri": normalizePath(source) |
| 138 | + ] } |
| 139 | + ] }, |
| 140 | + ], |
| 141 | + "execution_domain": [ |
| 142 | + "script": [ normalizePath(metadata.scriptFile) ], |
| 143 | + "script_driver": "nextflow", |
| 144 | + "software_prerequisites": [ |
| 145 | + [ |
| 146 | + "name": "Nextflow", |
| 147 | + "version": nextflowVersion, |
| 148 | + "uri": [ |
| 149 | + "uri": "https://github.com/nextflow-io/nextflow/releases/tag/v${nextflowVersion}" |
| 150 | + ] |
| 151 | + ] |
| 152 | + ], |
| 153 | + "external_data_endpoints": [], |
| 154 | + "environment_variables": [:] |
| 155 | + ], |
| 156 | + "parametric_domain": params.collect( (k, v) -> [ |
| 157 | + "param": k, |
| 158 | + "value": normalizePath(v.toString()), |
| 159 | + "step": "0" |
| 160 | + ] ), |
| 161 | + "io_domain": [ |
| 162 | + "input_subdomain": workflowInputs.collect { source -> [ |
| 163 | + "uri": [ |
| 164 | + "uri": normalizePath(source) |
| 165 | + ] |
| 166 | + ] }, |
| 167 | + "output_subdomain": workflowOutputs.collect { source, target -> [ |
| 168 | + "mediatype": Files.probeContentType(source) ?: "", |
| 169 | + "uri": [ |
| 170 | + "filename": normalizePath(source), |
| 171 | + "uri": normalizePath(target) |
| 172 | + ] |
| 173 | + ] } |
| 174 | + ], |
| 175 | + "error_domain": [ |
| 176 | + "empirical_error": [:], |
| 177 | + "algorithmic_error": [:] |
| 178 | + ] |
| 179 | + ] |
| 180 | + |
| 181 | + // append git repository info |
| 182 | + if( metadata.repository ) { |
| 183 | + final extension_domain = bco.extension_domain as List |
| 184 | + extension_domain << [ |
| 185 | + "extension_schema": "https://w3id.org/biocompute/extension_domain/1.1.0/scm/scm_extension.json", |
| 186 | + "scm_extension": [ |
| 187 | + "scm_repository": metadata.repository, |
| 188 | + "scm_type": "git", |
| 189 | + "scm_commit": metadata.commitId, |
| 190 | + "scm_path": metadata.scriptFile.toUriString().replace(projectDir + '/', ''), |
| 191 | + "scm_preview": normalizePath(metadata.scriptFile) |
| 192 | + ] |
| 193 | + ] |
| 194 | + } |
| 195 | + |
| 196 | + // compute etag |
| 197 | + // TODO: make a more canonical hash |
| 198 | + final etag = CacheHelper.hasher(bco, CacheHelper.HashMode.SHA256).hash() |
| 199 | + |
| 200 | + // append non-cacheable fields |
| 201 | + bco.object_id = "urn:uuid:${UUID.randomUUID()}" |
| 202 | + bco.spec_version = "https://w3id.org/ieee/ieee-2791-schema/2791object.json" |
| 203 | + bco.etag = etag.toString() |
| 204 | + |
| 205 | + // render BCO manifest to JSON file |
| 206 | + path.text = JsonOutput.prettyPrint(JsonOutput.toJson(bco)) |
| 207 | + } |
| 208 | + |
| 209 | +} |
0 commit comments