annotate 0.4.0/subworkflows/process_fastq.nf @ 101:ce6d9548fe89

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