Skip to content

Commit

Permalink
Merge pull request #16 from maxulysse/getRelativePath
Browse files Browse the repository at this point in the history
Add getRelativePath() function
  • Loading branch information
maxulysse authored Oct 1, 2024
2 parents 8494f78 + 1e35b22 commit 56d5054
Show file tree
Hide file tree
Showing 5 changed files with 146 additions and 0 deletions.
66 changes: 66 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,72 @@ assert snapshot(

First argument is the pipeline `outdir` directory path, second is a boolean to include folders, and the third is a list of glob patterns to ignore, and the fourth is a file containing a list of glob patterns to ignore.

## `getRelativePath()`

This function is used to get the relative path from a list of files compared to a given directory.

```bash
results/
├── pipeline_info
│   └── execution_trace_2024-09-30_13-10-16.txt
└── stable
├── stable_content.txt
└── stable_name.txt

2 directories, 3 files
```

Following the previous example, we want to get the relative path of the stable paths in the `results` directory.

```groovy
def stable_name = getAllFilesFromDir(params.outdir, true, ['pipeline_info/execution_*.{html,txt}'], null )
```

The `stable_name` variable contains the list of stable files and folders in the `results` directory.

```groovy
assert snapshot(
getRelativePath(stable_name, outputDir)
).match()
```

By using `getRelativePath()` we generate in the snapshot:

```text
"content": [
[
"pipeline_info",
"stable",
"stable/stable_content.txt",
"stable/stable_name.txt"
]
]
```

A reduced list can be generated by using the `getAllFilesFromDir()` not to output the folders.

```text
"content": [
[
"stable/stable_content.txt",
"stable/stable_name.txt"
]
]
```

While only a flat structure would be generated without using `getRelativePath()` and using `*.name` to capture the file names.

```text
"content": [
[
"pipeline_info",
"stable",
"stable_content.txt",
"stable_name.txt"
]
]
```

## Credits

nft-utils was created by the nf-core community.
Expand Down
11 changes: 11 additions & 0 deletions src/main/java/nf_core/nf/test/utils/Methods.java
Original file line number Diff line number Diff line change
Expand Up @@ -110,4 +110,15 @@ private static List<String> readGlobsFromFile(String filePath) throws IOExceptio
}
return globs;
}

public static List<String> getRelativePath(List<File> filePaths, String baseDir) {
Path basePath = Paths.get(baseDir).toAbsolutePath().normalize();

return filePaths.stream()
.map(filePath -> {
Path path = Paths.get(filePath.toURI()).toAbsolutePath().normalize();
return basePath.relativize(path).toString();
})
.collect(Collectors.toList());
}
}
23 changes: 23 additions & 0 deletions tests/getRelativePath/main.nf
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
workflow {

def trace_timestamp = new java.util.Date().format( 'yyyy-MM-dd_HH-mm-ss')

ch_stable_content = Channel.of(
"""
I HAVE STABLE CONTENT
""".stripIndent().trim())
.collectFile(storeDir: "${params.outdir}/stable", name: 'stable_content.txt', sort: true, newLine: true)

ch_stable_name = Channel.of(
"""
I DO NOT HAVE STABLE CONTENT
${trace_timestamp}
""".stripIndent().trim())
.collectFile(storeDir: "${params.outdir}/stable", name: 'stable_name.txt', sort: true, newLine: true)

ch_unstable_name = Channel.of(
"""
I DO NOT HAVE STABLE NAME
""".stripIndent().trim())
.collectFile(storeDir: "${params.outdir}/pipeline_info", name: "execution_trace_${trace_timestamp}.txt", sort: true, newLine: true)
}
25 changes: 25 additions & 0 deletions tests/getRelativePath/main.nf.test
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
nextflow_pipeline {

name "Test getRelativePath"
script "./main.nf"
tag "getRelativePath"

test("getRelativePath") {
when {
params {
outdir = "$outputDir"
}
}

then {
// Use getAllFilesFromDir() to get a list of all files and folders from the output directory, minus the timestamped files
def stable_name = getAllFilesFromDir(params.outdir, true, ['pipeline_info/execution_*.{html,txt}'], null )
def stable_file = getAllFilesFromDir(params.outdir, false, ['pipeline_info/execution_*.{html,txt}'], null )
assert snapshot(
// Use getRelativePath to get the relative path starting from outputDir
getRelativePath(stable_name, outputDir),
getRelativePath(stable_file, outputDir)
).match()
}
}
}
21 changes: 21 additions & 0 deletions tests/getRelativePath/main.nf.test.snap
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
{
"getRelativePath": {
"content": [
[
"pipeline_info",
"stable",
"stable/stable_content.txt",
"stable/stable_name.txt"
],
[
"stable/stable_content.txt",
"stable/stable_name.txt"
]
],
"meta": {
"nf-test": "0.9.0",
"nextflow": "24.04.4"
},
"timestamp": "2024-10-01T18:20:38.379963"
}
}

0 comments on commit 56d5054

Please sign in to comment.