NCBI C++ ToolKit
project_view_factory.cpp
Go to the documentation of this file.

Go to the SVN repository for this file.

1 /* $Id: project_view_factory.cpp 47479 2023-05-02 13:24:02Z ucko $
2 * ===========================================================================
3 *
4 * PUBLIC DOMAIN NOTICE
5 * National Center for Biotechnology Information
6 *
7 * This software / database is a "United States Government Work" under the
8 * terms of the United States Copyright Act. It was written as part of
9 * the author's official duties as a United States Government employee and
10 * thus cannot be copyrighted. This software / database is freely available
11 * to the public for use. The National Library of Medicine and the U.S.
12 * Government have not placed any restriction on its use or reproduction.
13 *
14 * Although all reasonable efforts have been taken to ensure the accuracy
15 * and reliability of the software and data, the NLM and the U.S.
16 * Government do not and cannot warrant the performance or results that
17 * may be obtained by using this software or data. The NLM and the U.S.
18 * Government disclaim all warranties, express or implied, including
19 * warranties of performance, merchantability or fitness for any particular
20 * purpose.
21 *
22 * Please cite the author in any work or product based on this material.
23 *
24 * ===========================================================================
25 *
26 * Authors: Liangshou Wu
27 *
28 * File Description:
29 */
30 
31 
32 #include <ncbi_pch.hpp>
33 
36 #include <gui/objutils/label.hpp>
37 
38 #include <objmgr/util/sequence.hpp>
40 
41 
44 
46 {
48 }
49 
51 {
52  return NULL;
53 }
54 
55 bool IProjectViewFactory::IsCompatibleWith( const CObject& object, objects::CScope& scope )
56 {
58  objects.push_back(SConstScopedObject(&object, &scope));
59 
60  return TestInputObjects( objects ) != 0;
61 }
62 
63 
65 {
66  size_t num_objects = objs.size();
67 
68  if( num_objects <= 0 ){
69  return vector<int>();
70  } else if( num_objects == 1 ){
71  return vector<int>( 1, 0 );
72  }
73 
74  vector<int> groups( num_objects, -1 );
75  vector<CSeq_id_Handle> loc_handles;
76  vector< set<CSeq_id_Handle> > aln_handles;
77 
78  for( size_t ix = 0; ix < num_objects; ix++ ){
79  const CSeq_loc* loc = dynamic_cast<const CSeq_loc*>( objs[ix].object.GetPointer() );
80  if( loc ){
81  CScope* scope = const_cast<CScope*>( objs[ix].scope.GetPointer() );
82  CSeq_id_Handle idh = sequence::GetIdHandle( *loc, scope );
83  idh = sequence::GetId( idh, *scope, sequence::eGetId_Canonical );
84 
85  vector<CSeq_id_Handle>::iterator found =
86  find( loc_handles.begin(), loc_handles.end(), idh )
87  ;
88  if( found != loc_handles.end() ){
89  groups[ix] = static_cast<int>(found - loc_handles.begin());
90  } else {
91  groups[ix] = static_cast<int>(loc_handles.size());
92  loc_handles.push_back( idh );
93  }
94 
95  continue;
96  }
97 
98  const CSeq_align* aln = dynamic_cast<const CSeq_align*>( objs[ix].object.GetPointer() );
99  if( aln ){
100  CScope* scope = const_cast<CScope*>( objs[ix].scope.GetPointer() );
101 
102  int num_seqs = aln->CheckNumRows();
103  if( num_seqs <= 0 ){
104  continue;
105  }
106 
107  set<CSeq_id_Handle> idh_set;
108  for( int q = 0; q < num_seqs; q++ ){
109  const CSeq_id& seq_id = aln->GetSeq_id( q );
111  idh = sequence::GetId( idh, *scope, sequence::eGetId_Canonical );
112  idh_set.insert( idh );
113  }
114 
115  vector< set<CSeq_id_Handle> >::iterator found =
116  find( aln_handles.begin(), aln_handles.end(), idh_set )
117  ;
118  if( found != aln_handles.end() ){
119  groups[ix] = (int)(found - aln_handles.begin() + num_objects);
120  } else {
121  groups[ix] = (int)(aln_handles.size() + num_objects);
122  aln_handles.push_back( idh_set );
123  }
124 
125  continue;
126  }
127  }
128 
129  return groups;
130 }
131 
133  const TConstScopedObjects& objects, TObjectsMap& objects_map
134 ){
135  typedef map<vector<CSeq_id_Handle>, TConstScopedObjects> TIdsToObjectsMap;
136  TIdsToObjectsMap handle_groups;
137 
139 
140  CConstRef<CObject> object = obtr->object;
141  CRef<CScope> scope = obtr->scope;
142 
143  if( object.IsNull() || scope.IsNull() ){
144  continue;
145  }
146 
147  vector<CSeq_id_Handle> idh_vec;
148 
149  const CSeq_id* id = dynamic_cast<const CSeq_id*>( &*object );
150  if( id ){
152  idh_vec.push_back( idh ? idh : CSeq_id_Handle::GetHandle( *id ) );
153  }
154 
155  const CSeq_loc* loc = dynamic_cast<const CSeq_loc*>( &*object );
156  if( loc ){
157  //! If loc is mix, we need to iterate ids
158  const CSeq_id& seq_id = *loc->GetId();
160  idh_vec.push_back( idh ? idh : CSeq_id_Handle::GetHandle( seq_id ) );
161  }
162 
163  const CSeq_align* aln = dynamic_cast<const CSeq_align*>( &*object );
164  if( aln ){
165  int num_seqs = aln->CheckNumRows();
166  if( num_seqs <= 0 ){
167  continue;
168  }
169 
170  set<CSeq_id_Handle> idh_set;
171  for( int seqix = 0; seqix < num_seqs; seqix++ ){
172  const CSeq_id& seq_id = aln->GetSeq_id( seqix );
174  idh_set.insert( idh ? idh : CSeq_id_Handle::GetHandle( seq_id ) );
175  }
176 
177  ITERATE( set<CSeq_id_Handle>, idh_itr, idh_set ){
178  idh_vec.push_back( *idh_itr );
179  }
180 
181  if( idh_vec.size() == 1 ){
182  idh_vec.push_back( idh_vec.front() );
183  }
184  }
185 
186 
187  handle_groups[idh_vec].push_back( *obtr );
188  }
189 
190  objects_map.clear();
191  NON_CONST_ITERATE( TIdsToObjectsMap, sidhtr, handle_groups ){
192  const vector<CSeq_id_Handle>& idh_vec = sidhtr->first;
193  TConstScopedObjects& group = sidhtr->second;
194 
195  string label;
196 
197  bool comma = false;
198  ITERATE( vector<CSeq_id_Handle>, idhtr, idh_vec ){
199  if( comma ){
200  label += " x ";
201  } else {
202  comma = true;
203  }
204 
205  CSeq_id_Handle idh = sequence::GetId( *idhtr, *group[0].scope, sequence::eGetId_Best );
206  label += (idh ? idh : *idhtr).GetSeqId()->GetSeqIdString( true );
207  }
208 
209  if( !label.empty() ){
210  objects_map[label] = group;
211 
212  } else {
213  NON_CONST_ITERATE( TConstScopedObjects, scobtr, group ){
214  TConstScopedObjects single;
215  single.push_back( *scobtr );
216 
217  CLabel::GetLabel( *scobtr->object, &label, CLabel::eContent, scobtr->scope );
218 
219  objects_map[label].push_back( *scobtr );
220  label.clear();
221  }
222  }
223  }
224 }
225 
226 
CObject –.
Definition: ncbiobj.hpp:180
CScope –.
Definition: scope.hpp:92
TDim CheckNumRows(void) const
Validatiors.
Definition: Seq_align.cpp:73
const CSeq_id & GetSeq_id(TDim row) const
Get seq-id (the first one if segments have different ids).
Definition: Seq_align.cpp:317
CViewTypeDescriptor - holds description of a view type.
Definition: view.hpp:98
IOpenViewManager - interface dealing with a series of open view operations.
virtual const CViewTypeDescriptor & GetViewTypeDescriptor() const
returns a Descriptor for the View Type supported by the Factory
virtual int TestInputObjects(TConstScopedObjects &objects)=0
tests input objects (probably using object conversion, or not) and returns a combination of ETestResu...
virtual IOpenViewManager * GetOpenViewManager()
virtual bool IsCompatibleWith(const CObject &object, objects::CScope &scope)
virtual vector< int > CombineInputObjects(const TConstScopedObjects &objects)
virtual const CProjectViewTypeDescriptor & GetProjectViewTypeDescriptor() const =0
returns a Descriptor for the View Type supported by the Factory
virtual void CombineObjects(const TConstScopedObjects &objects, TObjectsMap &objects_map)
void clear()
Definition: map.hpp:169
Definition: set.hpp:45
iterator_bool insert(const value_type &val)
Definition: set.hpp:149
#define ITERATE(Type, Var, Cont)
ITERATE macro to sequence through container elements.
Definition: ncbimisc.hpp:815
#define NON_CONST_ITERATE(Type, Var, Cont)
Non constant version of ITERATE macro.
Definition: ncbimisc.hpp:822
#define NULL
Definition: ncbistd.hpp:225
static void GetLabel(const CObject &obj, string *label, ELabelType type=eDefault)
Definition: label.cpp:140
vector< SConstScopedObject > TConstScopedObjects
Definition: objects.hpp:65
@ eContent
Definition: label.hpp:62
static CSeq_id_Handle GetHandle(const CSeq_id &id)
Normal way of getting a handle, works for any seq-id.
const CSeq_id * GetId(void) const
Get the id of the location return NULL if has multiple ids or no id at all.
Definition: Seq_loc.hpp:941
const CSeq_id & GetId(const CSeq_loc &loc, CScope *scope)
If all CSeq_ids embedded in CSeq_loc refer to the same CBioseq, returns the first CSeq_id found,...
CSeq_id_Handle GetIdHandle(const CSeq_loc &loc, CScope *scope)
@ eGetId_Best
return the "best" gi (uses FindBestScore(), with CSeq_id::CalculateScore() as the score function
Definition: sequence.hpp:101
@ eGetId_Canonical
Definition: sequence.hpp:114
bool IsNull(void) const THROWS_NONE
Check if pointer is null – same effect as Empty().
Definition: ncbiobj.hpp:735
#define END_NCBI_SCOPE
End previously defined NCBI scope.
Definition: ncbistl.hpp:103
#define BEGIN_NCBI_SCOPE
Define ncbi namespace.
Definition: ncbistl.hpp:100
static const char label[]
unsigned int
A callback function used to compare two keys in a database.
Definition: types.hpp:1210
USING_SCOPE(objects)
static bool GetSeqId(const T &d, set< string > &labels, const string name="", bool detect=false, bool found=false)
Modified on Thu Jul 18 16:00:45 2024 by modify_doxy.py rev. 669887