File: SAM_entry.cpp

package info (click to toggle)
trinityrnaseq 2.11.0%2Bdfsg-6
  • links: PTS, VCS
  • area: main
  • in suites: bookworm, bullseye, sid
  • size: 417,528 kB
  • sloc: perl: 48,420; cpp: 17,749; java: 12,695; python: 3,124; sh: 1,030; ansic: 983; makefile: 688; xml: 62
file content (247 lines) | stat: -rw-r--r-- 4,115 bytes parent folder | download | duplicates (3)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
#include <stdlib.h>
#include "SAM_entry.hpp"
#include "string_util.hpp"
#include <iostream>
#include <sstream>
#include "Cigar.hpp"

using namespace Cigar;

// constructor
SAM_entry::SAM_entry(string sam_line) {

  line = sam_line;

  string_util::tokenize(line, tokens, "\t");



}

string SAM_entry::get_sam_string() {
  
  // reconstruct the line
  stringstream sam_line;
  
  for (unsigned int i = 0; i < tokens.size(); i++) {
	sam_line << tokens[i];
	
	if (i < tokens.size() - 1) {
	  sam_line << "\t";
	}
  }
  
  return(sam_line.str());  
}


string SAM_entry::toString() {

  return(get_sam_string());
}



string SAM_entry::get_read_name() {
  return(tokens[0]);
}


string SAM_entry::get_full_read_name() {

    if (is_paired()) {
        
        string core_read_name = get_read_name();
        if (is_first_in_pair()) {
            return(core_read_name + "/1");
        }
        else {
            // assert is second in pair
            return(core_read_name + "/2"); 
        }
        

    }
    else {
        return(get_read_name());
    }


}


string SAM_entry::get_scaffold_name() {
  return(tokens[2]);
}

unsigned long SAM_entry::get_scaffold_position() {
  
  return(atol(tokens[3].c_str()));
}

void SAM_entry::set_scaffold_position(unsigned long position) {
  
  stringstream s;
  s << position;

  tokens[3] = s.str();
  
}

string SAM_entry::get_cigar_alignment() {
  return(tokens[5]);
}

void SAM_entry::set_cigar_alignment(string cigar_string) {
  tokens[5] = cigar_string;
}


vector<alignment_segment> SAM_entry::get_alignment_coords() {

  vector<alignment_segment> segs;

  unsigned long genome_lend = get_scaffold_position();

  string alignment = get_cigar_alignment();

  unsigned long query_lend = 0;

  genome_lend--; // move pointer just before first position

  vector<t_cigar> cigar_structs = parse_cigar(alignment);
  
  for (unsigned int i=0; i < cigar_structs.size(); i++) {

	t_cigar cigar_struct = cigar_structs[i];
	
	unsigned long len = cigar_struct.len;
	char code = cigar_struct.code;

	

	if (code == 'M') {
	  
	  alignment_segment s;
	  
	  unsigned long genome_rend = genome_lend + len;
	  unsigned long query_rend = query_lend + len;
	  
	  s.genome_lend = genome_lend + 1;
	  s.genome_rend = genome_rend;
	  s.query_lend = query_lend + 1;
	  s.query_rend = query_rend;

	  segs.push_back(s);

	  genome_lend = genome_rend;
	  query_lend = query_rend;
	  
	}
	else if (code == 'D' || code == 'N') {
	  genome_lend += len;
	  
	}
	else if (code == 'I' || code == 'S' || code == 'H') {
	  query_lend += len;
	}
	else {

	  stringstream errmsg;
	  errmsg << "Error, cannot parse cigar code: " << code;
	  throw(errmsg.str());
	}
	
  }
  
  return(segs);
}

string SAM_entry::get_mate_scaffold_name() {
  return(tokens[6]);
}

unsigned long SAM_entry::get_mate_scaffold_position() {
  return(atol(tokens[7].c_str()));
}

unsigned int SAM_entry::get_mapping_quality() {
  return(atoi(tokens[4].c_str()));
}

string SAM_entry::get_sequence() {
  return(tokens[9]);
}

string SAM_entry::get_quality_scores() {
  return(tokens[10]);
}

//***** FLAG Processing ******//

unsigned int SAM_entry::get_flag() {
  return(atoi(tokens[1].c_str()));
}

bool SAM_entry::is_paired() {
  return(get_flag() & 0x0001);
}

bool SAM_entry::is_proper_pair() {
  return(get_flag() & 0x0002);
}

bool SAM_entry::is_query_unmapped() {
  return(get_flag() & 0x0004);
}

bool SAM_entry::is_mate_unmapped() {
  return(get_flag() & 0x0008);
}

char SAM_entry::get_query_strand() {
  if (get_flag() & 0x0010) {
	return('-');
  }
  else {
	return('+');
  }
}

char SAM_entry::get_query_transcribed_strand() {
  
  char strand = get_query_strand();
  
  if (is_paired() && is_first_in_pair()) {

	char transcribed_strand = (strand == '+') ? '-' : '+';

	return(transcribed_strand);
  }
  else {
	return (strand);
  }
  
  
  
}

char SAM_entry::get_mate_strand() {
  if (get_flag() & 0x0020) {
	return('-');
  }
  else {
	return('+');
  }
}

bool SAM_entry::is_first_in_pair() {
  return(get_flag() & 0x0040);
}

bool SAM_entry::is_second_in_pair() {
  return(get_flag() & 0x0080);
}