Perl Module to convert OWL files to OBO - perl-module

I am new to Perl and have never used a CPAN module before, I need to convert an OWL file to OBO format. I successfully installed the "ONTO-PERL-1.37" module. I want to use the following script:
use Carp;
use strict;
use warnings;
use OBO::Parser::OWLParser;
my $my_parser = OBO::Parser::OWLParser->new();
my $ontology = $my_parser->work(shift(#ARGV));
$ontology->export('obo');
exit 0;
END
My question is do i need to declare the input OWL file, if yes how can that be done for the above script?

This script takes the argument from the command line. This is what the shift(#ARGV) is for. shift removes the first element from a list and #ARGV is a list filled with the filename given as a command line argument, when running the script with perl owl2obo.pl my-ontology.owl.
If it easier, you can modify the script with
my $ontology = $my_parser->work('/the/path/to/ontology.owl');
or
my $owlfile = shift(#ARGV) || '/path/to/owlfile.owl';
my $ontology = $my_parser->work($owlfile);
.

Related

Extracting the name given as an argument from command line in Perl script

I am new to the scripting languages, and I have a task that says I need to extract the name from a given argument from the command line in erl.
I am calling the Perl script like this
./perl.plx file.txt
and I need to get only that file name, not the whole file.txt
The command line arguments to a Perl script appear in #ARGV, described in perldoc perlvar.
Parsing filenames seems trivial, but appearances may be misleading. However, Perl ships with a module called File::Basename that handles edge cases you might not immediately consider. One edge case that simple split wouldn't handle is the potential for dots to appear elsewhere in the filename aside from the final suffix.
You can review File::Basename's documentation by typing perldoc File::Basename at the command prompt.
Here is an example:
use stict;
use warnings;
use File::Basename qw(fileparse);
my ($fname, $dirs, $suffix) = fileparse($ARGV[0], qr/\.txt/);
print "Base file name is $fname\n";
print "Suffix is $suffix\n";
print "Path to $fname$suffix is $dirs\n";
Because this module ships with Perl, you don't need to install anything to use it. In taking advantage of the core Perl modules that ship with every Perl distribution, you leverage best practices and debugging embodied within these tools.
To get the name of the file, you need to extract it fromĀ #ARGV, and split it on the dot.
my $fileName = (split /\./, $ARGV[0])[0];
Explaination:
split /\./, $ARGV[0] splits on a dot (the character "." is a special character in regular expressions, it means 'any character'; to have it literally, you need to escape it, thus, /\./).
(...)[0]; takes the first element, i.e. the file name.

Trying to create a Ruby script. I want to append Strings containing commands to a file

mysqldump = "mysqldump"
`#{mysqldump} > backup_file.sql`
I'm supposed to append several of those mysqldump Strings (I simplified it for this example; normally line 2 would have the username and password as options) into the SQL file.
The problem is line 2, when I try to call the Bash operator '>' to append the String. Instead of appending, the script ends up calling the mysqldump command itself.
What can I do to store the String "mysqldump" into the file backup_file.sql? I want to do it the same way as line 2: automatically appending through the Bash.
if you are trying to append "like" you said and not overwrite the target file use >> instead of > . Here is a working version of your script:
za$ emacs ./foo.rb
#!/usr/bin/env ruby
target_dir = "/Users/za/ruby-practice/backup_file.sql"
mysqldump = "mysqldump"
`echo #{mysqldump} >> "#{target_dir}"`
You can also do something like : system %Q{echo "#{mysqldump}" >> "#{target_dir}"}
. Personally , I would say use IO#puts instead of making system calls inside your script , if you want a pure ruby solution/system independent solution.
Why don't you use pure ruby to do it? Like:
File.open("backup_file.sql", "w") do |f|
dump_lines.each do |line|
f.puts line
end
end
assuming that you have the dump in an array..

Bash-style Programming-quote functionality in Matlab?

I would use programming-quote like this in Bash
$ export b=`ls /`
$ echo $b
Applications Library Network System Users Volumes tmp usr var
and now I want to find similar functionality in Matlab. Also, I want to find a command that outputs relative paths, not absolute paths like Matlab's ls -- I feel like reinventing the wheel if I am parsing this with a regex. I need to find this command to debug what is wrong with namespaces here. Familiar-Bash-style functionatilities would be so cool.
For your first question, I think you can get that behavior with anonymous functions:
b = #() ls('C:\'); %I'm on windows
b()
The expression b() now returns the contents of my C drive.
The Matlab equivalent of bash backticks is calling the system() function and using the second output argument. It will run an external command and capture the output.
[status,b] = system('ls /');
If it's a string of Matlab code you want to run and capture the console output of, use evalc.
But to just get a listing of files, you want the Matlab dir function. Lots easier than parsing that output string, and you get more info. See Matlab dir documentation or doc dir for more details.
children = dir('/');
childNames = { children.name };
childIsDir = [ children.isdir ];

Simple map for pipeline in shell script

I'm dealing with a pipeline of predominantly shell and Perl files, all of which pass parameters (paths) to the next. I decided it would be better to use a single file to store all the paths and just call that for every file. The issue is I am using awk to grab the files at the beginning of each file, and it's turning out to be a lot of repetition.
My question is: I do not know if there is a way to store key-value pairs in a file so shell can natively do something with the key and return the value? It needs to access an external file, because the pipeline uses many scripts and a map in a specific file would result in parameters being passed everywhere. Is there some little quirk I do not know of that performs a map function on an external file?
You can make a file of env var assignments and source that file as need, ie.
$ cat myEnvFile
path1=/x/y/z
path2=/w/xy
path3=/r/s/t
otherOpt1="-x"
Inside your script you can source with either . myEnvFile or the more versbose version of the same feature sourc myEnvFile (assuming bash shell) , i.e.
$cat myScript
#!/bin/bash
. /path/to/myEnvFile
# main logic below
....
# references to defined var
if [[ -d $path2 ]] ; then
cd $path2
else
echo "no pa4h2=$path2 found, can't continue" 1>&1
exit 1
fi
Based on how you've described your problem this should work well, and provide a-one-stop-shop for all of your variable settings.
IHTH
In bash, there's mapfile, but that reads the lines of a file into a numerically-indexed array. To read a whitespace-separated file into an associative array, I would
declare -A map
while read key value; do
map[$key]=$value
done < filename
However this sounds like an XY problem. Can you give us an example (in code) of what you're actually doing? When I see long piplines of grep|awk|sed, there's usually a way to simplify. For example, is passing data by parameters better than passing via stdout|stdin?
In other words, I'm questioning your statement "I decided it would be better..."

Why don't the keys with empty string values for Perl's %ENV show up Windows subprocesses?

I want to (need to) start a sub-process from a perl script that checks certain environment variables. In one instance the environment variable needs to be there but empty.
$ENV{"GREETING"} = "Hello World"; # Valid
$ENV{"GREETING"} = ""; # also valid
I can set $ENV{"GREETING"} = ""; and in that perl script $ENV{"GREETING"} is empty, but in any sub-process that environment variable is not there.
Here is some example code to demonstrate. This script, env_in.pl sets up some environment variables, ZZZ_3 is empty. It then calls env_out.pl to output the environment variables, ZZZ_3 is missing from the output.
#!/usr/bin/perl
# env_in.pl
use strict;`enter code here`
use warnings;
$ENV{ZZZ_1} = "One";
$ENV{ZZZ_2} = "Two";
$ENV{ZZZ_3} = "";
$ENV{ZZZ_4} = "Four";
my (#cmd) = ("perl", "env_out.pl");
system(#cmd) == 0 or die "system #cmd failed: $?";
Here is the env_out.pl script.
#!/usr/bin/perl
use strict;
use warnings;
print ($_," = ", $ENV{$_}, "\n") for (sort keys %ENV);
I'm using ActiveState perl version v5.8.8 on a WinXP box.
I know that this DOES work in python, but I don't have a choice about the implementation language, it has to be Perl.
As far as I know, there's no such thing as an empty environment variable in Windows. Defining them to empty is the same thing as undefining them.
Your Perl scripts does indeed display a ZZZ_3 entry when run on a Unix-like system.
That seems to be some issue with your activestate perl. Testing on windows2000 + cygwin gives:
$ perl --version
This is perl, v5.10.0 built for cygwin-thread-multi-64int
(with 6 registered patches, see perl -V for more detail)
Copyright 1987-2007, Larry Wall
Perl may be copied only under the terms of either the Artistic License or the
GNU General Public License, which may be found in the Perl 5 source kit.
Complete documentation for Perl, including FAQ lists, should be found on
this system using "man perl" or "perldoc perl". If you have access to the
Internet, point your browser at http://www.perl.org/, the Perl Home Page.
$ perl env_in.pl | grep ZZZ
ZZZ_1 = One
ZZZ_2 = Two
ZZZ_3 =
ZZZ_4 = Four
$
Using both strawberry perl and cygwin perl on vista results in
ZZZ_1 = One
ZZZ_2 = Two
ZZZ_4 = Four

Resources