annotate 0.6.1/subworkflows/process_fastq.nf @ 11:749faef1caa9

"planemo upload"
author kkonganti
date Tue, 05 Sep 2023 11:51:40 -0400
parents
children
rev   line source
kkonganti@11 1 // Include any necessary methods and modules
kkonganti@11 2 include { stopNow; validateParamsForFASTQ } from "${params.routines}"
kkonganti@11 3 include { GEN_SAMPLESHEET } from "${params.modules}${params.fs}gen_samplesheet${params.fs}main"
kkonganti@11 4 include { SAMPLESHEET_CHECK } from "${params.modules}${params.fs}samplesheet_check${params.fs}main"
kkonganti@11 5 include { CAT_FASTQ } from "${params.modules}${params.fs}cat${params.fs}fastq${params.fs}main"
kkonganti@11 6 include { SEQKIT_SEQ } from "${params.modules}${params.fs}seqkit${params.fs}seq${params.fs}main"
kkonganti@11 7
kkonganti@11 8 // Validate 4 required workflow parameters if
kkonganti@11 9 // FASTQ files are the input for the
kkonganti@11 10 // entry point.
kkonganti@11 11 validateParamsForFASTQ()
kkonganti@11 12
kkonganti@11 13 // Start the subworkflow
kkonganti@11 14 workflow PROCESS_FASTQ {
kkonganti@11 15 main:
kkonganti@11 16 versions = Channel.empty()
kkonganti@11 17 input_ch = Channel.empty()
kkonganti@11 18 reads = Channel.empty()
kkonganti@11 19
kkonganti@11 20 def input = file( (params.input ?: params.metadata) )
kkonganti@11 21
kkonganti@11 22 if (params.input) {
kkonganti@11 23 def fastq_files = []
kkonganti@11 24
kkonganti@11 25 if (params.fq_suffix == null) {
kkonganti@11 26 stopNow("We need to know what suffix the FASTQ files ends with inside the\n" +
kkonganti@11 27 "directory. Please use the --fq_suffix option to indicate the file\n" +
kkonganti@11 28 "suffix by which the files are to be collected to run the pipeline on.")
kkonganti@11 29 }
kkonganti@11 30
kkonganti@11 31 if (params.fq_strandedness == null) {
kkonganti@11 32 stopNow("We need to know if the FASTQ files inside the directory\n" +
kkonganti@11 33 "are sequenced using stranded or non-stranded sequencing. This is generally\n" +
kkonganti@11 34 "required if the sequencing experiment is RNA-SEQ. For almost all of the other\n" +
kkonganti@11 35 "cases, you can probably use the --fq_strandedness unstranded option to indicate\n" +
kkonganti@11 36 "that the reads are unstranded.")
kkonganti@11 37 }
kkonganti@11 38
kkonganti@11 39 if (params.fq_filename_delim == null || params.fq_filename_delim_idx == null) {
kkonganti@11 40 stopNow("We need to know the delimiter of the filename of the FASTQ files.\n" +
kkonganti@11 41 "By default the filename delimiter is _ (underscore). This delimiter character\n" +
kkonganti@11 42 "is used to split and assign a group name. The group name can be controlled by\n" +
kkonganti@11 43 "using the --fq_filename_delim_idx option (1-based). For example, if the FASTQ\n" +
kkonganti@11 44 "filename is WT_REP1_001.fastq, then to create a group WT, use the following\n" +
kkonganti@11 45 "options: --fq_filename_delim _ --fq_filename_delim_idx 1")
kkonganti@11 46 }
kkonganti@11 47
kkonganti@11 48 if (!input.exists()) {
kkonganti@11 49 stopNow("The input directory,\n${params.input}\ndoes not exist!")
kkonganti@11 50 }
kkonganti@11 51
kkonganti@11 52 input.eachFileRecurse {
kkonganti@11 53 it.name.endsWith("${params.fq_suffix}") ? fastq_files << it : fastq_files << null
kkonganti@11 54 }
kkonganti@11 55
kkonganti@11 56 if (fastq_files.findAll{ it != null }.size() == 0) {
kkonganti@11 57 stopNow("The input directory,\n${params.input}\nis empty! or does not " +
kkonganti@11 58 "have FASTQ files ending with the suffix: ${params.fq_suffix}")
kkonganti@11 59 }
kkonganti@11 60
kkonganti@11 61 GEN_SAMPLESHEET( Channel.fromPath(params.input, type: 'dir') )
kkonganti@11 62 GEN_SAMPLESHEET.out.csv.set{ input_ch }
kkonganti@11 63 versions.mix( GEN_SAMPLESHEET.out.versions )
kkonganti@11 64 .set { versions }
kkonganti@11 65 } else if (params.metadata) {
kkonganti@11 66 if (!input.exists()) {
kkonganti@11 67 stopNow("The metadata CSV file,\n${params.metadata}\ndoes not exist!")
kkonganti@11 68 }
kkonganti@11 69
kkonganti@11 70 if (input.size() <= 0) {
kkonganti@11 71 stopNow("The metadata CSV file,\n${params.metadata}\nis empty!")
kkonganti@11 72 }
kkonganti@11 73
kkonganti@11 74 Channel.fromPath(params.metadata, type: 'file')
kkonganti@11 75 .set { input_ch }
kkonganti@11 76 }
kkonganti@11 77
kkonganti@11 78 SAMPLESHEET_CHECK( input_ch )
kkonganti@11 79 .csv
kkonganti@11 80 .splitCsv( header: true, sep: ',')
kkonganti@11 81 .map { create_fastq_channel(it) }
kkonganti@11 82 .groupTuple(by: [0])
kkonganti@11 83 .branch {
kkonganti@11 84 meta, fastq ->
kkonganti@11 85 single : fastq.size() == 1
kkonganti@11 86 return [ meta, fastq.flatten() ]
kkonganti@11 87 multiple : fastq.size() > 1
kkonganti@11 88 return [ meta, fastq.flatten() ]
kkonganti@11 89 }
kkonganti@11 90 .set { reads }
kkonganti@11 91
kkonganti@11 92 CAT_FASTQ( reads.multiple )
kkonganti@11 93 .catted_reads
kkonganti@11 94 .mix( reads.single )
kkonganti@11 95 .set { processed_reads }
kkonganti@11 96
kkonganti@11 97 if (params.fq_filter_by_len.toInteger() > 0) {
kkonganti@11 98 SEQKIT_SEQ( processed_reads )
kkonganti@11 99 .fastx
kkonganti@11 100 .set { processed_reads }
kkonganti@11 101
kkonganti@11 102 versions.mix( SEQKIT_SEQ.out.versions.first().ifEmpty(null) )
kkonganti@11 103 .set { versions }
kkonganti@11 104 }
kkonganti@11 105
kkonganti@11 106 versions.mix(
kkonganti@11 107 SAMPLESHEET_CHECK.out.versions,
kkonganti@11 108 CAT_FASTQ.out.versions.first().ifEmpty(null)
kkonganti@11 109 )
kkonganti@11 110 .set { versions }
kkonganti@11 111
kkonganti@11 112 emit:
kkonganti@11 113 processed_reads
kkonganti@11 114 versions
kkonganti@11 115 }
kkonganti@11 116
kkonganti@11 117 // Function to get list of [ meta, [ fq1, fq2 ] ]
kkonganti@11 118 def create_fastq_channel(LinkedHashMap row) {
kkonganti@11 119
kkonganti@11 120 def meta = [:]
kkonganti@11 121 meta.id = row.sample
kkonganti@11 122 meta.single_end = row.single_end.toBoolean()
kkonganti@11 123 meta.strandedness = row.strandedness
kkonganti@11 124 meta.id = meta.id.split(params.fq_filename_delim)[0..params.fq_filename_delim_idx.toInteger() - 1]
kkonganti@11 125 .join(params.fq_filename_delim)
kkonganti@11 126 meta.id = (meta.id =~ /\./ ? meta.id.take(meta.id.indexOf('.')) : meta.id)
kkonganti@11 127
kkonganti@11 128 def array = []
kkonganti@11 129
kkonganti@11 130 if (!file(row.fq1).exists()) {
kkonganti@11 131 stopNow("Please check input metadata CSV. The following Read 1 FASTQ file does not exist!" +
kkonganti@11 132 "\n${row.fq1}")
kkonganti@11 133 }
kkonganti@11 134 if (meta.single_end) {
kkonganti@11 135 array = [ meta, [ file(row.fq1) ] ]
kkonganti@11 136 } else {
kkonganti@11 137 if (!file(row.fq2).exists()) {
kkonganti@11 138 stopNow("Please check input metadata CSV. The following Read 2 FASTQ file does not exist!" +
kkonganti@11 139 "\n${row.fq2}")
kkonganti@11 140 }
kkonganti@11 141 array = [ meta, [ file(row.fq1), file(row.fq2) ] ]
kkonganti@11 142 }
kkonganti@11 143 return array
kkonganti@11 144 }