Can a function take and return a channel? I want t...
# nostupidquestions
a
Can a function take and return a channel? I want to make some of my complex channel manipulations more easily testable and that seems like it might be the best option
m
yes
a
Danke
Hmm. The language server doesn’t like this one bit.
m
Can you show some code?
m
a
It specifically doesn't seem to like
tap
or
set
and complained that the values I put in them were variables that were not declared
m
What's your code? I used
tap
recently in an example and it was fine
a
Copy code
def make_samplesheet(input, projDir){
    Channel
        .fromList(
            samplesheetToList(input, "${projDir}/assets/schema_input.json")
        )
        .tap{ ch_samplesheet_intermediate }
        .dump(tag:'samplesheet_raw', pretty:true)
        .map{ meta, samples ->
            validateSampleFileTypes(meta, samples)
        }
        .collect()
        .map{ errs -> error("Validation errors:\n ${errs.join('\n')}\n") }
        .set{ ch_errors }

    // This join allows us to prevent the pipeline from proceeding
    // until all samples have been validated
    ch_samplesheet_intermediate
        .join( ch_errors, remainder:true )
        .map{ sval -> sval[0..-2] }
        .set{ ch_samplesheet }
    
    return ch_samplesheet
}
And the
set
and
tap
variables have e.g.
ch_errors is not defined
as an error
m
I think because it's inside a function you need to locally scope them:
Copy code
def make_samplesheet(input, projDir){
    def ch_samplesheet_intermediate = Channel.fromList(
            samplesheetToList(input, "${projDir}/assets/schema_input.json")
        )
    def ch_errors = ch_samplesheet_intermediate
        .dump(tag:'samplesheet_raw', pretty:true)
        .map{ meta, samples ->
            validateSampleFileTypes(meta, samples)
        }
        .collect()
        .map{ errs -> error("Validation errors:\n ${errs.join('\n')}\n") }

    // This join allows us to prevent the pipeline from proceeding
    // until all samples have been validated
    def ch_samplesheet = ch_samplesheet_intermediate
        .join( ch_errors, remainder:true )
        .map{ sval -> sval[0..-2] }

    ch_samplesheet
}
a
Ok. Not being able to use
tap
is somewhat annoying but hey ho
m
I think Ben wants to remove
tap
and
set
anyway because the syntax above is preferred, possibly because of scoping
a
I never liked
set
but then it is (was?) used quite extensively in docs and across
nf-core
so I sort of got used to it
m
Never used set in my life
😂 1
I hate it with passion
n
Imo set is great because it moves the assignment where it should go: after everything else has is done
a
That's why it's terrible. Assignments at the end is just not natural coming from eg python
I am also an avowed disliker of pipes though. So take that as you will
Its also just generally bad to have multiple ways to do one thing
n
If you are coming from python that should feel quite natural 😉
m
shakefist python shakefist
shakefist 1