Skip to content

Commit

Permalink
Add option to append barcodes from another fastq file to read names
Browse files Browse the repository at this point in the history
  • Loading branch information
porchard committed Nov 8, 2019
1 parent 391fdef commit bb89aa1
Showing 1 changed file with 32 additions and 1 deletion.
33 changes: 32 additions & 1 deletion src/cta.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,13 @@ class FASTQRecord {
std::string comment;
std::string sequence;
std::string quality;
void append_barcode(const std::string& barcode) {
if (name.find(' ')!=std::string::npos) {
name.insert(name.find(' '), "_" + barcode);
} else {
name += "_" + barcode;
}
}
};

std::ostream& operator<<(std::ostream& os, const FASTQRecord& record) {
Expand Down Expand Up @@ -279,6 +286,9 @@ void print_usage() {
<< "-t|--trim-from-start\n"
<< " Trim this number of bases from the start of each sequence. The default is zero.\n"

<< "-a|--append-barcode\n"
<< " Append barcodes from this fastq file to the end of read names (useful for e.g. single-cell data).\n"

<< std::endl;
}

Expand All @@ -293,6 +303,7 @@ int main(int argc, char **argv)
long long int rc_length = 20;
std::string input_filename1;
std::string input_filename2;
std::string barcode_filename;
std::string output_filename1;
std::string output_filename2;

Expand All @@ -304,11 +315,12 @@ int main(int argc, char **argv)
{"fudge", required_argument, NULL, 'f'},
{"trim-from-start", required_argument, NULL, 't'},
{"rc-length", required_argument, NULL, 'r'},
{"append-barcode", required_argument, NULL, 'a'},
{0, 0, 0, 0}
};

// parse the command line arguments
while ((c = getopt_long(argc, argv, "d:f:t:r:vh?", long_options, &option_index)) != -1) {
while ((c = getopt_long(argc, argv, "d:f:t:r:a:vh?", long_options, &option_index)) != -1) {
switch (c) {
case 'h':
case '?':
Expand All @@ -329,6 +341,9 @@ int main(int argc, char **argv)
case 'r':
rc_length = std::stoll(optarg);
break;
case 'a':
barcode_filename = optarg;
break;
case 0:
if (long_options[option_index].flag != 0){
break;
Expand Down Expand Up @@ -369,6 +384,9 @@ int main(int argc, char **argv)
bio::file_source input_file2(input_filename2);
bio::stream<bio::file_source> input_stream2(input_file2);

bio::file_source barcode_file(barcode_filename);
bio::stream<bio::file_source> barcode_stream(barcode_file);

bio::filtering_stream<bio::input> in1;
if (is_gzipped_file(input_filename1)) {
in1.push(bio::gzip_decompressor());
Expand All @@ -381,6 +399,12 @@ int main(int argc, char **argv)
}
in2.push(input_stream2);

bio::filtering_stream<bio::input> inBarcode;
if (!barcode_filename.empty() && is_gzipped_file(barcode_filename)) {
inBarcode.push(bio::gzip_decompressor());
}
inBarcode.push(barcode_stream);

if (output_filename1.empty()) {
output_filename1 = make_trimmed_filename(input_filename1);
}
Expand Down Expand Up @@ -410,9 +434,16 @@ int main(int argc, char **argv)

FASTQRecord record1;
FASTQRecord record2;
FASTQRecord recordBarcode;

while ((in1 >> record1) && (in2 >> record2)) {
trim_pair(record1, record2, rc_length, max_edit_distance, fudge, trim_start, verbose);
if (!barcode_filename.empty()) {
inBarcode >> recordBarcode;
std::string barcode = recordBarcode.sequence;
record1.append_barcode(barcode);
record2.append_barcode(barcode);
}
out1 << record1;
out2 << record2;
}
Expand Down

0 comments on commit bb89aa1

Please sign in to comment.