sortedverification.cxx 3.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980
  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 JSON file and store in jsonObject
  9. std::ifstream file;
  10. file.open(argv[1]);
  11. nlohmann::json jsonObject;
  12. // Store the contents filename into jsonObject
  13. if (file.is_open()) {
  14. file >> jsonObject;
  15. }
  16. std::string sampleName; // Sample key name to iterate
  17. nlohmann::json outputJSON; // output JSON file
  18. int numberInversions = 0; // total number of samples with inversions
  19. int currValue = 0; // current element value
  20. int prevValue = 0; // previous element value
  21. // iterate through samples
  22. for (auto itr = jsonObject.begin(); itr != jsonObject.end(); ++itr) {
  23. sampleName = itr.key();
  24. int loopCounter = 0; // counter to figure out array's element number
  25. int inversionCounter = 0; // counts the number of consecutive inversions per sample
  26. if (sampleName != "metadata") {
  27. for (auto arrayItr = jsonObject[sampleName].begin(); arrayItr != jsonObject[sampleName].end(); ++arrayItr) {
  28. nlohmann::json inversion; // if there is a consecutive inversion
  29. // on the first iteration of the loop it saves the first element number
  30. if (loopCounter == 0) {
  31. currValue = *arrayItr;
  32. loopCounter++;
  33. } else { // after the first loop iteration, the program has both the current and previous values so it can start comparing them
  34. prevValue = currValue;
  35. currValue = *arrayItr;
  36. loopCounter++;
  37. if (currValue < prevValue) {
  38. // add the two values in the consecutive inversion to a JSON array
  39. inversion.push_back(prevValue);
  40. inversion.push_back(currValue);
  41. // convert loopCounter into a string to use it as a key name in JSON object
  42. std::stringstream convert2String;
  43. convert2String << (loopCounter-2); // loopCounter is two numbers ahead of the actual array element value
  44. // because (a) the loop starts at 1 and the elements at zero
  45. // and (b) the loop is comparing backwards so it is one element ahead of the one that has the consecutive inversion
  46. std::string arrayElementNumber = convert2String.str();
  47. outputJSON[sampleName]["ConsecutiveInversions"][arrayElementNumber] = inversion;
  48. inversionCounter++; // adds one every time a consecutive inversion is detected
  49. // at 1st inversion
  50. if (inversionCounter == 1) {
  51. outputJSON[sampleName]["sample"] = itr.value();
  52. numberInversions++;
  53. }
  54. }
  55. }
  56. }
  57. }
  58. }
  59. // create metadata key values
  60. outputJSON["metadata"]["arraySize"] = jsonObject["metadata"]["arraySize"];
  61. outputJSON["metadata"]["file"] = argv[1];
  62. outputJSON["metadata"]["numSamples"] = jsonObject["metadata"]["numSamples"];
  63. outputJSON["metadata"]["samplesWithInversions"] = numberInversions;
  64. // JSON output
  65. std::cout << outputJSON << std::endl;
  66. file.close();
  67. }