Joining a list of filenames

1,150 views
Skip to first unread message

Phil Ewels

unread,
May 26, 2016, 11:18:04 AM5/26/16
to Nextflow
Hi there,

Apologies for a simple question, but hopefully a simple question. I'm trying to get a list of filenames into an R script. They should be wrapped in double quotes and separated with commas. I think I'm getting close based on FAQ #7 and this part of the docs, but I can't get it to run.

Here's my simplified code - this does work:

#!/usr/bin/env nextflow

Channel
     .fromPath( params.reads )
     .ifEmpty { error "Cannot find any reads matching: ${params.reads}" }
     .set { read_files }
 
read_files.into  { read_files_input }

process sample_correlation {    
    
    input:
    file input_files from read_files_input.toList()
    
    exec:
    println "list <- c(\"${input_files}\")"

}

This outputs the following:

N E X T F L O W  ~  version 0.18.3
Launching test.nf
WARN: Process 'sample_correlation' cannot be executed by 'slurm' executor -- Using 'local' executor instead
[warm up] executor > local
list <- c("human_1_R1.fastq.gz human_2_R1.fastq.gz human_3_R1.fastq.gz human_5_R1.fastq.gz human_4_R1.fastq.gz")
[11/83c378] Submitted process > sample_correlation (1)

However, when I try to get it into a list with quotation marks with the following code (note extra join()):

#!/usr/bin/env nextflow

Channel
     .fromPath( params.reads )
     .ifEmpty { error "Cannot find any reads matching: ${params.reads}" }
     .set { read_files }
 
read_files.into  { read_files_input }

process sample_correlation {    
    
    input:
    file input_files from read_files_input.toList()
    
    exec:
    println "list <- c(\"${input_files.join('", "')}\")"

}


...I get the following error:

N E X T F L O W  ~  version 0.18.3
Launching test.nf
WARN: Process 'sample_correlation' cannot be executed by 'slurm' executor -- Using 'local' executor instead
[warm up] executor > local
[47/ec3662] Submitted process > sample_correlation (1)
Error executing process > 'sample_correlation (1)'

Caused by:
  No signature of method: nextflow.util.BlankSeparatedList.join() is applicable for argument types: (java.lang.String) values: [", "]
Possible solutions: wait(), find(), wait(long), print(java.lang.Object), is(java.lang.Object), find(groovy.lang.Closure)


Source block:
  println "list <- c(\"${input_files.join('", "')}\")"

Work dir:
  /lupus/home/phil/nextflow-test/work/47/ec3662993149ac4cf177f12950ec53

Tip: you can try to figure out what's wrong by changing to the process work dir and showing the script file named: '.command.sh'

 -- Check script 'test.nf' at line: 16 or see '.nextflow.log' file for more details


I'm new to groovy and feeling slightly overwhelmed with the results of my google searches. I'm sure that I'm missing something simple here (probably a typing error?) but can't see a way forward. Does anyone have any suggestions to what feels like it should be a simple question?

Many thanks,

Phil



Paolo Di Tommaso

unread,
May 26, 2016, 11:32:12 AM5/26/16
to nextflow
Hi Phil, 

Quick answer, this works: 

    println "list <- c(\"${(input_files as List).join('", "')}\")"


Long one: Actually your code if fine. The problem is that to make that list bash friendly i.e. printed as blank separated sequence of items by default, under the hood there's an hack that requires that object to be casted as shown to apply regular list methods. 

It's something that soon or later I need to improve. 


Hope it helps. 

Cheers,
Paolo



--
You received this message because you are subscribed to the Google Groups "Nextflow" group.
To unsubscribe from this group and stop receiving emails from it, send an email to nextflow+u...@googlegroups.com.
Visit this group at https://groups.google.com/group/nextflow.
For more options, visit https://groups.google.com/d/optout.

Phil Ewels

unread,
May 26, 2016, 11:38:23 AM5/26/16
to next...@googlegroups.com
Fantastic, that seems to do the job - thanks for the quick answer!

Phil

Paolo Di Tommaso

unread,
May 27, 2016, 11:19:52 AM5/27/16
to nextflow
You are welcome. 

One more thing: you should be aware that when the channel `read_files_input` emits only one item the variable `input_files` isn't a list anymore but just a Path object. 

To make your code more safe I would suggest to add a small helper function like the following one in your code: 


def wrap_items(input_files) {
  def result =  input_files instanceof Path ? input_files.toString() : (input_files as List).join('", "')
  return '"'+result+'"'
}


and use it in your R script. 


Enjoy. 
Paolo

Reply all
Reply to author
Forward
0 new messages