-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathmerge_vc.cpp
More file actions
165 lines (149 loc) · 5.66 KB
/
merge_vc.cpp
File metadata and controls
165 lines (149 loc) · 5.66 KB
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
/**
* Copyright (c) 2015-2018 Hiroyuki Ichida. All rights reserved.
*
* @file merge_vc.cpp
* @author Hiroyuki Ichida <histfd@gmail.com>
*
* This program is free software: you can redistribute it and/or modify it
* under the terms of the GNU General Public License version 2 (GPL-2.0)
* as published by the Free Software Foundation, Inc.
*
* THE SOFTWARE IS PROVIDED “AS IS”, WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
* THE SOFTWARE.
*/
#include "histd.h"
#include "headerline.h"
#include <iostream>
#include <fstream>
#include <string>
#include <cstring>
#include <cstdlib>
#include <iomanip>
#include <map>
#define KEY_PROGRAM "#Program"
#define KEY_LINE "Line"
#define KEY_CHR "CHROM"
#define KEY_START "ChrStart"
#define KEY_END "ChrEnd"
//------------------------------------------------------------------------------
typedef std::map<std::string,std::string> MutationDB;
//------------------------------------------------------------------------------
bool determine_position(hi::StringArray &header_elements, \
const std::string &keyword, int *column_pos){
*column_pos = -1;
ssize_t hit;
for(size_t pos=0; pos<header_elements.size(); pos++){
hit = header_elements.at(pos).find(keyword);
if(std::string::npos != hit){
*column_pos = pos;
return true;
}
}
return false;
}
//------------------------------------------------------------------------------
inline std::string create_locus_id(const std::string &line, \
const std::string &chr, const std::string &start, const std::string &end){
std::stringstream sstr;
sstr << line << '|' << chr << '_' \
<< std::setw(8) << std::setfill('0') << start << '-' \
<< std::setw(8) << std::setfill('0') << end;
return sstr.str();
}
//------------------------------------------------------------------------------
bool read_file(const char *filename, MutationDB &mutationDB, std::string &header_line){
std::ifstream file(filename, std::ios::in);
if(file.fail()){
std::cerr << ERROR_STRING \
<< "an input file (" << filename \
<< ") can't open for reading." << ENDL;
return false;
}
// header
std::cout << "##ORIGINAL_FILE: " << filename << ENDL;
while(std::getline(file, header_line)){
if('#' == header_line[0] && std::string::npos != header_line.find(KEY_CHR))
break;
else if('#' == header_line[0]){
std::cout << header_line << ENDL;
continue;
}
}
hi::StringArray elements;
hi::split(elements, header_line, '\t');
// find column position
int pos_program, pos_line, pos_chr, pos_start, pos_end;
determine_position(elements, KEY_PROGRAM, &pos_program);
determine_position(elements, KEY_LINE, &pos_line);
determine_position(elements, KEY_CHR, &pos_chr);
determine_position(elements, KEY_START, &pos_start);
determine_position(elements, KEY_END, &pos_end);
if(0 > pos_program || 0 > pos_line || 0 > pos_chr || 0 > pos_start || 0 > pos_end){
std::cerr << ERROR_STRING << "1 or more of critical columns can't find." << ENDL;
return false;
}
// process records
std::string line, locus_id, prev_record;
std::stringstream sstr;
size_t tabPos;
MutationDB::iterator iter;
while(std::getline(file, line)){
elements.clear();
hi::split(elements, line, '\t');
locus_id = create_locus_id(elements[pos_line], \
elements[pos_chr], elements[pos_start], elements[pos_end]);
iter = mutationDB.find(locus_id);
if(mutationDB.end() == iter)
mutationDB.insert(std::pair<std::string,std::string>(locus_id, line));
else{
tabPos = iter->second.find('\t');
if(std::string::npos == tabPos)
continue;
sstr.str("");
sstr << iter->second.substr(0, tabPos) \
<< '/' << elements[pos_program] << iter->second.substr(tabPos);
iter->second = sstr.str();
}
}
file.close();
return true;
}
//------------------------------------------------------------------------------
int main(int argc, char** argv) {
// parse arguments
int nArg=1;
if(argc<=nArg){
std::cerr << USAGE_STRING << argv[0] << " file1 file2 file3..." << ENDL;
exit(EXIT_FAILURE);
}
// create input filelist
std::stringstream inputstr;
std::string header_line;
MutationDB mutationDB;
for(size_t i=nArg; i<argc; i++){
if(! read_file(argv[i], mutationDB, header_line)){
std::cerr << WARNING_STRING << "can't open an input file (" \
<< argv[i] << "). Skipped." << ENDL;
continue;
}
if(nArg == i)
inputstr << "input_fn=" << argv[i];
else
inputstr << ";input_fn_" << i-nArg+1 << "=" << argv[i];
}
// header
std::string cmdstr = generate_cmd_string(argc, argv);
write_basic_header(__FILE__, __DATE__, __TIME__, \
cmdstr.c_str(), inputstr.str().c_str(), std::cout);
std::cout << header_line << ENDL;
// output results
for(MutationDB::iterator iter=mutationDB.begin(); iter!=mutationDB.end(); iter++)
std::cout << iter->second << ENDL;
exit(EXIT_SUCCESS);
}
//------------------------------------------------------------------------------