4242#include < kmtricks/plugin_manager.hpp>
4343#endif
4444
45+ #include < gatb/bank/impl/BankBam.hpp>
46+
4547namespace km {
4648
49+ inline void apply_bam_filtering (IBank* bank, const std::string& bam_exclude_refs,
50+ uint32_t bam_include_flags = 0 , uint32_t bam_exclude_flags = 0 ) {
51+ BankBam* bam_bank = dynamic_cast <BankBam*>(bank);
52+ if (!bam_bank) return ;
53+
54+ // Apply reference filtering
55+ if (!bam_exclude_refs.empty ()) {
56+ std::set<std::string> excluded_refs;
57+ std::istringstream ss (bam_exclude_refs);
58+ std::string ref;
59+ while (std::getline (ss, ref, ' ,' )) {
60+ // Trim whitespace
61+ ref.erase (0 , ref.find_first_not_of (" \t " ));
62+ ref.erase (ref.find_last_not_of (" \t " ) + 1 );
63+ if (!ref.empty ()) {
64+ excluded_refs.insert (ref);
65+ }
66+ }
67+ if (!excluded_refs.empty ()) {
68+ bam_bank->setExcludedReferences (excluded_refs);
69+ }
70+ }
71+
72+ // Apply flag filtering (samtools -f/-F style)
73+ if (bam_include_flags != 0 ) {
74+ bam_bank->setRequireFlags (static_cast <uint16_t >(bam_include_flags));
75+ }
76+ if (bam_exclude_flags != 0 ) {
77+ bam_bank->setExcludeFlags (static_cast <uint16_t >(bam_exclude_flags));
78+ }
79+ }
80+
4781namespace fs = std::filesystem;
4882using parti_info_t = std::shared_ptr<PartiInfo<5 >>;
4983
@@ -52,8 +86,11 @@ class ConfigTask : public ITask
5286{
5387public:
5488 ConfigTask (const std::string& path, IProperties* props, uint64_t bloom_size,
55- uint32_t partitions)
56- : ITask(0 ), m_path(path), m_props(props), m_bloom_size(bloom_size), m_nb_partitions(partitions)
89+ uint32_t partitions, const std::string& bam_exclude_refs = " " ,
90+ uint32_t bam_include_flags = 0 , uint32_t bam_exclude_flags = 0 )
91+ : ITask(0 ), m_path(path), m_props(props), m_bloom_size(bloom_size), m_nb_partitions(partitions),
92+ m_bam_exclude_refs (bam_exclude_refs), m_bam_include_flags(bam_include_flags),
93+ m_bam_exclude_flags(bam_exclude_flags)
5794 {}
5895
5996 void preprocess () {}
@@ -63,6 +100,7 @@ class ConfigTask : public ITask
63100 spdlog::debug (" [exec] - ConfigTask" );
64101 spdlog::info (" {} samples found ({} read files)." , KmDir::get ().m_fof .size (), KmDir::get ().m_fof .total ());
65102 IBank* bank = Bank::open (KmDir::get ().m_fof .get_all ()); LOCAL (bank);
103+ apply_bam_filtering (bank, m_bam_exclude_refs, m_bam_include_flags, m_bam_exclude_flags);
66104 Storage* config_storage =
67105 StorageFactory (STORAGE_FILE ).create (KmDir::get ().m_config_storage , true , false );
68106 LOCAL (config_storage);
@@ -90,6 +128,9 @@ class ConfigTask : public ITask
90128 IProperties* m_props;
91129 uint64_t m_bloom_size;
92130 uint32_t m_nb_partitions;
131+ std::string m_bam_exclude_refs;
132+ uint32_t m_bam_include_flags;
133+ uint32_t m_bam_exclude_flags;
93134};
94135
95136void check_repart_compatibility (Configuration& c1, Configuration& c2,
@@ -109,8 +150,11 @@ template<size_t span>
109150class RepartTask : public ITask
110151{
111152public:
112- RepartTask (const std::string& path, const std::string& from = " " , bool static_repart = false )
113- : ITask(1 ), m_path(path), m_from(from), m_static_repart(static_repart) {}
153+
154+ RepartTask (const std::string& path, const std::string& bam_exclude_refs = " " ,
155+ uint32_t bam_include_flags = 0 , uint32_t bam_exclude_flags = 0 , const std::string& from = " " , bool static_repart = false )
156+ : ITask(1 ), m_path(path), m_bam_exclude_refs(bam_exclude_refs),
157+ m_bam_include_flags (bam_include_flags), m_bam_exclude_flags(bam_exclude_flags), m_from(from), m_static_repart(static_repart) {}
114158
115159 void preprocess () {}
116160 void postprocess ()
@@ -142,9 +186,11 @@ class RepartTask : public ITask
142186 {
143187 Fof fof (m_path);
144188 IBank* bank = Bank::open (fof.get_all ()); LOCAL (bank);
189+ apply_bam_filtering (bank, m_bam_exclude_refs, m_bam_include_flags, m_bam_exclude_flags);
145190 Storage* rep_store =
146191 StorageFactory (STORAGE_FILE ).create (KmDir::get ().m_repart_storage , true , false );
147192
193+
148194 LOCAL (rep_store);
149195
150196 RepartitorAlgorithm<span> repartition (
@@ -180,6 +226,9 @@ class RepartTask : public ITask
180226
181227private:
182228 std::string m_path;
229+ std::string m_bam_exclude_refs;
230+ uint32_t m_bam_include_flags;
231+ uint32_t m_bam_exclude_flags;
183232 std::string m_from;
184233 int m_cores;
185234 uint32_t m_nb_parts {0 };
0 commit comments