Skip to content

Commit

Permalink
Merge pull request #26 from nvnieuwk/add-md5-functions
Browse files Browse the repository at this point in the history
Add `listToMD5`
  • Loading branch information
maxulysse authored Nov 22, 2024
2 parents 49f194f + 88217f4 commit eba1583
Show file tree
Hide file tree
Showing 7 changed files with 72 additions and 1 deletion.
2 changes: 1 addition & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

### New features

The first release of nft-utils
- Added the `listToMD5` function

### Fixes

Expand Down
6 changes: 6 additions & 0 deletions docs/usage.md
Original file line number Diff line number Diff line change
Expand Up @@ -196,3 +196,9 @@ Without using `getRelativePath()` and by using `*.name` to capture the file name
def stable_name = getAllFilesFromDir(params.outdir, relative: true, ignore: ['pipeline_info/execution_*.{html,txt}'] )
def stable_name_again = getAllFilesFromDir(params.outdir, relative: true, include: ['stable/*'] )
```

## `listToMD5()`

This function takes a list of values as input and converts the sequence to a MD5 hash. All values in the list should be of a type that can be converted to a string, otherwise the function will fail.

A common use case for this function could be to read a file, remove all unstable lines from it and regerenate an MD5 hash.
5 changes: 5 additions & 0 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,11 @@
<artifactId>snakeyaml</artifactId>
<version>2.3</version>
</dependency>
<dependency>
<groupId>com.joyent.util</groupId>
<artifactId>fast-md5</artifactId>
<version>2.7.1</version>
</dependency>
</dependencies>

<build>
Expand Down
12 changes: 12 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 @@ -4,6 +4,7 @@
import java.io.File;
import java.io.FileReader;
import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.nio.file.FileSystems;
import java.nio.file.FileVisitResult;
import java.nio.file.Files;
Expand All @@ -16,6 +17,8 @@
import java.util.stream.Collectors;
import org.yaml.snakeyaml.Yaml;

import com.twmacinta.util.MD5;

public class Methods {

// Read a Version YAML file and return a Map of Map
Expand Down Expand Up @@ -184,4 +187,13 @@ public static List<String> getRelativePath(List<File> filePaths, String baseDir)
})
.collect(Collectors.toList());
}

public static String listToMD5(ArrayList<Object> input) throws UnsupportedEncodingException {
MD5 md5 = new MD5();
Iterator<Object> inputIterator = input.iterator();
while(inputIterator.hasNext()) {
md5.Update(inputIterator.next().toString(), null);
}
return md5.asHex();
}
}
16 changes: 16 additions & 0 deletions tests/listToMD5/main.nf
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
workflow TEST {
main:
def fill = "This needs to be filled in"
def lines = [
"This is a line",
32,
453.2,
"""
Wow, a multiline string
""",
"${fill}"
]

emit:
lines_out = lines
}
18 changes: 18 additions & 0 deletions tests/listToMD5/main.nf.test
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
nextflow_workflow {

name "Test Workflow TEST"
script "./main.nf"
workflow "TEST"

tag "listToMD5"

test("Should run without failures") {

then {
assert workflow.success
assert snapshot(workflow.out.lines_out.collect { listToMD5(it) }).match()
}

}

}
14 changes: 14 additions & 0 deletions tests/listToMD5/main.nf.test.snap
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
{
"Should run without failures": {
"content": [
[
"a2112ea5e55a647ce81b27810d0d1070"
]
],
"meta": {
"nf-test": "0.9.0",
"nextflow": "24.10.0"
},
"timestamp": "2024-10-28T14:22:04.02598768"
}
}

0 comments on commit eba1583

Please sign in to comment.