From 846a862513f312d1f638cd4d3313473566f7d330 Mon Sep 17 00:00:00 2001 From: Stephen Kelly Date: Thu, 16 Jul 2020 09:17:48 -0400 Subject: [PATCH] move md5 function to lib dir and symlink to fastqc test dir, rename md5 function --- lib/checksum.groovy | 34 +++++++++++++++++++++++++++++ software/fastqc/test/lib | 1 + software/fastqc/test/main.nf | 42 ++++-------------------------------- 3 files changed, 39 insertions(+), 38 deletions(-) create mode 100644 lib/checksum.groovy create mode 120000 software/fastqc/test/lib diff --git a/lib/checksum.groovy b/lib/checksum.groovy new file mode 100644 index 00000000..53c7334b --- /dev/null +++ b/lib/checksum.groovy @@ -0,0 +1,34 @@ +import java.security.MessageDigest +private static String getMD5(File file) throws IOException +{ + // https://howtodoinjava.com/java/io/how-to-generate-sha-or-md5-file-checksum-hash-in-java/ + //Get file input stream for reading the file content + FileInputStream fis = new FileInputStream(file); + + //Create byte array to read data in chunks + byte[] byteArray = new byte[1024]; + int bytesCount = 0; + + //Read file data and update in message digest + def digest = MessageDigest.getInstance("MD5") + while ((bytesCount = fis.read(byteArray)) != -1) { + digest.update(byteArray, 0, bytesCount); + }; + + //close the stream; We don't need it now. + fis.close(); + + //Get the hash's bytes + byte[] bytes = digest.digest(); + + //This bytes[] has bytes in decimal format; + //Convert it to hexadecimal format + StringBuilder sb = new StringBuilder(); + for(int i=0; i< bytes.length ;i++) + { + sb.append(Integer.toString((bytes[i] & 0xff) + 0x100, 16).substring(1)); + } + + //return complete hash + return sb.toString(); +} diff --git a/software/fastqc/test/lib b/software/fastqc/test/lib new file mode 120000 index 00000000..a5bc7439 --- /dev/null +++ b/software/fastqc/test/lib @@ -0,0 +1 @@ +../../../lib \ No newline at end of file diff --git a/software/fastqc/test/main.nf b/software/fastqc/test/main.nf index 5eeedd35..8618bd9b 100755 --- a/software/fastqc/test/main.nf +++ b/software/fastqc/test/main.nf @@ -1,5 +1,5 @@ #!/usr/bin/env nextflow -import java.security.MessageDigest +import checksum nextflow.preview.dsl = 2 params.out_dir = "test_output" @@ -8,40 +8,6 @@ params.publish_dir_mode = "copy" include { FASTQC } from '../main.nf' - -private static String getFileChecksum(MessageDigest digest, File file) throws IOException -{ - // https://howtodoinjava.com/java/io/how-to-generate-sha-or-md5-file-checksum-hash-in-java/ - //Get file input stream for reading the file content - FileInputStream fis = new FileInputStream(file); - - //Create byte array to read data in chunks - byte[] byteArray = new byte[1024]; - int bytesCount = 0; - - //Read file data and update in message digest - while ((bytesCount = fis.read(byteArray)) != -1) { - digest.update(byteArray, 0, bytesCount); - }; - - //close the stream; We don't need it now. - fis.close(); - - //Get the hash's bytes - byte[] bytes = digest.digest(); - - //This bytes[] has bytes in decimal format; - //Convert it to hexadecimal format - StringBuilder sb = new StringBuilder(); - for(int i=0; i< bytes.length ;i++) - { - sb.append(Integer.toString((bytes[i] & 0xff) + 0x100, 16).substring(1)); - } - - //return complete hash - return sb.toString(); -} - /** * Test if FASTQC runs with single-end data */ @@ -52,7 +18,7 @@ workflow test_single_end { // test that the output looks as expected FASTQC.out.html.map { name, is_single_end, html_file -> - html_hash = getFileChecksum(MessageDigest.getInstance("MD5"), new File("${html_file}")); + html_hash = checksum.getMD5(new File("${html_file}")); assert name == "test_single_end.fastq" assert is_single_end == true @@ -80,8 +46,8 @@ workflow test_paired_end { html_r1 = html_files[0] html_r2 = html_files[1] - html_r1_hash = getFileChecksum(MessageDigest.getInstance("MD5"), new File("${html_r1}")); - html_r2_hash = getFileChecksum(MessageDigest.getInstance("MD5"), new File("${html_r2}")); + html_r1_hash = checksum.getMD5(new File("${html_r1}")); + html_r2_hash = checksum.getMD5(new File("${html_r2}")); assert name == "test_R" assert is_single_end == false