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