Skip to content

Commit

Permalink
support JSON report
Browse files Browse the repository at this point in the history
  • Loading branch information
sfchen committed Jun 14, 2018
1 parent 9c3ed13 commit 402f0c6
Show file tree
Hide file tree
Showing 13 changed files with 160 additions and 12 deletions.
2 changes: 1 addition & 1 deletion src/common.h
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
#ifndef COMMON_H
#define COMMON_H

#define FUSIONSCAN_VER "0.4.0"
#define FUSIONSCAN_VER "0.5.0"

#define _DEBUG true

Expand Down
7 changes: 4 additions & 3 deletions src/fusionscan.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -6,23 +6,24 @@
#include "pescanner.h"
#include "util.h"

FusionScan::FusionScan(string fusionFile, string refFile, string read1File, string read2File, string html, int threadNum){
FusionScan::FusionScan(string fusionFile, string refFile, string read1File, string read2File, string html, string json, int threadNum){
mRead1File = read1File;
mRead2File = read2File;
mFusionFile = fusionFile;
mRefFile = refFile;
mHtmlFile = html;
mJsonFile = json;
mThreadNum = threadNum;
}

bool FusionScan::scan(){
vector<Fusion> fusions = Fusion::parseCsv(mFusionFile);
if(mRead2File != ""){
PairEndScanner pescanner( mFusionFile, mRefFile, mRead1File, mRead2File, mHtmlFile, mThreadNum);
PairEndScanner pescanner( mFusionFile, mRefFile, mRead1File, mRead2File, mHtmlFile, mJsonFile, mThreadNum);
return pescanner.scan();
}
else{
SingleEndScanner sescanner( mFusionFile, mRefFile, mRead1File, mHtmlFile, mThreadNum);
SingleEndScanner sescanner( mFusionFile, mRefFile, mRead1File, mHtmlFile, mJsonFile, mThreadNum);
return sescanner.scan();
}
}
3 changes: 2 additions & 1 deletion src/fusionscan.h
Original file line number Diff line number Diff line change
Expand Up @@ -12,14 +12,15 @@ using namespace std;

class FusionScan{
public:
FusionScan(string fusionFile, string refFile, string read1File, string read2File, string html, int threadNum);
FusionScan(string fusionFile, string refFile, string read1File, string read2File, string html, string json, int threadNum);
bool scan();

private:
string mFusionFile;
string mRead1File;
string mRead2File;
string mHtmlFile;
string mJsonFile;
string mRefFile;
int mThreadNum;
};
Expand Down
82 changes: 82 additions & 0 deletions src/jsonreporter.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,82 @@
#include "jsonreporter.h"
#include "common.h"
#include <chrono>
#include "globalsettings.h"

JsonReporter::JsonReporter(string filename, FusionMapper* mapper){
mFusionMapper = mapper;
mFusionResults = mapper->mFusionResults;
mFilename = filename;
mFile.open(mFilename.c_str(), ifstream::out);
}

JsonReporter::~JsonReporter(){
mFile.close();
}

extern string getCurrentSystemTime();
extern string command;

void JsonReporter::run() {
mFile << "{" << endl;
mFile << "\t\"command\":\"" << command << "\"," << endl;
mFile << "\t\"version\":\"" << FUSIONSCAN_VER << "\"," << endl;
mFile << "\t\"time\":\"" << getCurrentSystemTime() << "\"," << endl;
mFile << "\t\"mutations\":{";

bool isFirstMut = true;
for(int i=0;i<mFusionResults.size();i++){
FusionResult fusion = mFusionResults[i];
vector<Match*> matches = fusion.mMatches;

if(isFirstMut) {
mFile << endl;
isFirstMut = false;
}
else
mFile << "," << endl;

mFile << "\t\t\"" << fusion.mTitle << "\":{" << endl;
mFile << "\t\t\t\"" << "left" << "\":{" << endl;
mFile << "\t\t\t\t\"" << "gene_name" << "\":" << "\"" << fusion.mLeftGene.mName << "\"," << endl;
mFile << "\t\t\t\t\"" << "gene_chr" << "\":" << "\"" << fusion.mLeftGene.mChr << "\"," << endl;
mFile << "\t\t\t\t\"" << "position" << "\":" << fusion.mLeftGP.position << "," << endl;
mFile << "\t\t\t\t\"" << "reference" << "\":" << "\"" << fusion.mLeftRef << "\"," << endl;
mFile << "\t\t\t\t\"" << "ref_ext" << "\":" << "\"" << fusion.mLeftRefExt << "\"," << endl;
mFile << "\t\t\t\t\"" << "pos_str" << "\":" << "\"" << fusion.mLeftPos << "\"," << endl;
mFile << "\t\t\t\t\"" << "exon_or_intron" << "\":" << "\"" << (fusion.mLeftIsExon?"exon":"intron") << "\"," << endl;
mFile << "\t\t\t\t\"" << "exon_or_intron_id" << "\":" << fusion.mLeftExonOrIntronID << "," << endl;
mFile << "\t\t\t\t\"" << "strand" << "\":" << "\"" << (fusion.isLeftProteinForward()?"forward":"reversed") << "\"" << endl;
mFile << "\t\t\t}, " << endl;
mFile << "\t\t\t\"" << "right" << "\":{" << endl;
mFile << "\t\t\t\t\"" << "gene_name" << "\":" << "\"" << fusion.mRightGene.mName << "\"," << endl;
mFile << "\t\t\t\t\"" << "gene_chr" << "\":" << "\"" << fusion.mRightGene.mChr << "\"," << endl;
mFile << "\t\t\t\t\"" << "position" << "\":" << fusion.mRightGP.position << "," << endl;
mFile << "\t\t\t\t\"" << "reference" << "\":" << "\"" << fusion.mRightRef << "\"," << endl;
mFile << "\t\t\t\t\"" << "ref_ext" << "\":" << "\"" << fusion.mRightRefExt << "\"," << endl;
mFile << "\t\t\t\t\"" << "pos_str" << "\":" << "\"" << fusion.mRightPos << "\"," << endl;
mFile << "\t\t\t\t\"" << "exon_or_intron" << "\":" << "\"" << (fusion.mRightIsExon?"exon":"intron") << "\"," << endl;
mFile << "\t\t\t\t\"" << "exon_or_intron_id" << "\":" << fusion.mRightExonOrIntronID << "," << endl;
mFile << "\t\t\t\t\"" << "strand" << "\":" << "\"" << (fusion.isRightProteinForward()?"forward":"reversed") << "\"" << endl;
mFile << "\t\t\t}, " << endl;

mFile << "\t\t\t\"" << "unique" << "\":" << fusion.mUnique << "," << endl;
mFile << "\t\t\t\"" << "reads" << "\":[" << endl;
for(int m=0; m<matches.size(); m++){
mFile << "\t\t\t\t{" << endl;
mFile << "\t\t\t\t\t\"break\":" << matches[m]->mReadBreak << "," << endl;
mFile << "\t\t\t\t\t\"" << "strand" << "\":" << "\"" << (matches[m]->mReversed?"reversed":"forward") << "\"," << endl;
matches[m]->printReadToJson(mFile, "\t\t\t\t\t");
mFile << "\t\t\t\t}";
if(m!=matches.size()-1)
mFile << ",";
mFile << endl;
}
mFile << "\t\t\t]" << endl;
mFile << "\t\t}";
}

mFile << endl;
mFile << "\t}" << endl;
mFile << "}" << endl;
}
29 changes: 29 additions & 0 deletions src/jsonreporter.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
#ifndef JSON_REPORTER_H
#define JSON_REPORTER_H

#include <stdio.h>
#include <stdlib.h>
#include <string>
#include "read.h"
#include "fusion.h"
#include "match.h"
#include <iostream>
#include <fstream>
#include "fusionmapper.h"

using namespace std;

class JsonReporter{
public:
JsonReporter(string filename, FusionMapper* mapper);
~JsonReporter();
void run();

private:
string mFilename;
FusionMapper* mFusionMapper;
ofstream mFile;
vector<FusionResult> mFusionResults;
};

#endif
6 changes: 4 additions & 2 deletions src/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -23,13 +23,15 @@ int main(int argc, char* argv[]){
cmd.add<string>("ref", 'r', "reference fasta file name", true, "");
cmd.add<int>("unique", 'u', "specify the least supporting read number is required to report a fusion, default is 2", false, 2);
cmd.add<int>("deletion", 'd', "specify the least deletion length of a intra-gene deletion to report, default is 50", false, 50);
cmd.add<string>("html", 'h', "file name to store html report, default is genefuse.html", false, "genefuse.html");
cmd.add<string>("html", 'h', "file name to store HTML report, default is genefuse.html", false, "genefuse.html");
cmd.add<string>("json", 'j', "file name to store JSON report, default is genefuse.json", false, "genefuse.json");
cmd.add<int>("thread", 't', "worker thread number, default is 4", false, 4);
cmd.parse_check(argc, argv);
string r1file = cmd.get<string>("read1");
string r2file = cmd.get<string>("read2");
string fusionFile = cmd.get<string>("fusion");
string html = cmd.get<string>("html");
string json = cmd.get<string>("json");
string refFile = cmd.get<string>("ref");
int threadNum = cmd.get<int>("thread");
int unique = cmd.get<int>("unique");
Expand Down Expand Up @@ -61,7 +63,7 @@ int main(int argc, char* argv[]){

time_t t1 = time(NULL);

FusionScan fs(fusionFile, refFile, r1file, r2file, html, threadNum);
FusionScan fs(fusionFile, refFile, r1file, r2file, html, json, threadNum);
fs.scan();

time_t t2 = time(NULL);
Expand Down
5 changes: 5 additions & 0 deletions src/match.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -87,3 +87,8 @@ int Match::countUnique(vector<Match*>& matches) {
}
return count;
}

void Match::printReadToJson(ofstream& file, string pad) {
file << pad << "\"seq\":" << "\"" << mRead->mSeq.mStr << "\"," << endl;
file << pad << "\"qual\":" << "\"" << mRead->mQuality << "\"" << endl;
}
1 change: 1 addition & 0 deletions src/match.h
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ class Match{
Match(Read* r, int readBreak, GenePos leftGP, GenePos rightGP, int gap, bool reversed = false);
~Match();
void print();
void printReadToJson(ofstream& file, string pad);
void printHtmlTD(ofstream& file);
void printReadsToFile(ofstream& file);
void setReversed(bool flag);
Expand Down
13 changes: 12 additions & 1 deletion src/pescanner.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,15 @@
#include <thread>
#include <memory.h>
#include "util.h"
#include "jsonreporter.h"

PairEndScanner::PairEndScanner(string fusionFile, string refFile, string read1File, string read2File, string html, int threadNum){
PairEndScanner::PairEndScanner(string fusionFile, string refFile, string read1File, string read2File, string html, string json, int threadNum){
mRead1File = read1File;
mRead2File = read2File;
mFusionFile = fusionFile;
mRefFile = refFile;
mHtmlFile = html;
mJsonFile = json;
mProduceFinished = false;
mThreadNum = threadNum;
mFusionMapper = NULL;
Expand Down Expand Up @@ -53,6 +55,7 @@ bool PairEndScanner::scan(){
mFusionMapper->clusterMatches();

htmlReport();
jsonReport();

mFusionMapper->freeMatches();
return true;
Expand Down Expand Up @@ -266,3 +269,11 @@ void PairEndScanner::htmlReport() {
HtmlReporter reporter(mHtmlFile, mFusionMapper);
reporter.run();
}

void PairEndScanner::jsonReport() {
if(mJsonFile == "")
return;

JsonReporter reporter(mJsonFile, mFusionMapper);
reporter.run();
}
4 changes: 3 additions & 1 deletion src/pescanner.h
Original file line number Diff line number Diff line change
Expand Up @@ -38,11 +38,12 @@ typedef struct ReadPairRepository ReadPairRepository;

class PairEndScanner{
public:
PairEndScanner(string fusionFile, string refFile, string read1File, string read2File, string html="", int threadnum=1);
PairEndScanner(string fusionFile, string refFile, string read1File, string read2File, string html, string json, int threadnum);
~PairEndScanner();
bool scan();
void textReport();
void htmlReport();
void jsonReport();

private:
bool scanPairEnd(ReadPairPack* pack);
Expand All @@ -60,6 +61,7 @@ class PairEndScanner{
string mRead1File;
string mRead2File;
string mHtmlFile;
string mJsonFile;
ReadPairRepository mRepo;
bool mProduceFinished;
std::mutex mFusionMtx;
Expand Down
2 changes: 2 additions & 0 deletions src/read.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -229,6 +229,8 @@ Read* ReadPair::fastMerge(){
} else {
// add the quality of the pair to make a high qual
mergedQual[offset+i] = qual1[offset+i] + qual2[i] - 33;
if(mergedQual[offset+i] >= 'Z')
mergedQual[offset+i] = 'Z';
}
}
delete rcRight;
Expand Down
14 changes: 12 additions & 2 deletions src/sescanner.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,14 @@
#include <thread>
#include <memory.h>
#include "util.h"
#include "jsonreporter.h"

SingleEndScanner::SingleEndScanner(string fusionFile, string refFile, string read1File, string html, int threadNum){
SingleEndScanner::SingleEndScanner(string fusionFile, string refFile, string read1File, string html, string json, int threadNum){
mRead1File = read1File;
mFusionFile = fusionFile;
mRefFile = refFile;
mHtmlFile = html;
mJsonFile = json;
mProduceFinished = false;
mThreadNum = threadNum;
mFusionMapper = NULL;
Expand Down Expand Up @@ -51,8 +53,8 @@ bool SingleEndScanner::scan(){
mFusionMapper->sortMatches();
mFusionMapper->clusterMatches();

textReport();
htmlReport();
jsonReport();

mFusionMapper->freeMatches();

Expand Down Expand Up @@ -224,3 +226,11 @@ void SingleEndScanner::htmlReport() {
HtmlReporter reporter(mHtmlFile, mFusionMapper);
reporter.run();
}

void SingleEndScanner::jsonReport() {
if(mJsonFile == "")
return;

JsonReporter reporter(mJsonFile, mFusionMapper);
reporter.run();
}
4 changes: 3 additions & 1 deletion src/sescanner.h
Original file line number Diff line number Diff line change
Expand Up @@ -38,11 +38,12 @@ typedef struct ReadRepository ReadRepository;

class SingleEndScanner{
public:
SingleEndScanner(string fusionFile, string refFile, string read1File, string html="", int threadnum=1);
SingleEndScanner(string fusionFile, string refFile, string read1File, string html, string json, int threadnum);
~SingleEndScanner();
bool scan();
void textReport();
void htmlReport();
void jsonReport();

private:
bool scanSingleEnd(ReadPack* pack);
Expand All @@ -60,6 +61,7 @@ class SingleEndScanner{
string mRead1File;
string mRead2File;
string mHtmlFile;
string mJsonFile;
ReadRepository mRepo;
bool mProduceFinished;
std::mutex mFusionMtx;
Expand Down

0 comments on commit 402f0c6

Please sign in to comment.