consistentresultverification.cxx 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119
  1. #include <cstdio>
  2. #include <iostream>
  3. #include <string>
  4. #include <fstream>
  5. #include <sstream>
  6. #include "json.hpp"
  7. int main(int argc, char* argv[]) {
  8. // read 1st JSON file and store in fileOne
  9. std::ifstream file1;
  10. file1.open(argv[1]);
  11. nlohmann::json fileOne;
  12. // Store the contents filename into fileOne
  13. if (file1.is_open()) {
  14. file1 >> fileOne;
  15. }
  16. // read 2nd JSON file and store in fileTwo
  17. std::ifstream file2;
  18. file2.open(argv[2]);
  19. nlohmann::json fileTwo;
  20. // Store the contents filename into fileTwo
  21. if (file2.is_open()) {
  22. file2 >> fileTwo;
  23. //std::cout << "does second file exist?" << std::endl;
  24. }
  25. nlohmann::json outputJSON; // output JSON file
  26. std::string sampleNameOne; // Sample key name of first file
  27. std::string sampleNameTwo; // Sample key name of second file
  28. int firstFileArrayElement = 0; // current element value of the first file
  29. int secondFileArrayElement = 0; // current element value of the second file
  30. int samplesWithConflicts = 0; // number of samples that have conflicting results
  31. // iterate through the first file's samples
  32. for (auto itr = fileOne.begin(); itr != fileOne.end(); ++itr) {
  33. sampleNameOne = itr.key();
  34. //std::cout << "1st file sample name: " << sampleNameOne << std::endl;
  35. if (sampleNameOne != "metadata") {
  36. // iterate through the second file's samples
  37. for (auto itr_1 = fileTwo.begin(); itr_1 != fileTwo.end(); ++itr_1) {
  38. sampleNameTwo = itr_1.key();
  39. //std::cout << "2nd file sample name: " << sampleNameTwo << std::endl;
  40. int elementNumber = 0; // element number in Sample 1 array currently being checked
  41. int conflictingResultsInSamples = 0; // number of conflicts within a sample
  42. // check that the loops are at the same sample
  43. if (sampleNameOne == sampleNameTwo) {
  44. // iterate through the first file's sample array
  45. for (auto arrayItr = fileOne[sampleNameOne].begin(); arrayItr != fileOne[sampleNameOne].end(); ++arrayItr) {
  46. firstFileArrayElement = *arrayItr;
  47. //std::cout << "First File Element: " << firstFileArrayElement << std::endl;
  48. elementNumber++;
  49. int compareElementNumber = 0; // element number in Sample 2 array currently being checked
  50. for (auto arrayItr_1 = fileTwo[sampleNameOne].begin(); arrayItr_1 != fileTwo[sampleNameTwo].end(); ++arrayItr_1) {
  51. secondFileArrayElement = *arrayItr_1;
  52. //std::cout << "Second File Element: " << secondFileArrayElement << std::endl;
  53. compareElementNumber++;
  54. if (elementNumber == compareElementNumber) {
  55. // if there is a conlfict
  56. if (firstFileArrayElement != secondFileArrayElement) {
  57. // begin writing out the ouput for it
  58. nlohmann::json conflict;
  59. // make array of mismatched values
  60. conflict.push_back(firstFileArrayElement);
  61. conflict.push_back(secondFileArrayElement);
  62. // convert key of array into string
  63. std::stringstream convert2String;
  64. convert2String << (elementNumber - 1);
  65. std::string arrayElementNumber = convert2String.str();
  66. // output the mismatch array
  67. outputJSON[itr.key()]["Mismatches"][arrayElementNumber] = conflict;
  68. // increase counter of number of mismatches between the two samples
  69. conflictingResultsInSamples++;
  70. // the first time there is a conflict in a sample
  71. if (conflictingResultsInSamples == 1) {
  72. // increment the counter for how many samples have conflicts
  73. samplesWithConflicts++;
  74. outputJSON[itr.key()]["sample1"] = itr.value();
  75. outputJSON[itr_1.key()]["sample2"] = itr_1.value();
  76. }
  77. break;
  78. }
  79. } else if (elementNumber < compareElementNumber) {
  80. file1.close();break; // once it goes over the element number of the first array, the loop shouldn't keep going
  81. }
  82. }
  83. }
  84. }
  85. }
  86. }
  87. }
  88. outputJSON["metadata"]["samplesWithConflictingResults"] = samplesWithConflicts;
  89. outputJSON["sample1"]["metadata"]["arraySize"] = fileOne["metadata"]["arraySize"];
  90. outputJSON["sample1"]["metadata"]["file"] = argv[1];
  91. outputJSON["sample1"]["metadata"]["numSamples"] = fileOne["metadata"]["numSamples"];
  92. outputJSON["sample2"]["metadata"]["arraySize"] = fileTwo["metadata"]["arraySize"];
  93. outputJSON["sample2"]["metadata"]["file"] = argv[2];
  94. outputJSON["sample2"]["metadata"]["numSamples"] = fileTwo["metadata"]["numSamples"];
  95. // JSON output
  96. std::cout << outputJSON << std::endl;
  97. file1.close();
  98. file2.close();
  99. return 0;
  100. }