annotate 0.7.0/subworkflows/process_fastq.nf @ 17:0e7a0053e4a6

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