1+ //
2+ // Created by Daniel Olson on 08/07/24.
3+ //
4+
5+ #include " TabFileWriter.hpp"
6+ #include " repeat.hpp"
7+ #include " ultra.hpp"
8+ #include < algorithm>
9+ #include < iostream>
10+
11+ void TabFileWriter::InitializeWriter (Ultra *ultra, FILE *out_f) {
12+ owner = ultra;
13+ out = out_f;
14+ fprintf (out, " SeqID" );
15+ fprintf (out, " \t Start" );
16+ fprintf (out, " \t End" );
17+ fprintf (out, " \t Period" );
18+ fprintf (out, " \t Score" );
19+ if (owner->settings ->pval )
20+ fprintf (out, " ,PValue" );
21+ if (owner->settings ->max_consensus_period != 0 )
22+ fprintf (out, " \t Consensus" );
23+ if (owner->settings ->max_split > 0 ) {
24+ fprintf (out, " \t #Subrepeats" );
25+ fprintf (out, " \t SubrepeatStarts" );
26+ if (owner->settings ->max_consensus_period != 0 )
27+ fprintf (out, " \t SubrepeatConsensi" );
28+ }
29+
30+ if (owner->settings ->show_seq ) {
31+ fprintf (out, " \t Sequence" );
32+ }
33+
34+ fprintf (out, " \n " );
35+
36+ }
37+
38+ void TabFileWriter::WriteRepeat (RepeatRegion *repeat) {
39+
40+ // We need a better behavior here - check with Travis
41+ std::string name = repeat->sequenceName ;
42+ for (int i = 0 ; i < name.size (); ++i) {
43+ if ((name[i] >= ' a' && name[i] <= ' z' ) ||
44+ (name[i] >= ' A' && name[i] <= ' Z' ) ||
45+ (name[i] >= ' 0' && name[i] <= ' 9' ) ||
46+ name[i] == ' -' || name[i] == ' _' || name[i] == ' .' ||
47+ name[i] == ' :' || name[i] == ' *' || name[i] == ' #' ) {
48+ continue ;
49+ }
50+
51+ else {
52+ name = name.substr (0 , i);
53+ break ;
54+ }
55+
56+ /* else {
57+ name[i] = '_';
58+ }*/
59+ }
60+
61+ // Columns 1 (name) 2 (start) 3 (end) 4 (score)
62+ fprintf (out, " %s\t %lu\t %lu\t %i\t %f" , name.c_str (), repeat->sequenceStart ,
63+ repeat->sequenceStart + repeat->repeatLength , repeat->repeatPeriod , repeat->regionScore );
64+ if (owner->settings ->pval ) {
65+ fprintf (out, " ,%g" , owner->PvalForScore (repeat->regionScore ));
66+ }
67+
68+ // We need to decide what to do with the overall sequence
69+ std::string rep_con = " ." ;
70+ if (owner->settings ->max_consensus_period != 0 ) {
71+ if (owner->settings ->max_consensus_period >= repeat->repeatPeriod &&
72+ !repeat->string_consensus .empty ())
73+ rep_con = repeat->string_consensus ;
74+
75+ fprintf (out, " \t %s" , rep_con.c_str ());
76+ }
77+
78+ if (owner->settings ->max_split > 0 ) {
79+ std::string sizes = " " ;
80+ std::string starts = " 0" ;
81+ std::string consensi = " " ;
82+ int numberOfValidSplits = 0 ;
83+
84+ int cstart = 0 ;
85+ if (repeat->splits != nullptr && !repeat->splits ->empty ()) {
86+ for (int i = 0 ; i < repeat->splits ->size (); ++i) {
87+ int split_i = repeat->splits ->at (i);
88+ if (split_i > 0 ) {
89+ if (numberOfValidSplits > 0 ) {
90+ sizes.push_back (' ,' );
91+ }
92+
93+ sizes += std::to_string (split_i - cstart);
94+ starts.push_back (' ,' );
95+ starts += std::to_string (split_i);
96+
97+ ++numberOfValidSplits;
98+ cstart = split_i;
99+ }
100+ }
101+
102+ if (owner->settings ->max_consensus_period != 0 ) {
103+ for (int i = 0 ; i < repeat->consensi ->size (); ++i) {
104+ std::string con = " ." ;
105+ if (owner->settings ->max_consensus_period >= repeat->repeatPeriod ) {
106+ if (repeat->consensi != nullptr && repeat->consensi ->size () > i) {
107+ con = repeat->consensi ->at (i);
108+ }
109+ }
110+ if (i > 0 )
111+ consensi.push_back (' ,' );
112+ consensi += con;
113+ }
114+ }
115+
116+ if (numberOfValidSplits > 0 )
117+ sizes.push_back (' ,' );
118+ sizes += std::to_string (repeat->repeatLength - cstart);
119+
120+ fprintf (out, " \t %i\t %s" , numberOfValidSplits + 1 ,
121+ starts.c_str ());
122+ if (owner->settings ->max_consensus_period != 0 ) {
123+ fprintf (out, " \t %s" , consensi.c_str ());
124+ }
125+
126+ }
127+
128+ else {
129+ fprintf (out, " \t 1\t 0" );
130+ if (owner->settings ->max_consensus_period != 0 ) {
131+ fprintf (out, " \t %s" , rep_con.c_str ());
132+ }
133+ }
134+ }
135+
136+ if (owner->settings ->show_seq ) {
137+ fprintf (out, " \t %s" , repeat->sequence .c_str ());
138+ }
139+ fprintf (out, " \n " );
140+ }
141+
142+ void TabFileWriter::EndWriter () {}
0 commit comments