annotate 0.5.0/subworkflows/process_fastq.nf @ 0:97cd2f532efe

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