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