Imported Upstream version 0.12.7
[bowtie.git] / SeqAn-1.1 / seqan / file / file_format_cgviz.h
1  /*==========================================================================
2                 SeqAn - The Library for Sequence Analysis
3                           http://www.seqan.de 
4  ============================================================================
5   Copyright (C) 2007
6
7   This library is free software; you can redistribute it and/or
8   modify it under the terms of the GNU Lesser General Public
9   License as published by the Free Software Foundation; either
10   version 3 of the License, or (at your option) any later version.
11
12   This library is distributed in the hope that it will be useful,
13   but WITHOUT ANY WARRANTY; without even the implied warranty of
14   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15   Lesser General Public License for more details.
16
17  ============================================================================
18   $Id: file_format_cgviz.h,v 1.1 2008/08/25 16:20:03 langmead Exp $
19  ==========================================================================*/
20
21 #ifndef SEQAN_HEADER_FILE_CGVIZ_H
22 #define SEQAN_HEADER_FILE_CGVIZ_H
23
24 namespace SEQAN_NAMESPACE_MAIN
25 {
26
27 //////////////////////////////////////////////////////////////////////////////
28 // File Formats - CGViz
29 //////////////////////////////////////////////////////////////////////////////
30
31
32 /**
33 .Tag.File Format.tag.CGViz:
34         CGViz file format for sequences. Only output.
35 */
36 struct TagCGViz_;
37 typedef Tag<TagCGViz_> const CGViz;
38
39 /////////////////////////////////////////////////////////////////////////
40
41
42
43 //////////////////////////////////////////////////////////////////////////////
44 // goNext
45 //////////////////////////////////////////////////////////////////////////////
46
47 template <typename TFile>
48 void goNext(TFile & file, CGViz) {
49         SEQAN_CHECKPOINT
50         SEQAN_ASSERT(!_streamEOF(file))
51         
52         return;
53 }
54
55
56
57 //////////////////////////////////////////////////////////////////////////////
58 // write
59 //////////////////////////////////////////////////////////////////////////////
60
61 template <typename TFile, typename TStringContainer, typename TSource, typename TSpec>
62 void _write_impl(TFile& target, Align<TSource, TSpec>& align, TStringContainer& ids, CGViz) {
63         SEQAN_CHECKPOINT
64
65         typedef Align<TSource, TSpec> const TAlign;
66         typedef typename Row<TAlign>::Type TRow;
67         typedef typename Position<typename Rows<TAlign>::Type>::Type TRowsPosition;
68         typedef typename Position<TAlign>::Type TPosition;
69         TRowsPosition row_count = length(rows(align));
70         if (row_count < 2) return;
71
72         unsigned int pair=1;
73         unsigned int count=0;
74         for(TRowsPosition i=0;i<row_count-1;++i) {
75                 for(TRowsPosition j=i+1;j<row_count;++j) {
76                         
77                         // Print header
78                         _streamWrite(target, "{DATA dat"); 
79                         _streamPutInt(target, pair);
80                         _streamPut(target, '\n');
81                         _streamWrite(target, "[__GLOBAL__] dimension=2:\n"); 
82                                                 
83                         TPosition begin_ = beginPosition(cols(align));
84                         TPosition end_ = endPosition(cols(align));
85                 
86                         bool match = false;
87                         while(begin_ < end_) {
88                                 if ((row(align, i)[begin_]==row(align, j)[begin_]) && (row(align, i)[begin_]!='-')) {
89                                         if (!match) {
90                                                 match=true;
91                                                 _streamPutInt(target, toSourcePosition(row(align,i),begin_+1));
92                                                 _streamPut(target, ' ');
93                                                 _streamPutInt(target, toSourcePosition(row(align,j),begin_+1));
94                                                 _streamPut(target, ' ');
95                                         }
96                                 }
97                                 if ((row(align, i)[begin_]!=row(align, j)[begin_]) || (row(align, i)[begin_]=='-') || (row(align, j)[begin_]=='-')) {
98                                         if (match) {
99                                                 _streamPutInt(target, toSourcePosition(row(align,i),begin_));
100                                                 _streamPut(target, ' ');
101                                                 _streamPutInt(target, toSourcePosition(row(align,j),begin_));
102                                                 _streamPut(target, '\n');
103                                                 match=false;
104                                         }
105                                 }
106                                 begin_++;
107                         }
108                         if (match) {
109                                 _streamPutInt(target, toSourcePosition(row(align,i),begin_));
110                                 _streamPut(target, ' ');
111                                 _streamPutInt(target, toSourcePosition(row(align,j),begin_));
112                                 _streamPut(target, '\n');
113                                 match=false;
114                         }
115                         _streamPut(target, '}');
116                         _streamPut(target, '\n');
117
118                         // Write footer
119                         _streamWrite(target, "{GLYPH Glyph");
120                         _streamPutInt(target, pair);
121                         _streamPut(target, '\n');
122                         _streamWrite(target, "drawerName=Lines\n");
123                         _streamWrite(target, "lineWidth=3\n");
124                         _streamPut(target, '}');
125                         _streamPut(target, '\n');
126                         _streamWrite(target, "{PANE Pane");
127                         _streamPutInt(target, pair);
128                         _streamPut(target, '\n');
129                         _streamWrite(target, "uLabel=");
130                         _streamWrite(target, getValue(ids,i));
131                         _streamPut(target, '\n');
132                         _streamWrite(target, "uStop=");
133                         _streamPutInt(target, length(source(row(align,i))));
134                         _streamPut(target, '\n');
135                         _streamWrite(target, "vLabel=");
136                         _streamWrite(target, getValue(ids,j));
137                         _streamPut(target, '\n');
138                         _streamWrite(target, "vStop=");
139                         _streamPutInt(target, length(source(row(align,j))));
140                         _streamPut(target, '\n');
141                         _streamPut(target, '}');
142                         _streamPut(target, '\n');
143                         _streamWrite(target, "{WINDOW Window");
144                         _streamPutInt(target, pair);
145                         _streamPut(target, '\n');
146                         _streamPut(target, '}');
147                         _streamPut(target, '\n');
148                         _streamWrite(target, "{FEEDER Feeder<");
149                         _streamPutInt(target, pair);
150                         _streamPut(target, '>');
151                         _streamPut(target, ' ');
152                         _streamPutInt(target, count);
153                         _streamPut(target, ' ');
154                         _streamPutInt(target, count+1);
155                         _streamPut(target, '\n');
156                         _streamPut(target, '}');
157                         _streamPut(target, '\n');
158                         ++count;
159                         _streamWrite(target, "{THREADER Threader<");
160                         _streamPutInt(target, pair);
161                         _streamPut(target, '>');
162                         _streamPut(target, ' ');
163                         _streamPutInt(target, count);
164                         _streamPut(target, ' ');
165                         _streamPutInt(target, count+1);
166                         _streamPut(target, '\n');
167                         _streamPut(target, '}');
168                         _streamPut(target, '\n');
169                         ++count;
170                         _streamWrite(target, "{ANCHOR Anchor<");
171                         _streamPutInt(target, pair);
172                         _streamPut(target, '>');
173                         _streamPut(target, ' ');
174                         _streamPutInt(target, count);
175                         _streamPut(target, ' ');
176                         _streamPutInt(target, count+1);
177                         _streamPut(target, '\n');
178                         _streamPut(target, '}');
179                         _streamPut(target, '\n');
180                         count+=2;
181                         ++pair;
182                 }
183         }
184 }
185
186
187 //____________________________________________________________________________
188
189 template <typename TFile, typename TSource, typename TSpec>
190 void write(TFile & file, Align<TSource, TSpec>& align, CGViz) {
191         SEQAN_CHECKPOINT
192         _write_impl(file, align, String<String<char> >(), CGViz());
193 }
194
195 //____________________________________________________________________________
196
197 template <typename TFile, typename TStringContainer, typename TSource, typename TSpec>
198 void write(TFile & file, Align<TSource, TSpec> & align, TStringContainer& ids, CGViz) {
199         SEQAN_CHECKPOINT
200         _write_impl(file, align, ids, CGViz());
201 }
202
203
204 //VisualC++ const array bug workaround
205 template <typename TFile, typename TStringContainer, typename TSource, typename TSpec>
206 void write(TFile & file, Align<TSource, TSpec>* align, TStringContainer & ids, CGViz) {
207         SEQAN_CHECKPOINT
208         _write_impl(file, align, ids, CGViz());
209 }
210
211 //____________________________________________________________________________
212
213 template <typename TFile, typename TStringContainer, typename TSource, typename TSpec, typename TMeta>
214 void write(TFile & file, Align<TSource, TSpec> & align, TStringContainer& ids, TMeta &, CGViz) {
215         SEQAN_CHECKPOINT
216         _write_impl(file, align, ids, CGViz());
217 }
218
219
220
221 //////////////////////////////////////////////////////////////////////////////
222 } //namespace SEQAN_NAMESPACE_MAIN
223
224 //////////////////////////////////////////////////////////////////////////////
225
226 #endif //#ifndef SEQAN_HEADER_...