kkonganti@1: // Include any necessary methods and modules kkonganti@1: include { stopNow; validateParamsForFASTQ } from "${params.routines}" kkonganti@1: include { GEN_SAMPLESHEET } from "${params.modules}${params.fs}gen_samplesheet${params.fs}main" kkonganti@1: include { SAMPLESHEET_CHECK } from "${params.modules}${params.fs}samplesheet_check${params.fs}main" kkonganti@1: include { CAT_FASTQ } from "${params.modules}${params.fs}cat${params.fs}fastq${params.fs}main" kkonganti@1: include { SEQKIT_SEQ } from "${params.modules}${params.fs}seqkit${params.fs}seq${params.fs}main" kkonganti@1: kkonganti@1: // Validate 4 required workflow parameters if kkonganti@1: // FASTQ files are the input for the kkonganti@1: // entry point. kkonganti@1: validateParamsForFASTQ() kkonganti@1: kkonganti@1: // Start the subworkflow kkonganti@1: workflow PROCESS_FASTQ { kkonganti@1: main: kkonganti@1: versions = Channel.empty() kkonganti@1: input_ch = Channel.empty() kkonganti@1: reads = Channel.empty() kkonganti@1: kkonganti@1: def input = file( (params.input ?: params.metadata) ) kkonganti@1: kkonganti@1: if (params.input) { kkonganti@1: def fastq_files = [] kkonganti@1: kkonganti@1: if (params.fq_suffix == null) { kkonganti@1: stopNow("We need to know what suffix the FASTQ files ends with inside the\n" + kkonganti@1: "directory. Please use the --fq_suffix option to indicate the file\n" + kkonganti@1: "suffix by which the files are to be collected to run the pipeline on.") kkonganti@1: } kkonganti@1: kkonganti@1: if (params.fq_strandedness == null) { kkonganti@1: stopNow("We need to know if the FASTQ files inside the directory\n" + kkonganti@1: "are sequenced using stranded or non-stranded sequencing. This is generally\n" + kkonganti@1: "required if the sequencing experiment is RNA-SEQ. For almost all of the other\n" + kkonganti@1: "cases, you can probably use the --fq_strandedness unstranded option to indicate\n" + kkonganti@1: "that the reads are unstranded.") kkonganti@1: } kkonganti@1: kkonganti@1: if (params.fq_filename_delim == null || params.fq_filename_delim_idx == null) { kkonganti@1: stopNow("We need to know the delimiter of the filename of the FASTQ files.\n" + kkonganti@1: "By default the filename delimiter is _ (underscore). This delimiter character\n" + kkonganti@1: "is used to split and assign a group name. The group name can be controlled by\n" + kkonganti@1: "using the --fq_filename_delim_idx option (1-based). For example, if the FASTQ\n" + kkonganti@1: "filename is WT_REP1_001.fastq, then to create a group WT, use the following\n" + kkonganti@1: "options: --fq_filename_delim _ --fq_filename_delim_idx 1") kkonganti@1: } kkonganti@1: kkonganti@1: if (!input.exists()) { kkonganti@1: stopNow("The input directory,\n${params.input}\ndoes not exist!") kkonganti@1: } kkonganti@1: kkonganti@1: input.eachFileRecurse { kkonganti@1: it.name.endsWith("${params.fq_suffix}") ? fastq_files << it : fastq_files << null kkonganti@1: } kkonganti@1: kkonganti@1: if (fastq_files.findAll{ it != null }.size() == 0) { kkonganti@1: stopNow("The input directory,\n${params.input}\nis empty! or does not " + kkonganti@1: "have FASTQ files ending with the suffix: ${params.fq_suffix}") kkonganti@1: } kkonganti@1: kkonganti@1: GEN_SAMPLESHEET( Channel.fromPath(params.input, type: 'dir') ) kkonganti@1: GEN_SAMPLESHEET.out.csv.set{ input_ch } kkonganti@1: versions.mix( GEN_SAMPLESHEET.out.versions ) kkonganti@1: .set { versions } kkonganti@1: } else if (params.metadata) { kkonganti@1: if (!input.exists()) { kkonganti@1: stopNow("The metadata CSV file,\n${params.metadata}\ndoes not exist!") kkonganti@1: } kkonganti@1: kkonganti@1: if (input.size() <= 0) { kkonganti@1: stopNow("The metadata CSV file,\n${params.metadata}\nis empty!") kkonganti@1: } kkonganti@1: kkonganti@1: Channel.fromPath(params.metadata, type: 'file') kkonganti@1: .set { input_ch } kkonganti@1: } kkonganti@1: kkonganti@1: SAMPLESHEET_CHECK( input_ch ) kkonganti@1: .csv kkonganti@1: .splitCsv( header: true, sep: ',') kkonganti@1: .map { create_fastq_channel(it) } kkonganti@1: .groupTuple(by: [0]) kkonganti@1: .branch { kkonganti@1: meta, fastq -> kkonganti@1: single : fastq.size() == 1 kkonganti@1: return [ meta, fastq.flatten() ] kkonganti@1: multiple : fastq.size() > 1 kkonganti@1: return [ meta, fastq.flatten() ] kkonganti@1: } kkonganti@1: .set { reads } kkonganti@1: kkonganti@1: CAT_FASTQ( reads.multiple ) kkonganti@1: .catted_reads kkonganti@1: .mix( reads.single ) kkonganti@1: .set { processed_reads } kkonganti@1: kkonganti@1: if (params.fq_filter_by_len.toInteger() > 0) { kkonganti@1: SEQKIT_SEQ( processed_reads ) kkonganti@1: .fastx kkonganti@1: .set { processed_reads } kkonganti@1: kkonganti@1: versions.mix( SEQKIT_SEQ.out.versions.first().ifEmpty(null) ) kkonganti@1: .set { versions } kkonganti@1: } kkonganti@1: kkonganti@1: versions.mix( kkonganti@1: SAMPLESHEET_CHECK.out.versions, kkonganti@1: CAT_FASTQ.out.versions.first().ifEmpty(null) kkonganti@1: ) kkonganti@1: .set { versions } kkonganti@1: kkonganti@1: emit: kkonganti@1: processed_reads kkonganti@1: versions kkonganti@1: } kkonganti@1: kkonganti@1: // Function to get list of [ meta, [ fq1, fq2 ] ] kkonganti@1: def create_fastq_channel(LinkedHashMap row) { kkonganti@1: kkonganti@1: def meta = [:] kkonganti@1: meta.id = row.sample kkonganti@1: meta.single_end = row.single_end.toBoolean() kkonganti@1: meta.strandedness = row.strandedness kkonganti@1: meta.id = meta.id.split(params.fq_filename_delim)[0..params.fq_filename_delim_idx.toInteger() - 1] kkonganti@1: .join(params.fq_filename_delim) kkonganti@1: meta.id = (meta.id =~ /\./ ? meta.id.take(meta.id.indexOf('.')) : meta.id) kkonganti@1: kkonganti@1: def array = [] kkonganti@1: kkonganti@1: if (!file(row.fq1).exists()) { kkonganti@1: stopNow("Please check input metadata CSV. The following Read 1 FASTQ file does not exist!" + kkonganti@1: "\n${row.fq1}") kkonganti@1: } kkonganti@1: if (meta.single_end) { kkonganti@1: array = [ meta, [ file(row.fq1) ] ] kkonganti@1: } else { kkonganti@1: if (!file(row.fq2).exists()) { kkonganti@1: stopNow("Please check input metadata CSV. The following Read 2 FASTQ file does not exist!" + kkonganti@1: "\n${row.fq2}") kkonganti@1: } kkonganti@1: array = [ meta, [ file(row.fq1), file(row.fq2) ] ] kkonganti@1: } kkonganti@1: return array kkonganti@1: }