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

Go to the SVN repository for this file.

1 /*===========================================================================
2  *
3  * PUBLIC DOMAIN NOTICE
4  * National Center for Biotechnology Information
5  *
6  * This software/database is a "United States Government Work" under the
7  * terms of the United States Copyright Act. It was written as part of
8  * the author's official duties as a United States Government employee and
9  * thus cannot be copyrighted. This software/database is freely available
10  * to the public for use. The National Library of Medicine and the U.S.
11  * Government have not placed any restriction on its use or reproduction.
12  *
13  * Although all reasonable efforts have been taken to ensure the accuracy
14  * and reliability of the software and data, the NLM and the U.S.
15  * Government do not and cannot warrant the performance or results that
16  * may be obtained by using this software or data. The NLM and the U.S.
17  * Government disclaim all warranties, express or implied, including
18  * warranties of performance, merchantability or fitness for any particular
19  * purpose.
20  *
21  * Please cite the author in any work or product based on this material.
22  *
23  * ===========================================================================
24  *
25  * Author: Alexey Dobronadezhdin, NCBI
26  *
27  * File Description:
28  * hooks reading Bioseq_set from ASN.1 files containing Seq-submit
29  *
30  * ===========================================================================
31  */
32 
33 #include <ncbi_pch.hpp>
34 
35 #include <serial/objistr.hpp>
36 #include <serial/objostr.hpp>
37 #include <serial/objectio.hpp>
38 
42 
43 #include "read_hooks.hpp"
44 
46 
47 static void WriteClassMember(CObjectOStream& out, const CObjectInfoMI& member)
48 {
49  out.BeginClassMember(member.GetMemberInfo()->GetId());
50  out.WriteObject(member.GetMember().GetObjectPtr(), member.GetMemberInfo()->GetTypeInfo());
51  out.EndClassMember();
52 }
53 
54 ////////////////////////////////////////////////
55 // class CReadSubmitBlockHook
57  m_out(out),
58  m_handler(handler)
59 {
60 }
61 
63 {
65  m_out.WriteFileHeader(seqSubmitType);
66 
67  const CClassTypeInfo* classTypeInfo = CTypeConverter<CClassTypeInfo>::SafeCast(seqSubmitType);
68  m_out.BeginClass(classTypeInfo);
69 
70  // writing 'sub' member
71  in.ReadClassMember(member);
72 
73  // Is there another way to get pointer to object?
74  CSeq_submit* submit = reinterpret_cast<CSeq_submit*>(member.GetClassObject().GetObjectPtr());
75 
76  if (submit && submit->IsSetSub()) {
78  }
79 
80  WriteClassMember(m_out, member);
81 
82  // start writing 'data' member
83  const CMemberInfo* memInfo = classTypeInfo->GetMemberInfo("data");
84  m_out.BeginClassMember(memInfo->GetId());
85 
88 
89  m_out.PushFrame(CObjectStackFrame::eFrameChoice, choiceType, &memInfo->GetId());
90  m_out.BeginChoice(choiceType);
91 
92  // start writing 'entrys'
93  const CVariantInfo* variantInfo = choiceType->GetVariantInfo("entrys");
94 
96  m_out.BeginChoiceVariant(choiceType, variantInfo->GetId());
97 
99  m_out.BeginContainer(containerType);
100 }
101 
102 
104 {
105  CObjectTypeInfo bioseqTypeInfo = CType<CBioseq_set>();
106  return bioseqTypeInfo.FindMember("class");
107 }
108 
109 static void MidWritingSet(CObjectOStream& out, const CObjectInfo& obj)
110 {
111  // Skip all members before 'class' member - they have been already written by 'class' hook function
112  CObjectInfoMI item = obj.BeginMembers();
113  for (; item; ++item) {
114  if (item.GetItemInfo()->GetId().GetName() == "class")
115  break;
116  }
117 
118  // Write all members after ''class' member and before 'seq-set' member
119  for (++item; item; ++item) {
120  if (item.GetItemInfo()->GetId().GetName() == "seq-set")
121  break;
122  if (item.IsSet())
123  WriteClassMember(out, item);
124  }
125 }
126 
127 ////////////////////////////////////////////////
128 // class CReadSetHook
130 {
131 public:
134  m_out(out),
135  m_level(0)
136  {
137  }
138 
139  void ReadClassMember(CObjectIStream& in, const CObjectInfoMI& member) override
140  {
141  ++m_level;
142  if (m_level == 1) {
144 
145  // Start writing 'set'
147 
149  m_out.BeginContainer(containerType);
150 
151  // Read each element separately to a local TSeqEntry,
152  // process it somehow, and... not store it in the container.
153  for (CIStreamContainerIterator i(in, member); i; ++i) {
154 
155  CRef<CSeq_entry> entry(new CSeq_entry);
156  i >> *entry;
157 
158  m_handler.HandleSeqEntry(entry);
159 
161  m_out.WriteObject(entry, entry->GetThisTypeInfo());
163  }
164 
165  // Complete writing 'set'
168  } else {
169  // standard read
170  in.ReadClassMember(member);
171  }
172 
173  --m_level;
174  }
175 
176 private:
179  size_t m_level;
180 };
181 
182 
183 ////////////////////////////////////////////////
184 // class CReadBioseqsetClassHook
185 // needs to determine 'class' of the next bioseq_set
186 
188 {
189  CObjectTypeInfo entryTypeInfo = CType<CSeq_entry>();
190 
191  out.BeginContainerElement(entryTypeInfo.GetTypeInfo()); // begins the next seq-entry
192 
193  const CChoiceTypeInfo* choiceType = entryTypeInfo.GetChoiceTypeInfo();
194  out.BeginChoice(choiceType);
195 
196  // start writing entries
197  const CVariantInfo* variantInfo = choiceType->GetVariantInfo("set");
198  out.BeginChoiceVariant(choiceType, variantInfo->GetId());
199 
200  const CClassTypeInfo* classType = CTypeConverter<CClassTypeInfo>::SafeCast(variantInfo->GetTypeInfo());
201  out.BeginClass(classType);
202 }
203 
204 static void EndWritingSet(CObjectOStream& out, const CObjectInfo& obj)
205 {
206  // Skip all members before 'seq-set' member - they have been already written by 'class' hook function
207  CObjectInfoMI item = obj.BeginMembers();
208  for (; item; ++item) {
209  if (item.GetItemInfo()->GetId().GetName() == "seq-set")
210  break;
211  }
212 
213  // Write all members after 'seq-set' member
214  for (++item; item; ++item) {
215  if (item.IsSet())
216  WriteClassMember(out, item);
217  }
218 
219  out.EndClass();
220  out.EndChoiceVariant();
221  out.EndChoice();
222  out.EndContainerElement();
223 }
224 
226 {
227 public:
229  m_inside(false),
230  m_entryHook(entryHook),
231  m_out(out)
232  {
233  }
234 
235  void ReadClassMember(CObjectIStream& in, const CObjectInfoMI& member) override
236  {
237  in.ReadClassMember(member);
238 
239  if (! m_inside) {
240  m_inside = true;
241 
242  int val = member.GetMember().GetPrimitiveValueInt();
245  const CObjectInfo& oi = member.GetClassObject();
246  for (CObjectInfoMI item = oi.BeginMembers(); item; ++item) {
247  if (item.GetItemInfo()->GetId().GetName() == "class")
248  break;
249  if (item.IsSet())
250  WriteClassMember(m_out, item);
251  }
253  WriteClassMember(m_out, member);
254  }
255  }
256  }
257 
258 private:
259  bool m_inside;
262 };
263 
265  m_handler(handler),
266  m_out(out),
267  m_isGenbank(false)
268 {
269 }
270 
272 {
273  CObjectTypeInfoMI bioseqsetClassInfo = GetBioseqsetClassTypeInfo();
274 
275  for (CIStreamContainerIterator i(in, obj); i; ++i) {
276  m_isGenbank = false;
277  bioseqsetClassInfo.SetLocalReadHook(in, new CReadBioseqsetClassHook(*this, m_out));
278 
279  CRef<CSeq_entry> entry(new CSeq_entry);
280  i >> *entry;
281 
282  if (m_isGenbank) {
283  CBioseq_set& bio_set = entry->SetSet();
284  EndWritingSet(m_out, CObjectInfo(&bio_set, bio_set.GetThisTypeInfo()));
285  x_SetBioseqsetHook(in, false);
286  } else {
287  m_handler.HandleSeqEntry(entry);
288 
290  m_out.WriteObject(entry, entry->GetThisTypeInfo());
292  }
293 
294  bioseqsetClassInfo.ResetLocalReadHook(in);
295  }
296 }
297 
299 {
300  CObjectTypeInfo bioseqsetTypeInfo = CType<CBioseq_set>();
301 
302  CObjectTypeInfoMI setMember = bioseqsetTypeInfo.FindMember("seq-set");
303 
304  if (isSet)
305  setMember.SetLocalReadHook(in, new CReadSetHook(m_handler, m_out));
306  else
307  setMember.ResetLocalReadHook(in);
308 
309  m_isGenbank = true;
310 }
311 
User-defined methods of the data storage class.
User-defined methods of the data storage class.
Interface for handling Seq-entry objects.
virtual bool HandleSeqEntry(CRef< CSeq_entry > &entry)=0
user code for handling a Seq-entry goes here.
Reading (iterating through) elements of containers (SET OF, SEQUENCE OF).
Definition: objectio.hpp:164
CObjectIStream –.
Definition: objistr.hpp:93
CObjectInfoMI –.
Definition: objectiter.hpp:432
CObjectInfo –.
Definition: objectinfo.hpp:597
CObjectOStream –.
Definition: objostr.hpp:83
CObjectTypeInfoMI –.
Definition: objectiter.hpp:246
CObjectTypeInfo –.
Definition: objectinfo.hpp:94
CReadBioseqsetClassHook(CReadEntryHook &entryHook, CObjectOStream &out)
Definition: read_hooks.cpp:228
void ReadClassMember(CObjectIStream &in, const CObjectInfoMI &member) override
This method will be called at approriate time when the object of requested type is to be read.
Definition: read_hooks.cpp:235
CObjectOStream & m_out
Definition: read_hooks.cpp:261
CReadEntryHook & m_entryHook
Definition: read_hooks.cpp:260
Read hook for data member of a containing object (eg, SEQUENCE)
Definition: objhook.hpp:78
CReadEntryHook(CGBReleaseFile::ISeqEntryHandler &handler, CObjectOStream &out)
Definition: read_hooks.cpp:264
CObjectOStream & m_out
Definition: read_hooks.hpp:38
CGBReleaseFile::ISeqEntryHandler & m_handler
Definition: read_hooks.hpp:37
friend class CReadBioseqsetClassHook
Definition: read_hooks.hpp:43
void x_SetBioseqsetHook(CObjectIStream &in, bool isSet)
Definition: read_hooks.cpp:298
void ReadObject(CObjectIStream &in, const CObjectInfo &obj) override
This method will be called at approriate time when the object of requested type is to be read.
Definition: read_hooks.cpp:271
void ReadClassMember(CObjectIStream &in, const CObjectInfoMI &member) override
This method will be called at approriate time when the object of requested type is to be read.
Definition: read_hooks.cpp:139
CObjectOStream & m_out
Definition: read_hooks.cpp:178
CReadSetHook(CGBReleaseFile::ISeqEntryHandler &handler, CObjectOStream &out)
Definition: read_hooks.cpp:132
CGBReleaseFile::ISeqEntryHandler & m_handler
Definition: read_hooks.cpp:177
size_t m_level
Definition: read_hooks.cpp:179
void ReadClassMember(CObjectIStream &in, const CObjectInfoMI &member) override
This method will be called at approriate time when the object of requested type is to be read.
Definition: read_hooks.cpp:62
CObjectOStream & m_out
Definition: read_hooks.hpp:26
ISubmitBlockHandler & m_handler
Definition: read_hooks.hpp:27
CReadSubmitBlockHook(ISubmitBlockHandler &handler, CObjectOStream &out)
Definition: read_hooks.cpp:56
Definition: Seq_entry.hpp:56
virtual bool HandleSubmitBlock(CSubmit_block &block)=0
user code for handling a Submit-block goes here.
void(*)(CSeq_entry_Handle seh, IWorkbench *wb, const CSerialObject &obj) handler
std::ofstream out("events_result.xml")
main entry point for tests
#define false
Definition: bool.h:36
const CMemberId & GetId(void) const
const string & GetName(void) const
TTypeInfo GetTypeInfo(void) const
const CTypeInfo * TTypeInfo
Definition: serialdef.hpp:62
virtual const CTypeInfo * GetThisTypeInfo(void) const =0
static const TObjectType * SafeCast(TTypeInfo type)
Definition: serialutil.hpp:76
static TTypeInfo GetTypeInfo(void)
Definition: objecttype.hpp:85
virtual void EndClassMember(void)
Definition: objostr.cpp:888
virtual void BeginClass(const CClassTypeInfo *classInfo)=0
virtual void WriteFileHeader(TTypeInfo type)
Definition: objostr.cpp:680
virtual void BeginChoice(const CChoiceTypeInfo *choiceType)
Definition: objostr.cpp:1029
TFrame & PushFrame(EFrameType type, TTypeInfo typeInfo, TConstObjectPtr objectPtr=0)
void ResetLocalReadHook(CObjectIStream &stream) const
Definition: objectiter.cpp:107
virtual void EndContainerElement(void)
Definition: objostr.cpp:806
const CMemberInfo * GetMemberInfo(void) const
CMemberIterator BeginMembers(void) const
Create class member iterator.
const CItemInfo * GetItemInfo(void) const
TObjectPtr GetObjectPtr(void) const
Get pointer to object.
CMemberIterator FindMember(const string &memberName) const
Find class member by its name.
CObjectInfo GetMember(void) const
Get class member data.
const CObjectInfo & GetClassObject(void) const
Get containing class data.
virtual void BeginContainer(const CContainerTypeInfo *containerType)=0
void WriteObject(const CConstObjectInfo &object)
Definition: objostr.cpp:566
TTypeInfo GetTypeInfo(void) const
int GetPrimitiveValueInt(void) const
Get data as int.
Definition: objectinfo.cpp:154
const CChoiceTypeInfo * GetChoiceTypeInfo(void) const
Definition: objectinfo.cpp:67
virtual void BeginChoiceVariant(const CChoiceTypeInfo *choiceType, const CMemberId &id)=0
void SetLocalReadHook(CObjectIStream &stream, CReadClassMemberHook *hook) const
Definition: objectiter.cpp:96
virtual void BeginContainerElement(TTypeInfo elementType)
Definition: objostr.cpp:802
virtual void BeginClassMember(const CMemberId &id)=0
virtual void EndContainer(void)
Definition: objostr.cpp:798
bool IsSet(void) const
Is member assigned a value.
#define END_NCBI_SCOPE
End previously defined NCBI scope.
Definition: ncbistl.hpp:103
#define BEGIN_NCBI_SCOPE
Define ncbi namespace.
Definition: ncbistl.hpp:100
TTypeInfo GetPointedType(void) const
const CMemberInfo * GetMemberInfo(TMemberIndex index) const
const CVariantInfo * GetVariantInfo(TMemberIndex index) const
TSet & SetSet(void)
Select the variant.
Definition: Seq_entry_.cpp:130
@ eClass_genbank
converted genbank
void SetSub(TSub &value)
Assign a value to Sub data member.
bool IsSetSub(void) const
Check if a value has been assigned to Sub data member.
int i
std::istream & in(std::istream &in_, double &x_)
static CObjectTypeInfoMI GetBioseqsetClassTypeInfo()
Definition: read_hooks.cpp:103
static void EndWritingSet(CObjectOStream &out, const CObjectInfo &obj)
Definition: read_hooks.cpp:204
static void StartWritingSet(CObjectOStream &out)
Definition: read_hooks.cpp:187
static void WriteClassMember(CObjectOStream &out, const CObjectInfoMI &member)
Definition: read_hooks.cpp:47
static void MidWritingSet(CObjectOStream &out, const CObjectInfo &obj)
Definition: read_hooks.cpp:109
Modified on Tue Apr 16 20:08:48 2024 by modify_doxy.py rev. 669887