1 // ==========================================================================
2 //                 SeqAn - The Library for Sequence Analysis
3 // ==========================================================================
4 // Copyright (c) 2006-2010, Knut Reinert, FU Berlin
5 // All rights reserved.
6 //
7 // Redistribution and use in source and binary forms, with or without
8 // modification, are permitted provided that the following conditions are met:
9 //
10 //     * Redistributions of source code must retain the above copyright
11 //       notice, this list of conditions and the following disclaimer.
12 //     * Redistributions in binary form must reproduce the above copyright
13 //       notice, this list of conditions and the following disclaimer in the
14 //       documentation and/or other materials provided with the distribution.
15 //     * Neither the name of Knut Reinert or the FU Berlin nor the names of
16 //       its contributors may be used to endorse or promote products derived
17 //       from this software without specific prior written permission.
18 //
19 // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
20 // AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
21 // IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
22 // ARE DISCLAIMED. IN NO EVENT SHALL KNUT REINERT OR THE FU BERLIN BE LIABLE
23 // FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
24 // DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
25 // SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
26 // CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
27 // LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
28 // OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH
29 // DAMAGE.
30 //
31 // ==========================================================================
32 
33 #ifndef SEQAN_HEADER_BLAST_HIT_H
34 #define SEQAN_HEADER_BLAST_HIT_H
35 
36 
37 namespace SEQAN_NAMESPACE_MAIN
38 {
39 
40 template<typename TBlastHsp, typename TStoreSpec>
41 class BlastHit;
42 
43 
44 /**
45 .Class.BlastHit:
46 ..cat:Blast
47 ..summary:Object for storing Blast hits.
48 ..signature:BlastHit<TBlastHsp, TSpec>
49 ..param.TBlastHsp:The type of HSPs that are stored.
50 ..param.TSpec:The specializing type.
51 ...type:Spec.StreamReport
52 ...type:Spec.StoreReport
53 ..remarks:Use Metafunction.Hit to get the BlastHit type used in a BlastReport object.
54 ..include:seqan/blast.h
55 */
56 template<typename TBlastHsp, typename TSpec>
57 class BlastHit<TBlastHsp, StoreReport<TSpec> >
58 {
59 	public:
60 		String<char> name;
61 		unsigned int length; //length of whole sequence
62 
63 		String<TBlastHsp> hsps;
64 
BlastHit()65 		BlastHit()
66 		{
67 		SEQAN_CHECKPOINT
68 		}
69 
70 		//BlastHit(String<char> name, unsigned int len)
71 		//{
72 		//	name = name;
73 		//	length = length;
74 		//	clear(hsps);
75 		//}
76 
BlastHit(BlastHit const & other)77 		BlastHit(BlastHit const& other)
78 		{
79 		SEQAN_CHECKPOINT
80 			assign(hsps,other.hsps);
81 			name = other.name;
82 			length = other.length;
83 		}
84 
85 		BlastHit & operator = (BlastHit const & other)
86 		{
87 		SEQAN_CHECKPOINT
88 			assign(hsps,other.hsps);
89 			name = other.name;
90 			length = other.length;
91 			return *this;
92 		}
93 
~BlastHit()94 		~BlastHit()
95 		{
96 		}
97 
98 };
99 
100 
101 
102 template<typename TBlastHsp, typename TSpec>
103 inline void
clear(BlastHit<TBlastHsp,StoreReport<TSpec>> & blastHit)104 clear(BlastHit<TBlastHsp, StoreReport<TSpec> >& blastHit)
105 {
106 SEQAN_CHECKPOINT
107 
108 	for(unsigned int i = 0; i < length(blastHit.hsps); ++i)
109 		clear(blastHit.hsps[i]);
110 	resize(blastHit.hsps,0);
111 	resize(blastHit.name,0);
112 }
113 
114 
115 template<typename TBlastHsp, typename TStoreSpec>
116 inline String<char> &
name(BlastHit<TBlastHsp,TStoreSpec> & blastHit)117 name(BlastHit<TBlastHsp, TStoreSpec>& blastHit)
118 {
119 SEQAN_CHECKPOINT
120 	return blastHit.name;
121 }
122 
123 template<typename TBlastHsp, typename TStoreSpec>
124 inline String<char>
getName(BlastHit<TBlastHsp,TStoreSpec> & blastHit)125 getName(BlastHit<TBlastHsp, TStoreSpec>& blastHit)
126 {
127 SEQAN_CHECKPOINT
128 	return blastHit.name;
129 }
130 
131 template<typename TBlastHsp, typename TStoreSpec>
132 inline unsigned int &
length(BlastHit<TBlastHsp,TStoreSpec> & blastHit)133 length(BlastHit<TBlastHsp, TStoreSpec>& blastHit)
134 {
135 SEQAN_CHECKPOINT
136 	return blastHit.length;
137 }
138 
139 template<typename TBlastHsp, typename TStoreSpec>
140 inline unsigned int
getLength(BlastHit<TBlastHsp,TStoreSpec> & blastHit)141 getLength(BlastHit<TBlastHsp, TStoreSpec>& blastHit)
142 {
143 SEQAN_CHECKPOINT
144 	return blastHit.length;
145 }
146 
147 // for StoreReport only
148 template<typename TBlastHsp, typename TSpec>
149 inline unsigned int
numHsps(BlastHit<TBlastHsp,StoreReport<TSpec>> & blastHit)150 numHsps(BlastHit<TBlastHsp, StoreReport<TSpec> >& blastHit)
151 {
152 SEQAN_CHECKPOINT
153 	return length(blastHit.hsps);
154 }
155 
156 
157 /////////////////////////////////////////////////////////////////////
158 
159 //parse BlastHit
160 template<typename TFile, typename TChar, typename TBlastHit>
161 inline typename Position<TFile>::Type
_parseBlastHit(TFile & file,TChar & c,TBlastHit & hit)162 _parseBlastHit(TFile & file,
163 			TChar & c,
164 			TBlastHit & hit)
165 {
166 	typedef typename Position<TFile>::Type TPosition;
167 	typedef typename Hsp<TBlastHit>::Type TBlastHsp;
168 
169 	String<char> pword;
170 	int pint;
171 	TPosition start_pos,act_pos;
172 	act_pos = _streamTellG(file);
173 
174 	if(_parseUntilBeginLine(file,c,'>'))
175 	{
176 		start_pos = _streamTellG(file);
177 		c = _streamGet(file);
178 		pword = _parseReadWord(file, c);
179 		while (!_streamEOF(file) && c != '\n' && c != '\r')
180 			pword += _parseReadWord(file, c);
181 		if(pword[length(pword)-1] == ' ')
182 			resize(pword,length(pword)-1);
183 		hit.name = pword;
184 		_parseSkipWhitespace(file,c);
185 		String<char> search = "Length";
186 		if(_parseUntilBeginLine(file,c,search,6))
187 		{
188 			_parseSkipWhitespace(file,c);
189 			if(c == '=')
190 				c = _streamGet(file);
191 			_parseSkipWhitespace(file,c);
192 			pint = _parseReadNumber(file, c);
193 			hit.length = pint;
194 		}
195 //		TPosition temp = _streamTellG(file);
196 		//foreach Hsp
197 		//if(_parseUntilBeginLine(file,c,'S') && _parseReadWord(file,c)=="Score")
198 		search = "Score";
199 		if(_parseUntilBeginLine(file,c,search,5))
200 		{
201 			//c = _streamGet(file);
202 			bool in_hit = true;
203 			TPosition act_hsp_pos,next_hsp_pos;
204 			while(in_hit){
205 				act_hsp_pos = _streamTellG(file);
206 				TBlastHsp hsp;
207 				next_hsp_pos = _parseBlastHsp(file,c,hsp);
208 				//resize(hit.hsps, length(hit.hsps)+1);
209 				//hit.hsps[length(hit.hsps)-1] = hsp;
210 				appendValue(hit.hsps,hsp);
211 				//append(hit.hsps,hsp);
212 				if(next_hsp_pos == act_hsp_pos)
213 					in_hit = false;
214 				if(next_hsp_pos == (TPosition)0)
215 					return (TPosition) 0;
216 			}
217 			_streamSeekG(file,next_hsp_pos);
218 			c = _streamGet(file);
219 			if(_parseUntilBeginLine(file,c,'>'))
220 				return _streamTellG(file);
221 		}
222 
223 
224 	}//end hit
225 	_streamSeekG(file,act_pos);
226 	return act_pos;
227 }
228 
229 
230 
231 
232 
233 
234 ////////////////////////// MetaFunctions ////////////////////////////////
235 
236 template<typename TBlastHsp, typename TStoreSpec>
237 struct Value<BlastHit<TBlastHsp, TStoreSpec> >
238 {
239 	typedef BlastHit<TBlastHsp,TStoreSpec> Type;
240 };
241 
242 template<typename TBlastHsp, typename TStoreSpec>
243 struct Value<BlastHit<TBlastHsp, TStoreSpec> const>
244 {
245 	typedef BlastHit<TBlastHsp,TStoreSpec> Type;
246 };
247 
248 template<typename TBlastHsp, typename TStoreSpec>
249 struct Hit<BlastHit<TBlastHsp, TStoreSpec> >
250 {
251 	typedef BlastHit<TBlastHsp,TStoreSpec> Type;
252 };
253 
254 template<typename TBlastHsp, typename TStoreSpec>
255 struct Hit<BlastHit<TBlastHsp, TStoreSpec> const>
256 {
257 	typedef BlastHit<TBlastHsp,TStoreSpec> Type;
258 };
259 
260 
261 
262 template<typename TBlastHsp, typename TStoreSpec>
263 struct Hsp<BlastHit<TBlastHsp, TStoreSpec> >
264 {
265 	typedef TBlastHsp Type;
266 };
267 
268 template<typename TBlastHsp, typename TStoreSpec>
269 struct Hsp<BlastHit<TBlastHsp, TStoreSpec> const>
270 {
271 	typedef TBlastHsp Type;
272 };
273 
274 
275 /////////////komisch///////////
276 
277 	template<typename TBlastHsp, typename TStoreSpec>
278 	struct Hsp<String<BlastHit<TBlastHsp, TStoreSpec> > >
279 	{
280 		typedef TBlastHsp Type;
281 	};
282 
283 	template<typename TBlastHsp, typename TStoreSpec>
284 	struct Hsp<String<BlastHit<TBlastHsp, TStoreSpec> const> >
285 	{
286 		typedef TBlastHsp Type;
287 	};
288 
289 	template<typename TBlastSpec, typename TStoreSpec>
290 	struct Hsp<String<BlastHsp<TBlastSpec, TStoreSpec> > >
291 	{
292 		typedef BlastHsp<TBlastSpec, TStoreSpec> Type;
293 	};
294 
295 	template<typename TBlastSpec, typename TStoreSpec>
296 	struct Hsp<String<BlastHsp<TBlastSpec, TStoreSpec> const> >
297 	{
298 		typedef BlastHsp<TBlastSpec, TStoreSpec> Type;
299 	};
300 
301 
302 
303 
304 }// namespace SEQAN_NAMESPACE_MAIN
305 
306 #endif //#ifndef SEQAN_HEADER_...
307