@@ -59,6 +59,8 @@ void printUsage() {
5959 stderr.writeln(" output to stdout only reference names and lengths in JSON" );
6060 stderr.writeln(" -c, --count" );
6161 stderr.writeln(" output to stdout only count of matching records, hHI are ignored" );
62+ stderr.writeln(" -U, --unmapped" );
63+ stderr.writeln(" output reads with RNAME = *; available for indexed BAM only" );
6264 stderr.writeln(" -v, --valid" );
6365 stderr.writeln(" output only valid alignments" );
6466 stderr.writeln(" -S, --sam-input" );
@@ -105,6 +107,7 @@ bool with_header;
105107bool header_only;
106108bool reference_info_only;
107109bool count_only;
110+ bool unmapped_only;
108111bool skip_invalid_alignments;
109112bool is_sam;
110113
@@ -137,6 +140,7 @@ int view_main(string[] args) {
137140 " header|H" , &header_only,
138141 " reference-info|I" , &reference_info_only,
139142 " count|c" , &count_only,
143+ " unmapped|U" , &unmapped_only,
140144 " valid|v" , &skip_invalid_alignments,
141145 " sam-input|S" , &is_sam,
142146 " show-progress|p" , &show_progress,
@@ -198,6 +202,18 @@ int sambambaMain(T)(T _bam, TaskPool pool, string[] args)
198202 return 0 ;
199203 }
200204
205+ static if (is (T == SamReader)) {
206+ if (unmapped_only) {
207+ stderr.writeln(" --unmapped option is available only for indexed BAMs" );
208+ return - 1 ;
209+ }
210+ }
211+
212+ if (unmapped_only && args.length > 2 ) {
213+ stderr.writeln(" --unmapped option can't be used together with regions" );
214+ return - 1 ;
215+ }
216+
201217 if (header_only && ! count_only) {
202218 // write header to stdout
203219 (new HeaderSerializer(stdout, format)).writeln(bam.header);
@@ -255,7 +271,10 @@ int sambambaMain(T)(T _bam, TaskPool pool, string[] args)
255271 runProcessor(bam, reads, read_filter);
256272 bar.finish();
257273 } else {
258- runProcessor(bam, bam.reads! withoutOffsets(), read_filter);
274+ if (unmapped_only)
275+ runProcessor(bam, bam.unmappedReads(), read_filter);
276+ else
277+ runProcessor(bam, bam.reads! withoutOffsets(), read_filter);
259278 }
260279 } else { // SamFile
261280 runProcessor(bam, bam.reads, read_filter);
0 commit comments