-
Notifications
You must be signed in to change notification settings - Fork 1
/
chain_file_utils.cpp
177 lines (159 loc) · 4.4 KB
/
chain_file_utils.cpp
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
/* Copyright (C) 2018 University of Southern California and
* Andrew D. Smith
*
* Authors: Andrew D. Smith
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*/
#include "chain_file_utils.hpp"
#include <sstream>
#include <string>
#include <iostream>
#include <fstream>
#include <vector>
using std::string;
using std::ostream;
using std::istream;
using std::vector;
using std::endl;
ostream &
operator<<(ostream &os, const aln_dat &ad) {
os << ad.size;
if (ad.dt > 0 || ad.dq > 0)
os << '\t' << ad.dt << '\t' << ad.dq;
return os;
}
istream &
operator>>(istream &is, aln_dat &ad) {
is >> ad.size;
if (is >> ad.dt)
is >> ad.dq;
else {
ad.dt = 0;
ad.dq = 0;
is.clear();
}
return is;
}
ostream &
operator<<(ostream &os, const chain_head &ch) {
return os << "chain" << ' '
<< ch.score << ' '
<< ch.tName << ' '
<< ch.tSize << ' '
<< ch.tStrand << ' '
<< ch.tStart << ' '
<< ch.tEnd << ' '
<< ch.qName << ' '
<< ch.qSize << ' '
<< ch.qStrand << ' '
<< ch.qStart << ' '
<< ch.qEnd << ' '
<< ch.id;
}
istream &
operator>>(istream &is, chain_head &ch) {
string dummy;
is >> dummy; // this should always equal "chain";
is >> ch.score;
is >> ch.tName;
is >> ch.tSize;
is >> ch.tStrand;
is >> ch.tStart;
is >> ch.tEnd;
is >> ch.qName;
is >> ch.qSize;
is >> ch.qStrand;
is >> ch.qStart;
is >> ch.qEnd;
is >> ch.id;
char should_end_line = is.get(); // warnings... I know...
return is;
};
ostream &
operator<<(ostream &os, const chain &c) {
const string ref_chrom(c.head.tName);
const string query_chrom(c.head.qName);
size_t ref_position = c.head.tStart;
size_t query_position = c.head.qStart;
if (c.head.qStrand == '+') {
for (size_t i = 0; i < c.alns.size(); ++i) {
os << ref_chrom << '\t'
<< ref_position << '\t'
<< ref_position + c.alns[i].size << '\t';
os << query_chrom << '\t'
<< query_position << '\t'
<< query_position + c.alns[i].size << '\t'
<< '+' << endl;
ref_position += (c.alns[i].size + c.alns[i].dt);
query_position += (c.alns[i].size + c.alns[i].dq);
}
}
else {
const size_t query_chrom_size = c.head.qSize;
for (size_t i = 0; i < c.alns.size(); ++i) {
os << ref_chrom << '\t'
<< ref_position << '\t'
<< ref_position + c.alns[i].size << '\t';
os << query_chrom << '\t'
<< (query_chrom_size - (query_position + c.alns[i].size)) << '\t'
<< (query_chrom_size - query_position) << '\t'
<< '-' << endl;
ref_position += (c.alns[i].size + c.alns[i].dt);
query_position += (c.alns[i].size + c.alns[i].dq);
}
}
return os;
}
istream &
operator>>(istream &in, chain &c) {
if (in >> c.head) {
string line;
aln_dat ad_in;
vector<aln_dat> tmp_alns;
while (getline(in, line) && !line.empty()) {
std::istringstream iss(line);
if (iss >> ad_in)
tmp_alns.push_back(ad_in);
}
c.alns.swap(tmp_alns);
}
return in;
};
void
parse_species_from_chain_file_name(const string &chain_file,
string &spec1, string &spec2) {
const size_t first_dot = chain_file.find('.');
spec1 = chain_file.substr(0, first_dot);
const size_t second_dot = chain_file.find('.', first_dot + 1);
spec2 = chain_file.substr(first_dot + 1, second_dot - first_dot - 1);
}
std::ostream &
operator<<(std::ostream &os, const chain_block &b) {
os << b.ref << '\t' << b.query << '\t' << b.query_strand;
return os;
}
std::istream &
operator>>(std::istream &in, chain_block &b) {
string r_chrom;
size_t r_start = 0, r_end = 0;
string q_chrom;
size_t q_start = 0, q_end = 0;
char q_strand;
if (in
>> r_chrom >> r_start >> r_end
>> q_chrom >> q_start >> q_end >> q_strand) {
b.ref = SimpleGenomicRegion(r_chrom, r_start, r_end);
b.query = SimpleGenomicRegion(q_chrom, q_start, q_end);
b.query_strand = q_strand;
}
return in;
}