|
5 | 5 | // |
6 | 6 |
|
7 | 7 | include { BCLCONVERT } from "../../../modules/nf-core/bclconvert/main" |
8 | | -include { generateReadgroup as generateReadgroupBCLCONVERT } from "../../../modules/nf-core/bclconvert/main" |
9 | 8 | include { BCL2FASTQ } from "../../../modules/nf-core/bcl2fastq/main" |
10 | | -include { generateReadgroup as generateReadgroupBCL2FASTQ } from "../../../modules/nf-core/bcl2fastq/main" |
11 | 9 |
|
12 | 10 | workflow BCL_DEMULTIPLEX { |
13 | 11 | take: |
@@ -92,3 +90,96 @@ workflow BCL_DEMULTIPLEX { |
92 | 90 | interop = ch_interop |
93 | 91 | logs = ch_logs |
94 | 92 | } |
| 93 | + |
| 94 | +def generateReadgroupBCLCONVERT(ch_fastq_list_csv, ch_fastq) { |
| 95 | + return ch_fastq_list_csv |
| 96 | + .collect() // make it a value channel |
| 97 | + .map { meta, csv_file -> |
| 98 | + def fastq_metadata = [] |
| 99 | + csv_file |
| 100 | + .splitCsv(header: true) |
| 101 | + .each { row -> |
| 102 | + // Create the readgroup tuple |
| 103 | + // RGID,RGSM,RGLB,Lane,Read1File,Read2File |
| 104 | + def rg = [:] |
| 105 | + // row.RGID is index1.index2.lane |
| 106 | + rg.ID = row.RGID |
| 107 | + // RGPU is a custom column in the samplesheet containing the flowcell ID |
| 108 | + rg.PU = row.RGPU ? row.RGPU : meta.id + "." + row.Lane |
| 109 | + rg.SM = row.RGSM |
| 110 | + rg.LB = row.RGLB ? row.RGLB : "" |
| 111 | + rg.PL = "ILLUMINA" |
| 112 | + |
| 113 | + // replace the meta id with the sample name |
| 114 | + def new_meta = [id: row.RGSM, readgroup: rg] |
| 115 | + // Return the new meta with fastq file |
| 116 | + fastq_metadata << [new_meta, file(row.Read1File).name] |
| 117 | + if (row.Read2File) { |
| 118 | + fastq_metadata << [new_meta, file(row.Read2File).name] |
| 119 | + } |
| 120 | + } |
| 121 | + return [meta, fastq_metadata] |
| 122 | + } |
| 123 | + .join(ch_fastq, by:[0]) // -> [ meta, [fq_meta, fastq_filename], [fastq_file, ...] ] |
| 124 | + .transpose(by:[2]) // -> [ meta, [fq_meta, fastq_filename], fastq_file ] |
| 125 | + .map { meta, fastq_metadata, fastq_file -> |
| 126 | + def fastq_meta = fastq_metadata.find { _meta, filename -> filename == file(fastq_file).name } |
| 127 | + return [meta + fastq_meta[0], file(fastq_file)] |
| 128 | + } |
| 129 | + .groupTuple(by: [0]) |
| 130 | + .map { meta, fastq -> |
| 131 | + meta.single_end = fastq.size() == 1 |
| 132 | + return [meta, fastq.flatten()] |
| 133 | + } |
| 134 | +} |
| 135 | + |
| 136 | +def generateReadgroupBCL2FASTQ(ch_fastq) { |
| 137 | + ch_fastq |
| 138 | + .transpose() |
| 139 | + .map { fc_meta, fastq -> |
| 140 | + def meta = [:] |
| 141 | + meta.id = fastq.getSimpleName().toString() - ~/_R[0-9]_001.*$/ |
| 142 | + meta.samplename = fastq.getSimpleName().toString() - ~/_S[0-9]+.*$/ |
| 143 | + meta.fcid = fc_meta.id |
| 144 | + meta.lane = fc_meta.lane |
| 145 | + // The buffered input stream allows reading directly from cloud storage |
| 146 | + // It will not make a local copy of the file. |
| 147 | + def line = "" |
| 148 | + fastq.withInputStream { fq -> |
| 149 | + def gzipStream = new java.util.zip.GZIPInputStream(fq) |
| 150 | + def decoder = new InputStreamReader(gzipStream, 'ASCII') |
| 151 | + def buffered = new BufferedReader(decoder) |
| 152 | + line = buffered.readLine() |
| 153 | + buffered.close() |
| 154 | + } |
| 155 | + if (line != null && line.startsWith('@')) { |
| 156 | + line = line.substring(1) |
| 157 | + // expected format is like: |
| 158 | + // xx:yy:FLOWCELLID:LANE:... (seven fields) |
| 159 | + def fields = line.split(':') |
| 160 | + // CASAVA 1.8+ format, from https://support.illumina.com/help/BaseSpace_OLH_009008/Content/Source/Informatics/BS/FileFormat_FASTQ-files_swBS.htm |
| 161 | + // "@<instrument>:<run number>:<flowcell ID>:<lane>:<tile>:<x-pos>:<y-pos>:<UMI> <read>:<is filtered>:<control number>:<index>" |
| 162 | + //def sequencer_serial = fields[0] |
| 163 | + //def run_nubmer = fields[1] |
| 164 | + def fcid = fields[2] |
| 165 | + def lane = fields[3] |
| 166 | + def index = fields[-1] =~ /[GATC+-]/ ? fields[-1] : "" |
| 167 | + def ID = [index, lane].join(".") |
| 168 | + def LB = "" |
| 169 | + def PL = "ILLUMINA" |
| 170 | + def PU = [fcid, lane].findAll().join(".") |
| 171 | + def SM = fastq.getSimpleName().toString() - ~/_S[0-9]+.*$/ |
| 172 | + meta.readgroup = ["ID": ID, "SM": SM, "PL": PL, "PU": PU, "LB": LB] |
| 173 | + } |
| 174 | + else { |
| 175 | + println("No reads were found in FASTQ file: ${fastq}") |
| 176 | + meta.readgroup = [:] |
| 177 | + } |
| 178 | + return [meta, fastq] |
| 179 | + } |
| 180 | + .groupTuple(by: [0]) |
| 181 | + .map { meta, fastq -> |
| 182 | + meta.single_end = fastq.size() == 1 |
| 183 | + return [meta, fastq.flatten()] |
| 184 | + } |
| 185 | +} |
0 commit comments