polish: sortdiamond modularity

This commit is contained in:
kuoi 2024-07-05 11:35:53 +10:00
parent 4c0b989047
commit 680c5392e6

View file

@ -64,6 +64,70 @@ string revcomp(const string &seq) {
return revseq; return revseq;
} }
void readInputFile(const string &filename,
map<string, pair<double, string>> &max_map) {
ifstream infile(filename);
if (!infile) {
cerr << "Error opening input file: " << filename << endl;
return;
}
string line;
while (getline(infile, line)) {
istringstream iss(line);
string fields[20];
int i = 0;
while (iss >> fields[i] && i < 20) {
i++;
}
string key = fields[1];
double value = stod(fields[11]);
// Check if the key already exists in the map
if (max_map.find(key) != max_map.end()) {
// If the new value is greater, update the map
if (value > max_map[key].first) {
max_map[key] = make_pair(value, line);
}
} else {
// If the key does not exist, insert the new key-value
// pair
max_map[key] = make_pair(value, line);
}
}
infile.close();
}
void processMap(const map<string, pair<double, string>> &max_map,
vector<pair<string, string>> &result) {
for (const auto &entry : max_map) {
istringstream iss(entry.second.second);
string fields[20];
int i = 0;
while (iss >> fields[i] && i < 20) {
i++;
}
if (stoi(fields[6]) > stoi(fields[7])) {
fields[17] = revcomp(fields[17]);
}
result.push_back(make_pair(">" + fields[1], fields[17]));
}
sort(result.begin(), result.end());
}
void writeOutputFile(const string &filename,
const vector<pair<string, string>> &result) {
ofstream outfile(filename);
if (!outfile) {
cerr << "Error opening output file: " << filename << endl;
return;
}
for (const auto &entry : result) {
outfile << entry.first << "\n" << entry.second << "\n";
}
outfile.close();
}
int main(int argc, char *argv[]) { int main(int argc, char *argv[]) {
if (argc != 3) { if (argc != 3) {
cerr << "Usage: " << argv[0] << " <input_file> <output_file>" cerr << "Usage: " << argv[0] << " <input_file> <output_file>"
@ -74,69 +138,13 @@ int main(int argc, char *argv[]) {
string in_name = argv[1]; string in_name = argv[1];
string ot_name = argv[2]; string ot_name = argv[2];
ifstream infile(in_name); map<string, pair<double, string>> max_map;
if (!infile) { readInputFile(in_name, max_map);
cerr << "Error opening input file: " << in_name << endl;
return 1;
}
ofstream outfile(ot_name);
if (!outfile) {
cerr << "Error opening output file: " << ot_name << endl;
return 1;
}
map<string, pair<double, string>>
max_map; // Key: sseqid, Value: pair<score, line>
string line;
while (getline(infile, line)) {
istringstream iss(line);
string fields[20]; // Adjust size if needed
int i = 0;
while (iss >> fields[i] && i < 20) {
i++;
}
string key = fields[1];
double value = stod(fields[11]);
if (max_map.find(key) != max_map.end()) {
if (value > max_map[key].first) {
max_map[key] = make_pair(value, line);
}
} else {
max_map[key] = make_pair(value, line);
}
}
vector<pair<string, string>> result; vector<pair<string, string>> result;
processMap(max_map, result);
for (const auto &entry : max_map) { writeOutputFile(ot_name, result);
istringstream iss(entry.second.second);
string fields[20]; // Adjust size if needed
int i = 0;
while (iss >> fields[i] && i < 20) {
i++;
}
if (stoi(fields[6]) > stoi(fields[7])) {
fields[17] = revcomp(fields[17]);
}
result.push_back(make_pair(">" + fields[1], fields[17]));
}
sort(result.begin(), result.end());
for (const auto &entry : result) {
outfile << entry.first << "\n" << entry.second << endl;
}
infile.close();
outfile.close();
return 0; return 0;
} }