-
Notifications
You must be signed in to change notification settings - Fork 8
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Closes #26
- Loading branch information
Showing
7 changed files
with
252 additions
and
27 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,97 @@ | ||
#include "RMGroupTestReader.h" | ||
|
||
using namespace std; | ||
|
||
uint64_t RMGroupTestReader::get_num_records() { | ||
return records.size(); | ||
} | ||
|
||
void RMGroupTestReader::load(const string &file) { | ||
ifstream input_file(file); | ||
string line; | ||
auto line_separator = regex("[ \t]"); | ||
|
||
// Line regexes | ||
auto regex_samples = regex("##TotalSampleSize=(\\d+)"); | ||
auto regex_studies = regex("##STUDY_NUM=(\\d+)"); | ||
auto regex_method = regex("##Method=(\\w+)"); | ||
auto regex_header = regex("#GROUPNAME\tNUM_VAR.*"); | ||
|
||
bool header_done = false; | ||
vector<string> header; | ||
while (getline(input_file, line)) { | ||
smatch match; | ||
if (!header_done) { | ||
if (regex_search(line, match, regex_samples) && match.size() > 1) { | ||
this->nsamples = stoul(match.str(1)); | ||
} | ||
else if (regex_search(line, match, regex_studies) && match.size() > 1) { | ||
this->nstudies = stoul(match.str(1)); | ||
} | ||
else if (regex_search(line, match, regex_method) && match.size() > 1) { | ||
this->group_test = match.str(1); | ||
} | ||
else if (regex_search(line, match, regex_header)) { | ||
header_done = true; | ||
copy(sregex_token_iterator(line.begin(), line.end(), line_separator, -1), sregex_token_iterator(), back_inserter(header)); | ||
} | ||
} | ||
else { | ||
// Begin parsing record row | ||
vector<string> tokens; | ||
copy(sregex_token_iterator(line.begin(), line.end(), line_separator, -1), sregex_token_iterator(), back_inserter(tokens)); | ||
|
||
// For some reason RAREMETAL puts a genomic control line all the way at the end of the file... | ||
if (line.substr(0,1) == "#") { continue; } | ||
|
||
// Create record | ||
auto rec = make_shared<RMGroupTestRecord>(); | ||
for (int i = 0; i < tokens.size(); i++) { | ||
string col(header[i]); | ||
if (col == "#GROUPNAME") { | ||
rec->group = tokens.at(i); | ||
} | ||
else if (col == "NUM_VAR") { | ||
rec->num_var = stoul(tokens.at(i)); | ||
} | ||
else if (col == "VARs") { | ||
auto semi = regex(";"); | ||
copy(sregex_token_iterator(tokens.at(i).begin(), tokens.at(i).end(), semi, -1), sregex_token_iterator(), back_inserter(rec->variants)); | ||
} | ||
else if (col == "PVALUE_LIU") { | ||
rec->pvalue_liu = extract_fp<double>(tokens.at(i)); | ||
} | ||
else if (col == "PVALUE_DAVIES") { | ||
rec->pvalue_davies = extract_fp<double>(tokens.at(i)); | ||
} | ||
} | ||
|
||
// Insert | ||
records.push_back(rec); | ||
this->index.emplace(rec->group, rec); | ||
} | ||
} | ||
} | ||
|
||
RMGroupTestReader::RMGroupTestReader(const string &file) { | ||
load(file); | ||
} | ||
|
||
double RMGroupTestReader::get_nsamples() { | ||
return nsamples; | ||
} | ||
|
||
uint64_t RMGroupTestReader::get_nstudies() { | ||
return nstudies; | ||
} | ||
|
||
shared_ptr<RMGroupTestRecord> RMGroupTestReader::get_record(const string &i) { | ||
auto it = this->index.find(i); | ||
if (it != this->index.end()) { | ||
return it->second; | ||
} | ||
else { | ||
auto null = shared_ptr<RMGroupTestRecord>(); | ||
return null; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
#ifndef RAREMETAL_RMGROUPTESTREADER_H | ||
#define RAREMETAL_RMGROUPTESTREADER_H | ||
|
||
#include <string> | ||
#include <map> | ||
#include <vector> | ||
#include <memory> | ||
#include <fstream> | ||
#include <string> | ||
#include <iostream> | ||
#include <regex> | ||
#include <exception> | ||
#include <limits> | ||
#include "reader_util.h" | ||
|
||
struct RMGroupTestRecord { | ||
std::string group; | ||
uint64_t num_var; | ||
std::vector<std::string> variants; | ||
double avg_af; | ||
double min_af; | ||
double max_af; | ||
double stat; | ||
double pvalue_davies; | ||
double pvalue_liu; | ||
}; | ||
|
||
class RMGroupTestReader { | ||
protected: | ||
uint64_t nsamples; | ||
uint64_t nstudies; | ||
std::string group_test; | ||
std::string trait_name; | ||
std::vector<std::shared_ptr<RMGroupTestRecord>> records; | ||
std::map<std::string, std::shared_ptr<RMGroupTestRecord>> index; | ||
public: | ||
RMGroupTestReader(const std::string &file); | ||
void load(const std::string &file); | ||
double get_nsamples(); | ||
uint64_t get_nstudies(); | ||
std::string get_group_test(); | ||
uint64_t get_num_records(); | ||
std::shared_ptr<RMGroupTestRecord> get_record(const std::string &i); | ||
}; | ||
|
||
#endif //RAREMETAL_RMGROUPTESTREADER_H |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
#ifndef RAREMETAL_READER_UTIL_H | ||
#define RAREMETAL_READER_UTIL_H | ||
|
||
#include <string> | ||
#include <limits> | ||
#include <exception> | ||
|
||
template <typename T> T extract_fp(const std::string &s) { | ||
T d; | ||
try { | ||
if (std::is_same<T, long double>::value) { | ||
d = stold(s); | ||
} | ||
else if (std::is_same<T, double>::value) { | ||
d = stod(s); | ||
} | ||
else if (std::is_same<T, float>::value) { | ||
d = stof(s); | ||
} | ||
else { | ||
throw std::invalid_argument("Invalid return type when extracting floating point type number from string"); | ||
} | ||
} | ||
catch (const std::exception &e) { | ||
d = std::numeric_limits<T>::quiet_NaN(); | ||
} | ||
return d; | ||
} | ||
#endif //RAREMETAL_READER_UTIL_H |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters