Sidebar

Add in an OBX segment, renumbering the remaining.

0 votes
595 views
asked Jan 7, 2021 by david-m-6327 (240 points)
So I've seen how to renumber all the OBX segments, and have that working. However, is there a function to add an OBX segment that renumbers the remainging?

For example, I had asked this question:

https://www.qvera.com/kb/index.php/2578/trying-to-add-a-character-in-each-obx-segment?state=comment-2579&show=2579#c2579

Due to vendor stupidity, even though this does EXACTLY what they asked for, it's not working correctly. Their solution is to break the obx segment into the subsequent line.

So if I have the following:

OBX|1||MRI|45|IMPRESSION:  
OBX|1||MRI|46|1. Atrophy with partial tear of the subscapularis tendon.   
OBX|1||MRI|47|2. Small tear involving the footprint of the supraspinatus tendon with no retraction.  AC hypertrophic changes as noted.
OBX|1||MRI|48|3. Abnormal fluid again is seen juxtaposed to the glenohumeral ligament with what is thought to represent a possible Buford complex.  
OBX|1||MRI|49|4. Given the myriad of findings and the significant atrophy involving the supraspinatus tendon, orthopedic consultation is needed.  
OBX|1||MRI|50|5. Injury to the coracohumeral ligament with tendinopathy and cannot exclude a very small intrasubstance injury to the biceps as seen.  This is not consistent with a bifid bicep tendon, but rather intrasubstance injury.  
OBX|1||MRI|51|

And I have to reduce the size of each OBX-5, but to move it to the next segment would requiring renumbering the rest.

Do I have to do a cascading renumbering (ie when I split the OBX segment, I renumber all after, then the next time renumber all after etc) or is there a function that handles this?
commented Jan 7, 2021 by david-m-6327 (240 points)
I think I may have been overcomplicating the thought. If I keep this original solution, I can then run a second function to break up the OBX segments and renumber all of them. It would be nice if there's a function to do it, so at the end where we have the "message.createAndSetNode['OBX['+ (inst+1) + ']-5', value);" it could just be added, but I think this will work. I'll post what I come up with shortly.
commented Jan 7, 2021 by david-m-6327 (240 points)
So here's what I've come up with thus far:

// Get the total number of the OBX fields right now. This appears to create
// an array of each of the OBX-5 values.
var instances = message.getAllNodes('OBX-5');
// Get the total number of OBX-3 fields as well. They should all be the same
// but this seems to be a useful way to then use the similar syntax as above.
var obx3 = message.getAllNodes('OBX-3');
// This is the counter for the number of OBX segments, so we'd start at 1.
var obxCount = 1;

// This for loop cycles through each OBX segment, starting at the first
// and moving to the very end.
for (var inst = 0 ; inst < instances.length ; inst++) {
   
   // This is taking the current line (current OBX line) and splitting it
   // based of whether it has a ~ or not.
   var lines = instances[inst].split("~");
   
   // Initialize a variable that will be used for the line
   var value = '';

   // Now that we have the specific line in other variable, this would typically be where we'd
   // delete the current OBX line.
   // ** HOW DO WE DELETE THE CURRENT OBX LINE?? **
   
   // If the lines array is only one line long, we should be able to just write
   // it out to the file. If it's longer than that, then we need to make multiple
   // OBX segments out of it.
   if (lines[i].length() > 1)    {    // This assumes this language evaluates one entry as value 1

        // This is where we write out to the new OBX line

   } else    {    // Else the array has more than one entry, and requires iteration and multiple writes
       for (var i=0 ; i < lines.length ; i++) {
           var line = lines[i];    // Get this specific line for writing out.
           
           // This is where we write out to the new OBX line
       }
   }

}

Where I'm stuck is how to write out the new OBX lines, as I'd need to delete the current one being evaluated prior to writing out the new ones, but I think that may cause an issue since (for example) I have a single line now, that writes out to 3-4 lines, that would then overwrite what's in those OBX lines.
So possibly doing this as a reverse for loop? And then start the counter much higher than the total number of existing OBX segments? For example, if the total in the message is 10 OBX lines, setting a counter to 20, then do a reverse for loop and write out the the first line at 20, next 19 and so forth.

1 Answer

0 votes

Here is an example that cycles all of the segments in the message, OBX or not, if it is not an OBX segment, it just adds it to the result, if it is an OBX segment it checks the length of OBX 5 against the maxLength variable.  If it is longer than the max length, it breaks the segment down, and adds multiple for each part, if it is shorter than the max length, it will just add it.

Once you are done with the initial loop, we can then cycle all of the OBX segments and re-sequence the numbers.

var maxLength = 90;

var allSegments = source.getAllNodes('/');
var result = "";

for (var i = 0; i < allSegments.length; i++) {
   var segment = allSegments[i];
   if (StringUtils.startsWithIgnoreCase(segment, 'OBX')) {
      var obxSegment = qie.parseHL7String(segment);
      if (StringUtils.length(obxSegment.getNode('OBX-5')) > maxLength) {
         // break this OBX segment down to multiple segments
         var obx5 = obxSegment.getNode('OBX-5');
         while (StringUtils.length(obx5) > maxLength) {
            var firstHalf = StringUtils.substringBeforeLast(StringUtils.substring(obx5, 0, maxLength), ' ');
            obx5 = StringUtils.substring(obx5, firstHalf.length());
            
            // now add just this portion to the result
            obxSegment.setNode('OBX-5', firstHalf);
            result += obxSegment.toString() + "\n";
         }
         // now add the remaining to the result
         obxSegment.setNode('OBX-5', obx5);
         result += obxSegment.toString() + "\n";
      } else {
         result += obxSegment.toString() + "\n";
      }
   } else {
      result += segment + "\n";
   }
}

message.setNode('/', result);

// now we re-sequence the OBX segments
var segCount = message.getCount('OBX');
for (var j = 1; j <= segCount; j++) {
   message.setNode('OBX-1', j + '', j);
   message.setNode('OBX-4', j + '', j);
}

answered Jan 7, 2021 by ben-s-7515 (12,640 points)
...