Skip to content

Instantly share code, notes, and snippets.

@pewerner
Created November 4, 2012 11:59
Show Gist options
  • Save pewerner/4011594 to your computer and use it in GitHub Desktop.
Save pewerner/4011594 to your computer and use it in GitHub Desktop.
Windows Script Component for Parsing XML with Perl. Perl Scripts can be invoked Via JavaScript by Invoking ActiveX Controls
<?xml version="1.0" encoding="utf-8"?>
<order>
<attributes>
<attribute name="protocol" value ="PCR_Setup"/>
<attribute name="samples_per_source" value ="0"/>
<attribute name="plates_Sealed" value ="0"/>
<attribute name ="Add_Sample" value ="1"/>
<attribute name="Tip Box" value ="96 V11 LT250 Tip Box 19477.002"/>
</attributes>
<pcr_plates>
<barcode master_mix_volume="40" sample_volume="20">PCR-001</barcode>
<barcode master_mix_volume="20" sample_volume="10">PCR-002</barcode>
<barcode master_mix_volume="30" sample_volume="20">PCR-003</barcode>
<barcode master_mix_volume="30" sample_volume="20">PCR-004</barcode>
<barcode master_mix_volume="30" sample_volume="20">PCR-005</barcode>
</pcr_plates>
<sample_plates Labware ="96 Corning black FL assay" peel="false" seal ="true">
<barcode>SP-A0294</barcode>
<barcode>SP-A0316</barcode>
<barcode>SP-B0916</barcode>
<barcode>SP-C0815</barcode>
<barcode>SP-C0915</barcode>
</sample_plates>
</order>
<?xml version="1.0"?>
<component>
<registration
description="XML Inputfile Parser"
progid="perl.inputparser"
version="1.00"
classid="{74bb1ba9-2e69-4ad6-b02c-c52f3cbe153b}"
>
</registration>
<public>
<method name="OpenInput"/>
<method name="SaveInput"/>
<method name="getPCR_PlateAttributes"/>
<method name="getSamplePlateAttributes"/>
<method name="getPCRbarcodeAttributes"/>
<method name="getSampleBarcodeAttributes"/>
<method name="getOrderAttributes"/>
<method name="getNumberPCRplates"/>
<method name="getNumberSamplePlates"/>
</public>
<script language="PerlScript">
<![CDATA[
#!/usr/bin/perl
# InputParserClass
use XML::Simple;
use File::Copy;
my $xml = new XML::Simple;
my $doc;
my $inputFile;
# Load InputFile into Memmory
sub OpenInput{
$inputFile =$_[0];
$doc= $xml->XMLin($_[0], keyattr =>[]);
}
sub SaveInput{
copy($inputFile, $_[0]);
}
# getOrderAttributes(attribute);
sub getOrderAttributes{
my $node = $doc->{attributes}->{attribute};
#$nodeLength = $#{$node};
my $itemFound = 0;
foreach $item (@{$node}){
if($item->{name} eq $_[0])
{
$itemFound = 1;
return $item->{value};
}
}
if(!$itemFound)
{
return "Error: getOrderAttributes found $itemFound attributes named [$_[0]], expected to find 1";
}
}
# getPCR_PlateAttributes(attribute);
sub getPCR_PlateAttributes{
my $node = $doc->{pcr_plates}->{$_[0]};
if(!$node)
{
return "Error: getPCR_PlateAttributes found 0 attributes named [$_[0]], expected to find 1";
}
else
{
return $node;
}
}
# getPCR_PlateAttributes(attribute);
sub getSamplePlateAttributes{
my $node = $doc->{sample_plates}->{$_[0]};
if(!$node)
{
return "Error: getSamplePlateAttributes found 0 attributes named [$_[0]], expected to find 1";
}
else
{
return $node;
}
}
# getPCRplateAttributes(barcode, attribute);
sub getPCRbarcodeAttributes{
my $node = $doc->{pcr_plates}->{barcode};
my $barcodeFound = 0;
my $attributeFound =0;
foreach $item (@{$node}){
my $bc = $item->{content};
if($bc eq $_[0])
{
$barcodeFound = 1;
my $barcodeAttribute = $item->{$_[1]};
if($barcodeAttribute)
{
return $barcodeAttribute;
}
else
{
return "Error: getPCRplateAttributes found 0 attributes named [$_[1]], expected to find 1";
}
}
}
if(!$barcodeFound)
{
return "Error: getOrderAttributes found $barcodeFound Barcodes named [$_[0]], expected to find 1";
}
}
# getSamplePlateAttributes(barcode, attribute);
sub getSampleBarcodeAttributes{
my $node = $doc->{sample_plates}->{barcode};
my $barcodeFound = 0;
my $attributeFound =0;
foreach $item (@{$node}){
my $bc = $item->{content};
if($bc eq $_[0])
{
$barcodeFound = 1;
my $barcodeAttribute = $item->{$_[1]};
if($barcodeAttribute)
{
return $barcodeAttribute;
}
else
{
return "Error: getSamplePlateAttributes found 0 attributes named [$_[1]], expected to find 1";
}
}
}
if(!$barcodeFound)
{
return "Error: getSamplePlateAttributes found $barcodeFound Barcodes named [$_[0]], expected to find 1";
}
}
#------------------------------------------------------------------------------
sub getNumberPCRplates{
my $node = $doc->{pcr_plates}->{barcode};
my $nodeLength = $#{$node};
return $nodeLength + 1;
}
sub getNumberSamplePlates{
my $node = $doc->{sample_plates}->{barcode};
my $nodeLength = $#{$node};
return $nodeLength + 1;
}
]]>
</script>
</component>
print("Starting Watcher")
open("C:/VWorks Workspace/Scripts/Shared Scripts/FileUtilities.js")
open("C:/VWorks Workspace/Scripts/Shared Scripts/XML_files.js")
var VWorksFolder = "C:/VWorks Workspace/"
var WorkingFolder = VWorksFolder + "WorkingFolder/"
var OutputFolder = VWorksFolder + "Output/"
print("OutputFolder : [" + OutputFolder + "]")
function main(input_file_name) {
var ip = new ActiveX("perl.inputparser")
var pe = new ActiveX("protocol.editor")
print("Opening Input File [" + input_file_name + "]")
ip.OpenInput(input_file_name)
var time_string = getTimeString()
var working_input_filename = WorkingFolder + time_string + "_" + StripPath(input_file_name)
print(working_input_filename)
ip.SaveInput(working_input_filename);
print("Success")
//--Edit Protocol
print("Editing Protocol")
pe.loadProtocol("C:\\VWorks Workspace\\Protocol Files\\Templates\\PCR.pro")
var startupScripts = "var ip = new ActiveX(\"perl.inputparser\") \n" + "ip.OpenInput(\"" + working_input_filename + "\")"
pe.findAndReplace("//startup_scripts", startupScripts);
if(ip.getOrderAttributes("plates_Sealed") == "0")
pe.deleteTask("Xpeel_task");
if (ip.getOrderAttributes("Add_Sample") == "0") {
pe.deleteSubProcess("Add Sample", "Bravo")
pe.deleteProcess("Sample Tips");
pe.deleteProcess("Sample Plate");
}
var protocolPath = pe.saveProtocol("C:\\VWorks Workspace\\WorkingFolder\\", "PCR", 1)
DeleteFile(input_file_name);
runset.appendProtocolFileToRunset(protocolPath, ip.getNumberPCRplates(), "", "")
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment