|
| 1 | +#include <gtest/gtest.h> |
| 2 | +#include "ramcore/SamToNTuple.h" |
| 3 | +#include "rntuple/RAMNTupleRecord.h" |
| 4 | +#include "generate_sam_benchmark.h" |
| 5 | +#include <ROOT/RNTupleReader.hxx> |
| 6 | +#include <cstdio> |
| 7 | +#include <filesystem> |
| 8 | + |
| 9 | +class ChromosomeSplitTest : public ::testing::Test { |
| 10 | +protected: |
| 11 | + void SetUp() override |
| 12 | + { |
| 13 | + if (!std::filesystem::exists("test.sam")) { |
| 14 | + GenerateSAMFile("test.sam", 100); |
| 15 | + } |
| 16 | + CleanupTestFiles(); |
| 17 | + } |
| 18 | + |
| 19 | + void TearDown() override { CleanupTestFiles(); } |
| 20 | + |
| 21 | + void CleanupTestFiles() |
| 22 | + { |
| 23 | + std::remove("test_regular.root"); |
| 24 | + for (const auto &entry : std::filesystem::directory_iterator(".")) { |
| 25 | + std::string filename = entry.path().filename().string(); |
| 26 | + if (filename.find("test_split_") == 0 && filename.find(".root") != std::string::npos) { |
| 27 | + std::remove(filename.c_str()); |
| 28 | + } |
| 29 | + } |
| 30 | + } |
| 31 | +}; |
| 32 | + |
| 33 | +TEST_F(ChromosomeSplitTest, NoDataLoss) |
| 34 | +{ |
| 35 | + samtoramntuple("test.sam", "test_regular.root", false, true, true, 505, 1); |
| 36 | + auto regularReader = ROOT::Experimental::RNTupleReader::Open("RAM", "test_regular.root"); |
| 37 | + Long64_t totalEntries = regularReader->GetNEntries(); |
| 38 | + |
| 39 | + samtoramntuple_split_by_chromosome("test.sam", "test_split", 505, 1); |
| 40 | + |
| 41 | + Long64_t splitEntriesSum = 0; |
| 42 | + for (const auto &entry : std::filesystem::directory_iterator(".")) { |
| 43 | + std::string filename = entry.path().filename().string(); |
| 44 | + if (filename.find("test_split_") == 0 && filename.find(".root") != std::string::npos) { |
| 45 | + auto reader = ROOT::Experimental::RNTupleReader::Open("RAM", filename); |
| 46 | + if (reader) { |
| 47 | + splitEntriesSum += reader->GetNEntries(); |
| 48 | + } |
| 49 | + } |
| 50 | + } |
| 51 | + |
| 52 | + EXPECT_EQ(totalEntries, splitEntriesSum); |
| 53 | +} |
| 54 | + |
| 55 | +TEST_F(ChromosomeSplitTest, CorrectChromosomeAssignment) |
| 56 | +{ |
| 57 | + samtoramntuple_split_by_chromosome("test.sam", "test_split", 505, 1); |
| 58 | + |
| 59 | + for (const auto &entry : std::filesystem::directory_iterator(".")) { |
| 60 | + std::string filename = entry.path().filename().string(); |
| 61 | + if (filename.find("test_split_") == 0 && filename.find(".root") != std::string::npos) { |
| 62 | + size_t pos = filename.find("test_split_"); |
| 63 | + size_t end = filename.find(".root"); |
| 64 | + std::string expectedChr = filename.substr(pos + 11, end - pos - 11); |
| 65 | + |
| 66 | + auto reader = ROOT::Experimental::RNTupleReader::Open("RAM", filename); |
| 67 | + ASSERT_NE(reader, nullptr); |
| 68 | + |
| 69 | + auto viewRecord = reader->GetView<RAMNTupleRecord>("record"); |
| 70 | + |
| 71 | + for (auto i : reader->GetEntryRange()) { |
| 72 | + const auto &record = viewRecord(i); |
| 73 | + std::string actualChr = record.GetRNAME(); |
| 74 | + EXPECT_EQ(expectedChr, actualChr) << "Wrong chromosome in " << filename << " at entry " << i; |
| 75 | + } |
| 76 | + } |
| 77 | + } |
| 78 | +} |
| 79 | + |
| 80 | +TEST_F(ChromosomeSplitTest, MetadataPresent) |
| 81 | +{ |
| 82 | + samtoramntuple_split_by_chromosome("test.sam", "test_split", 505, 1); |
| 83 | + |
| 84 | + int filesChecked = 0; |
| 85 | + for (const auto &entry : std::filesystem::directory_iterator(".")) { |
| 86 | + std::string filename = entry.path().filename().string(); |
| 87 | + if (filename.find("test_split_") == 0 && filename.find(".root") != std::string::npos) { |
| 88 | + auto metaReader = ROOT::Experimental::RNTupleReader::Open("METADATA", filename); |
| 89 | + EXPECT_NE(metaReader, nullptr) << "Missing METADATA in " << filename; |
| 90 | + |
| 91 | + if (metaReader) { |
| 92 | + EXPECT_GT(metaReader->GetNEntries(), 0); |
| 93 | + } |
| 94 | + |
| 95 | + filesChecked++; |
| 96 | + } |
| 97 | + } |
| 98 | + |
| 99 | + EXPECT_GT(filesChecked, 0); |
| 100 | +} |
0 commit comments