source: tags/arb-6.0/CONVERTALN/printable.cxx

Last change on this file was 9516, checked in by westram, 11 years ago
  • reintegrated branch 'cxx11'
  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
File size: 2.4 KB
Line 
1#include "input_format.h"
2#include "reader.h"
3#include "ali.h"
4
5#define PRTLENGTH   62
6
7static void printable_print_line(const char *id, const char *sequence, int start, int base_count, Writer& write) {
8    // print one printable line.
9    int indi, index, count, bnum, seq_length;
10
11    write.out(' ');
12    if ((bnum = str0len(id)) > 10) {
13        // truncate if length of id is greater than 10
14        for (indi = 0; indi < 10; indi++) write.out(id[indi]);
15        bnum = 1;
16    }
17    else {
18        write.out(id);
19        bnum = 10 - bnum + 1;
20    }
21    // fill in the blanks to make up 10 chars id spaces
22    seq_length = str0len(sequence);
23    if (start < seq_length)
24        for (indi = 0; indi < bnum; indi++) write.out(' ');
25    else {
26        write.out('\n');
27        return;
28    }
29    write.outf("%4d ", base_count);
30    for (index = start, count = 0; count < PRTLENGTH && index < seq_length; index++) {
31        write.out(sequence[index]);
32        count++;
33    }
34    write.out('\n');
35}
36
37void to_printable(const FormattedFile& in, const char *outf) {
38    // Convert from some format to PRINTABLE format.
39    if (!is_input_format(in.type())) {
40        throw_conversion_not_supported(in.type(), PRINTABLE);
41    }
42
43    FileWriter write(outf);
44
45    Alignment ali;
46    read_alignment(ali, in);
47
48    int total_seq = ali.get_count();
49    int maxsize   = ali.get_max_len();
50    int base_nums[total_seq];
51    for (int i = 0; i<total_seq; ++i) base_nums[i] = 0;
52
53    int current = 0;
54    while (maxsize > current) {
55        for (int indi = 0; indi < total_seq; indi++) {
56            const Seq&  seq        = ali.get(indi);
57            int         length     = seq.get_len();
58            const char *sequence   = seq.get_seq();
59            int         base_count = 0;
60            for (int index = 0; index < PRTLENGTH && (current + index) < length; index++)
61                if (!is_gapchar(sequence[index + current]))
62                    base_count++;
63
64            // check if the first char is base or not
65            int   start;
66            if (current < length && !is_gapchar(sequence[current]))
67                start = base_nums[indi] + 1;
68            else
69                start = base_nums[indi];
70
71            printable_print_line(seq.get_id(), seq.get_seq(), current, start, write);
72            base_nums[indi] += base_count;
73        }
74        current += PRTLENGTH;
75        if (maxsize > current)
76            write.out("\n\n");
77    }
78
79    write.seq_done(ali.get_count());
80    write.expect_written();
81}
82
Note: See TracBrowser for help on using the repository browser.