NCBI C++ Toolkit Cross Reference

  C++/src/gui/packages/pkg_snp/bins/bins_ds.cpp


1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406
/* $Id: bins_ds.cpp 38966 2017-07-13 17:08:23Z katargir $ * =========================================================================== * * PUBLIC DOMAIN NOTICE * National Center for Biotechnology Information * * This software/database is a "United States Government Work" under the * terms of the United States Copyright Act. It was written as part of * the author's official duties as a United States Government employee and * thus cannot be copyrighted. This software/database is freely available * to the public for use. The National Library of Medicine and the U.S. * Government have not placed any restriction on its use or reproduction. * * Although all reasonable efforts have been taken to ensure the accuracy * and reliability of the software and data, the NLM and the U.S. * Government do not and cannot warrant the performance or results that * may be obtained by using this software or data. The NLM and the U.S. * Government disclaim all warranties, express or implied, including * warranties of performance, merchantability or fitness for any particular * purpose. * * Please cite the author in any work or product based on this material. * * =========================================================================== * * Authors: Melvin Quintos, Dmitry Rudnev * * Description: This file defines CBinsJob, CBinsDS, and CBinsDSType * */ #include <ncbi_pch.hpp> #include <gui/packages/pkg_snp/bins/bins_ds.hpp> #include <gui/packages/pkg_snp/bins/bins_glyph.hpp> #include <gui/packages/pkg_snp/bins/gmark_glyph.hpp> #include <gui/packages/pkg_snp/bins/scatter_glyph.hpp> #include <gui/packages/pkg_snp/utils/utils.hpp> #include <gui/objutils/utils.hpp> #include <gui/objutils/snp_gui.hpp> #include <gui/utils/app_job_dispatcher.hpp> #include <gui/widgets/seq_graphic/histogram_glyph.hpp> #include <objmgr/graph_ci.hpp> #include <objmgr/seq_loc_mapper.hpp> #include <objmgr/seq_map_switch.hpp> #include <objmgr/seqdesc_ci.hpp> #include <objmgr/table_field.hpp> #include <objmgr/util/feature.hpp> #include <objmgr/util/sequence.hpp> #include <objmgr/util/sequence.hpp> #include <objects/seq/Annot_descr.hpp> #include <objects/seq/Annotdesc.hpp> #include <objtools/snputil/snp_bins.hpp> #include <objects/seqres/Byte_graph.hpp> #include <cmath> BEGIN_NCBI_SCOPE USING_SCOPE(objects); /////////////////////////////////////////////////////////////////////////////// /// CBinsJob /////////////////////////////////////////////////////////////////////////////// class CBinsJob : public CSGAnnotJob { public: struct SParams { bool isSeqView; bool bAdaptiveSelector; TModelUnit scale; int binWidth; int depth; string name; }; CBinsJob(const CBioseq_Handle& handle, const TSeqRange& range, const SParams& params) : CSGAnnotJob("SNP", handle, SAnnotSelector(), range) , m_Params(params) {} virtual EJobState x_Execute(); private: void x_FetchGraphs(const CSeq_loc &, CSeqGlyph::TObjects*); void x_FetchFtable(const CSeq_loc &, CSeqGlyph::TObjects*); // will try to load and create bins and return true; // if however the amount of positions would be unreasonably (>500) big, will return // false and glyphs will be undefined bool x_PreFetchFtable(const SAnnotSelector& sel, const objects::CSeq_loc &loc, CSeqGlyph::TObjects* glyphs); private: SParams m_Params; SBinsJobResult::EDataType m_eDataType; }; IAppJob::EJobState CBinsJob::x_Execute() { EJobState state = eCompleted; try { CSeqGlyph::TObjects glyphs; CRef<CSeq_loc> loc = m_Handle.GetRangeSeq_loc(m_Range.GetFrom(), m_Range.GetTo()); x_FetchFtable(*loc, &glyphs); SBinsJobResult *result = new SBinsJobResult(); m_Result.Reset(result); result->listObjs.swap(glyphs); result->eDataType = m_eDataType; } catch (CException& ex) { m_Error.Reset(new CAppJobError(ex.GetMsg())); state = eFailed; } catch (std::exception& ex) { m_Error.Reset(new CAppJobError(ex.what())); state = eFailed; } return state; } void CBinsJob::x_FetchFtable(const CSeq_loc &loc, CSeqGlyph::TObjects *objs) { SAnnotSelector sel; NSnpBins::GetBinSelector(m_Params.name, m_Params.bAdaptiveSelector, m_Params.depth, sel); CSeqGlyph::TObjects tmpList; bool isBinsLoaded(x_PreFetchFtable(sel, loc, &tmpList)); if(isBinsLoaded) { objs->swap(tmpList); m_eDataType = SBinsJobResult::eBins; } else { x_FetchGraphs(loc, objs); } } bool CBinsJob::x_PreFetchFtable(const SAnnotSelector& sel, const CSeq_loc &loc, CSeqGlyph::TObjects *objs) { const CTableFieldHandle<int> col_type("trackType"); const CTableFieldHandle<string> col_sub_type("trackSubType"); int type; string subType; int pos_start, pos_end; string title, comment; bool hasResult = false; // data has at least 1 non-zero bin double scale = fabs(m_Params.scale); const int binWidth = m_Params.binWidth; // data has at least one non-0 pvalue bool hasPvalue(false); double window = scale*binWidth; if (window < 1 || m_Params.isSeqView) { window = 1; } CBinsGlyph::TDensityMap dmap( m_Range.GetFrom(), m_Range.GetTo(), window, new CBinsGlyph::accum_study()); CGeneMarkerGlyph::TGeneMarkers gene_markers; // Grab annotations and start processing data CSeq_annot_Handle annot; if(!NSnpBins::GetBinHandle(m_Handle.GetScope(), sel, loc, annot)) { return false; } NSnpBins::ReadAnnotDesc(annot, title, comment); NSnpBins::FindPosIndexRange(annot, (int)m_Range.GetFrom(), (int)m_Range.GetTo(), pos_start, pos_end); // Read the type information // if none, assume eGAP if(!col_type.TryGet(annot, 0, type)) { type = NSnpBins::eGAP; } // get the potentially existing subtype for GAP tracks if(type == NSnpBins::eGAP) { col_sub_type.TryGet(annot, 0, subType); } bool isGeneMarker(NSnpBins::isGeneMarker(subType)); // if there are too many rows, abort the load and indicate that a graph should be loaded instead if(pos_end - pos_start > 500) { return false; } // Read all the rows and columns // the least common denominator is having the following column: // pos (if absent, the line will be skipped entirely) // if trackType is missing, it is assumed to be eGAP // all others are allowed to be missing // (although the data provider should strive to have all correct columns expected // for a given trackType) for(int row = pos_start; row < pos_end; ++row) { CRef<NSnpBins::SBinEntry> BinEntry(NSnpBins::GetEntry(annot, row)); if(!BinEntry.IsNull()) { if(!isGeneMarker) { TSeqRange range = TSeqRange(BinEntry->pos, BinEntry->pos); if(BinEntry->pvalue != 0) { hasPvalue = true; } CBinsGlyph::SBinMapped bin; bin.obj.Reset(new NSnpBins::SBin); bin.obj->count = 1; bin.obj->type = type; bin.obj->range = range; bin.obj->m_EntryList.push_back(BinEntry); bin.obj->m_SigEntry = BinEntry; dmap.AddRange(range, bin); } else { TSeqRange range = TSeqRange(BinEntry->pos, BinEntry->pos_end); CRef<NSnpBins::SBin> obj(new NSnpBins::SBin); obj->count = 1; obj->type = type; obj->range = range; obj->m_SigEntry = BinEntry; gene_markers.push_back(obj); } hasResult = true; } } if (hasResult) { if(!isGeneMarker) { size_t binCount(0); NON_CONST_ITERATE(CBinsGlyph::TDensityMap, iBin, dmap) { iBin->SequentialNumber = binCount++; } if(hasPvalue && (type == NSnpBins::eGAP || type == NSnpBins::eGCAT)) { CRef<CScatterPlotGlyph> scatter_plot(new CScatterPlotGlyph(dmap, loc)); scatter_plot->SetTitle(title); scatter_plot->SetAnnotName(m_Params.name); CRef<CSeqGlyph> sref(scatter_plot.GetPointer()); objs->push_back(sref); } CRef<CBinsGlyph> bins; bins.Reset(new CBinsGlyph(dmap, loc, &m_Handle.GetScope())); bins->SetTitle(title); bins->SetAnnotName(m_Params.name); bins->SetType(type); CRef<CSeqGlyph> fref(bins.GetPointer()); objs->push_back(fref); } else { CRef<CGeneMarkerGlyph> gene_marker_glyph(new CGeneMarkerGlyph(gene_markers, loc, &m_Handle.GetScope())); gene_marker_glyph->SetTitle(title); gene_marker_glyph->SetAnnotName(m_Params.name); CRef<CSeqGlyph> fref(gene_marker_glyph.GetPointer()); objs->push_back(fref); } } return true; } void CBinsJob::x_FetchGraphs(const CSeq_loc& loc, CSeqGlyph::TObjects *glyphs) { SAnnotSelector sel; sel = CSeqUtils::GetAnnotSelector(CSeq_annot::TData::e_Seq_table); sel.IncludeNamedAnnotAccession(m_Params.name); sel.AddNamedAnnots(m_Params.name); CSeqUtils::SetResolveDepth(sel, m_Params.bAdaptiveSelector, m_Params.depth); CSeqGlyph::TObjects tmpList; CTableFieldHandle<int> col_pos("pos"); int pos; int pos_start, pos_end; CHistogramGlyph::TMap dmap(m_Range.GetFrom(), m_Range.GetTo(), m_Params.scale); // Grab annotations and start processing data CAnnot_CI iter(m_Handle.GetScope(), loc, sel); // internal error! if (iter.size()!=1) { NCBI_THROW(CException, eUnknown, "Incorrect bins data!"); } CSeq_annot_Handle annot = *iter; string title, comment; NSnpBins::ReadAnnotDesc(annot, title, comment); NSnpBins::FindPosIndexRange(annot, (int)m_Range.GetFrom(), (int)m_Range.GetTo(), pos_start, pos_end); // Read all the rows and columns for ( int row = pos_start; row < pos_end; ++row ) { if ( col_pos.TryGet(annot, row, pos)) { TSeqRange range = TSeqRange(pos, pos); dmap.AddRange(range); } } CHistogramGlyph* hist = new CHistogramGlyph(dmap, CSeqFeatData::eSubtype_variation, title); hist->SetAnnotName(m_Params.name); glyphs->push_back(CRef<CSeqGlyph>(hist)); m_eDataType = SBinsJobResult::eHistFromFeats; } /////////////////////////////////////////////////////////////////////////////// /// CBinsDS /////////////////////////////////////////////////////////////////////////////// CBinsDS::CBinsDS(CScope& scope, const CSeq_id& id) : CSGGenBankDS(scope, id) {} void CBinsDS::GetTrackNames(SConstScopedObject& object, TAnnotNameTitleMap& names, const TSeqRange& range, const ILayoutTrackFactory::SExtraParams& params) { const CSeq_id& id = dynamic_cast<const CSeq_id&>(object.object.GetObject()); CBioseq_Handle handle = object.scope->GetBioseqHandle(id); // Find data SAnnotSelector sel = CSeqUtils::GetAnnotSelector(params.m_Annots); CSeqUtils::SetResolveDepth(sel, params.m_Adaptive, params.m_Level); sel.SetAnnotType(CSeq_annot::TData::e_Seq_table); sel.SetCollectNames(); CAnnotTypes_CI annot_it(CSeq_annot::C_Data::e_Seq_table, handle, range, eNa_strand_unknown, &sel); ITERATE (CAnnotTypes_CI::TAnnotNames, iter, annot_it.GetAnnotNames()) { if (iter->IsNamed()) { names.insert(TAnnotNameTitleMap::value_type(iter->GetName(), "")); } else { names.insert(TAnnotNameTitleMap::value_type("Unnamed", "")); } } } void CBinsDS::SetName(const string& name) { m_Name = name; } void CBinsDS::LoadData(const TSeqRange& range, TModelUnit scale, bool isSeqView) { CRef<CBinsJob> job; CBinsJob::SParams p; p.isSeqView = isSeqView; p.bAdaptiveSelector = m_Adaptive; p.scale = scale; p.binWidth = NSnpGui::c_BinWidth; p.depth = m_Depth; p.name = m_Name; job.Reset(new CBinsJob(m_Handle, range, p)); x_LaunchJob(*job); } /////////////////////////////////////////////////////////////////////////////// /// CBinsDSType /////////////////////////////////////////////////////////////////////////////// ISGDataSource* CBinsDSType::CreateDS(SConstScopedObject& object) const { const CSeq_id& id = dynamic_cast<const CSeq_id&>(object.object.GetObject()); return new CBinsDS(object.scope.GetObject(), id); } string CBinsDSType::GetExtensionIdentifier() const { static string sid("snp_bins_ds_type"); return sid; } string CBinsDSType::GetExtensionLabel() const { static string slabel("SNP Bin Track Data Source Type"); return slabel; } bool CBinsDSType::IsSharable() const { return false; } END_NCBI_SCOPE

source navigation ]   [ diff markup ]   [ identifier search ]   [ freetext search ]   [ file search ]  

This page was automatically generated by the LXR engine.
Visit the LXR main site for more information.