StephenMcGowan
asked on
Perl Script to input data from an XLS file
Hi,
I'm trying to write a script which takes .xls column entries (from a table of peptides), creates ranges of +/- 3 for each value, and then matches this range against another .xls file to return label information.
I've put together a step-by-step procedure for what I'm trying to achieve in "script_procedure.txt".
I've also attached sample files: "cropped_MB4_Elephant.xls" , "cropped_MB10_Dolphin.xls"
Finally, Label_List is used for the script to extract the label information for START and END +/- 3 ranges and input these into a new column.
The attached text file should make clearer sense.
Thanks,
Stephen.
cropped-MB4-Elephant.xls
cropped-MB10-Dolphin.xls
Label-List.xls
script-procedure.txt
I'm trying to write a script which takes .xls column entries (from a table of peptides), creates ranges of +/- 3 for each value, and then matches this range against another .xls file to return label information.
I've put together a step-by-step procedure for what I'm trying to achieve in "script_procedure.txt".
I've also attached sample files: "cropped_MB4_Elephant.xls"
Finally, Label_List is used for the script to extract the label information for START and END +/- 3 ranges and input these into a new column.
The attached text file should make clearer sense.
Thanks,
Stephen.
cropped-MB4-Elephant.xls
cropped-MB10-Dolphin.xls
Label-List.xls
script-procedure.txt
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
Hi jb1dev,
Thanks a lot for getting back to me with this! very much appreciated!
I've tried running the script you've written but I'm currently coming across this error message:
Can't call method "worksheets" on an undefined value @ line 45
which is here...
Thanks again,
Stephen.
Thanks a lot for getting back to me with this! very much appreciated!
I've tried running the script you've written but I'm currently coming across this error message:
Can't call method "worksheets" on an undefined value @ line 45
which is here...
# print "INFO Populating label maps...\n";
for my $worksheet ( $labelWorkbook->worksheets() ) {
my ( $row_min, $row_max ) = $worksheet->row_range();
my ( $col_min, $col_max ) = $worksheet->col_range();
Thanks again,
Stephen.
So line 45 is where it is trying to open the worksheets in the "label" file:
Is the label file located in the SRCDIR?
Is the label filename correct? Check my comments in the code here:
for my $worksheet ( $labelWorkbook->worksheets() ) {
Is the label file located in the SRCDIR?
Is the label filename correct? Check my comments in the code here:
#
# Read Label List (Label_List.xls)
# Spec calls this Label_List.xls
# but file is actually called Label-List.xls
# Please note dash in filename versus underscore and change accordingly
# when/if appropriate
#
my $labelParser = Spreadsheet::ParseExcel->new();
my $labelWorkbook = $labelParser->parse(
File::Spec->catpath( $VOLUME,
$SRCDIR,
'Label-List.xls') );
Can you post what you have changed the SRCDIR value to?
If you are on windows, are you using \\ for directory separator?
Sorry I do not have a Windows platform handy on which to try this.
I can add additional logging and checking for presence of the specified directories and files, if that would help.
Let me know.
If you are on windows, are you using \\ for directory separator?
Sorry I do not have a Windows platform handy on which to try this.
I can add additional logging and checking for presence of the specified directories and files, if that would help.
Let me know.
ASKER
Hi jb1,
This is how I modified the beginning section of code:
I'm guessing that the SRCDIR and DSTDIR are set wrong?
Thanks again,
Stephen.
This is how I modified the beginning section of code:
#!/usr/bin/perl -w
use strict;
use File::Spec;
use Spreadsheet::ParseExcel;
use Spreadsheet::WriteExcel;
#
# Set source dir /dst dir setting
#
my $VOLUME = 'C';
my $SRCDIR = '/Users/Stephen/Desktop/LCMS';
my $DSTDIR = '/Users/Stephen/Desktop/output';
#
# Read files matching "cropped_MB" from DIR
#
opendir DIR, $SRCDIR or die "Could not open dir: $!\n";
my @filelist = grep(/cropped_MB/i, readdir DIR);
closedir DIR;
#
# Read Label List (Label_List.xls)
#
my $labelParser = Spreadsheet::ParseExcel->new();
my $labelWorkbook = $labelParser->parse(
File::Spec->catpath( $VOLUME,
$SRCDIR,
'Label_List.xls') );
#
# Populate label maps to check ranges against.
#
my %startLabelMap;
my %endLabelMap;
# print "INFO Populating label maps...\n";
for my $worksheet ( $labelWorkbook->worksheets() ) {
my ( $row_min, $row_max ) = $worksheet->row_range();
my ( $col_min, $col_max ) = $worksheet->col_range();
for my $row ( $row_min .. $row_max ) {
if($row == 0) {
# Skip first row of label list, these are headers
# print "DEBUG Skipping ".$worksheet->get_cell( $row, 0 )->unformatted()."\n";
next;
}
my $label = $worksheet->get_cell( $row, 0 )->unformatted();
my $start = $worksheet->get_cell( $row, 1 )->unformatted();
my $end = $worksheet->get_cell( $row, 2 )->unformatted();
I'm guessing that the SRCDIR and DSTDIR are set wrong?
Thanks again,
Stephen.
Can you try setting:
my $VOLUME = 'C:\\';
my $VOLUME = 'C:\\';
Or try with the following:
my $VOLUME = '';
my $SRCDIR = 'C:/Users/Stephen/Desktop/ LCMS';
my $DSTDIR = 'C:/Users/Stephen/Desktop/ output';
I realize, my dir listing is not using the File::Spec so the above should hopefully work for finding your "LCMS" dir.
my $VOLUME = '';
my $SRCDIR = 'C:/Users/Stephen/Desktop/
my $DSTDIR = 'C:/Users/Stephen/Desktop/
I realize, my dir listing is not using the File::Spec so the above should hopefully work for finding your "LCMS" dir.
Check if the below filetest.pl works.
If it does, I will adjust the code to use this path instead.
If it does, I will adjust the code to use this path instead.
#!/usr/bin/perl
my $SRCDIR = 'C:/Users/Stephen/Desktop/LCMS';
my $file = $SRCDIR.'/'.'Label-List.xls';
if(-e $file) {
print "File exists\n";
}
NOTE
Be sure to change the SRCDIR and DSTDIR appropriately. They are located at the top of the script.
Be sure to have the perl modules Spreadsheet::ParseExcel and Spreadsheet::WriteExcel installed.
Open in new window