Created
November 16, 2023 22:32
-
-
Save rlang81/7e0ea36cf1d5ec780b1c2b94702f0383 to your computer and use it in GitHub Desktop.
Function to rewrite index files generated by PE Harmony V6 to be compatible with https://github.com/broadinstitute/pe2loaddata
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import os | |
import xml.etree.ElementTree as ET | |
def rewrite_harmonyV6_index_file(index_path): | |
"""Fix Harmony V6 index file to be compatible with pe2loaddata | |
Warning: This will rewrite the index file in place! | |
Dependencies: os, xml.etree.ElementTree as ET | |
Parameters | |
---------- | |
index_path: str | |
Path to Harmony V6 index file | |
Returns | |
------- | |
""" | |
namespaces = dict([node for _, node in ET.iterparse(index_path, events=['start-ns'])]) | |
assert "HarmonyV6" in namespaces[''], "Index file does not have a HarmonyV6 namespace!" | |
for key, value in namespaces.items(): | |
ET.register_namespace(key, value) | |
tree = ET.parse(index_path) | |
xmlroot = tree.getroot() | |
maps = xmlroot.find("Maps", namespaces=namespaces)[-1] # Last map seems to have Channel information | |
images = xmlroot.find("Images", namespaces=namespaces) | |
for image in images: | |
if len(image.findall("ChannelName", namespaces=namespaces)) == 0: # Check that no channel name exists | |
for child in maps: # Find map with matching ChannelID | |
if (image.find("ChannelID", namespaces=namespaces).text == child.get('ChannelID')): # Stop if the Channel ID in the image matches the map entry | |
for sub in child: # Loop through every entry in the map and copy it to the image | |
temp = ET.SubElement(image, sub.tag, attrib=sub.attrib) | |
temp.text = sub.text | |
break # No need to continue looping through the map entries | |
# Save the updated xml | |
ET.indent(tree, ' ') | |
tree.write(index_path, encoding="utf-8", xml_declaration=False) | |
if __name__ == "__main__": | |
rewrite_harmonyV6_index_file(index_path="path/to/Index.idx.xml") |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment