Skip to content

Commit

Permalink
fix mem leak
Browse files Browse the repository at this point in the history
  • Loading branch information
tobiasrausch committed Nov 12, 2020
1 parent c0f3900 commit 75a0a3f
Show file tree
Hide file tree
Showing 4 changed files with 29 additions and 26 deletions.
9 changes: 1 addition & 8 deletions src/annotate.h
Original file line number Diff line number Diff line change
Expand Up @@ -69,14 +69,7 @@ namespace sansa
}

// Fix chr names
fixChrNames(c.nchr);

// Debug chr names
int32_t maxRID = 0;
for(AnnotateConfig::TChrMap::const_iterator itcm = c.nchr.begin(); itcm != c.nchr.end(); ++itcm) {
if ((int32_t) (itcm->second + 1) > maxRID) maxRID = itcm->second + 1;
//std::cerr << itcm->first << '=' << itcm->second << std::endl;
}
int32_t maxRID = fixChrNames(c);

// Optionally parse GFF/GTF/BED file
typedef std::vector<IntervalLabel> TChromosomeRegions;
Expand Down
6 changes: 2 additions & 4 deletions src/parsedb.h
Original file line number Diff line number Diff line change
Expand Up @@ -129,6 +129,7 @@ namespace sansa
}
}
}
bcf_destroy(rec);

// Sort SVs
sort(svs.begin(), svs.end(), SortSVs<SV>());
Expand All @@ -139,12 +140,9 @@ namespace sansa

// Close output VCF
bcf_hdr_destroy(hdr_out);
hts_close(ofile);

// Close file handles
bcf_hdr_destroy(hdr);
hts_close(ofile);
bcf_close(ifile);
bcf_destroy(rec);

return true;
}
Expand Down
11 changes: 6 additions & 5 deletions src/query.h
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@ namespace sansa

boost::posix_time::ptime now = boost::posix_time::second_clock::local_time();
std::cout << '[' << boost::posix_time::to_simple_string(now) << "] Query input SVs" << std::endl;

// Load bcf file
htsFile* ifile = bcf_open(c.infile.string().c_str(), "r");
if (ifile == NULL) {
Expand Down Expand Up @@ -150,7 +150,7 @@ namespace sansa
if (c.gtfFileFormat != -1) geneAnnotation(c, gRegions, geneIds, refIndex, rec->pos + 1, refIndex2, endsv, featureBp1, featureBp2);
if (featureBp1.empty()) featureBp1 = "NA";
if (featureBp2.empty()) featureBp2 = "NA";

// Any breakpoint hit?
typename TSV::iterator itSV = std::lower_bound(svs.begin(), svs.end(), SV(refIndex, std::max(0, startsv - c.bpwindow), refIndex2, endsv), SortSVs<SV>());
int32_t bestID = -1;
Expand Down Expand Up @@ -202,10 +202,11 @@ namespace sansa
}
dataOut << id << '\t' << bcf_hdr_id2name(hdr, rec->rid) << '\t' << (rec->pos + 1) << '\t' << chr2Name << '\t' << endsv << '\t' << rec->d.id << '\t' << qualval << '\t' << svtval << '\t' << ctval << '\t' << svlength << '\t' << featureBp1 << '\t' << featureBp2 << std::endl;
}

// Successful parse
++parsedSV;
}
bcf_destroy(rec);

// Statistics
now = boost::posix_time::second_clock::local_time();
Expand All @@ -214,10 +215,10 @@ namespace sansa
// Close file handles
dataOut.pop();
dataOut.pop();

bcf_hdr_destroy(hdr);
bcf_close(ifile);
bcf_destroy(rec);


return true;
}

Expand Down
29 changes: 20 additions & 9 deletions src/util.h
Original file line number Diff line number Diff line change
Expand Up @@ -284,15 +284,25 @@ namespace sansa
}

template<typename TChrMap>
inline void
fixChrNames(TChrMap& chrMap) {
// Take care of 1 vs. chr1, X vs chrX, ...
int32_t maxRID = 1;
for(typename TChrMap::iterator itcm = chrMap.begin(); itcm != chrMap.end(); ++itcm) {
if (itcm->second > maxRID) maxRID = itcm->second + 1;
inline int32_t
chrMapSize(TChrMap const& chrMap) {
int32_t maxRID = 0;
for(typename TChrMap::const_iterator itcm = chrMap.begin(); itcm != chrMap.end(); ++itcm) {
if (itcm->second >= maxRID) maxRID = itcm->second + 1;
}
std::vector<std::string> altName(maxRID, "NA");
for(typename TChrMap::iterator itcm = chrMap.begin(); itcm != chrMap.end(); ++itcm) {
return maxRID;
}


template<typename TConfig>
inline int32_t
fixChrNames(TConfig& c) {
typedef typename TConfig::TChrMap TChrMap;

// Take care of 1 vs. chr1, X vs chrX, ...
int32_t maxRID = chrMapSize(c.nchr);
std::vector<std::string> altName(maxRID);
for(typename TChrMap::const_iterator itcm = c.nchr.begin(); itcm != c.nchr.end(); ++itcm) {
std::string cn = itcm->first;
if (cn.size() == 1) {
if (cn == "1") altName[itcm->second] = "chr1";
Expand Down Expand Up @@ -355,8 +365,9 @@ namespace sansa

// Insert alternative names
for(uint32_t i = 0; i < altName.size(); ++i) {
if (altName[i] != "NA") chrMap.insert(std::make_pair(altName[i], i));
if (!altName[i].empty()) c.nchr.insert(std::make_pair(altName[i], i));
}
return maxRID;
}

// Output directory/file checks
Expand Down

0 comments on commit 75a0a3f

Please sign in to comment.