0
votes

I am trying to parse a XML file. I download the data from here http://mips.helmholtz-muenchen.de/proj/ppi/

and I use this code but I get error

use strict;
use warnings;
use XML::Twig;

my $MIPS_file = $ARGV[0];
my $xml = XML::Twig->new();
my $data = $xml->XMLin("$MIPS_file");
my $intList = $data->{'entry'}->{'interactionList'}->{'interaction'};
foreach my $int (@{$intList}) {
  my $experiment_type = $int->{'experimentList'}->{'experimentDescription'}->{'interactionDetection'}->{'names'}->{'shortLabel'};
  my $partList = $int->{'participantList'}->{'proteinParticipant'};
  my ($p1,$p2);
  foreach my $protPart(@{$partList}) {
      if ($protPart->{'proteinInteractor'}->{'organism'}->{'ncbiTaxId'} eq "9606") { # select human proteins
    if (!$p1) {
      $p1 = $protPart->{'proteinInteractor'}->{'xref'}->{'primaryRef'}->{'id'};
    }
    else {
      $p2 = $protPart->{'proteinInteractor'}->{'xref'}->{'primaryRef'}->{'id'};
    }
      }
  }
  print "$p1\$p2\n";
}

I put the file on a folder in desktop (mac) Then I open terminal and I invoke the program like perl myfile.pl

This is the error I get

Can't locate XML/Simple.pm in @INC (@INC contains: /Users/admin/perl5/perlbrew/perls/perl-5.16.0/lib/site_perl/5.16.0/darwin-2level /Users/admin/perl5/perlbrew/perls/perl-5.16.0/lib/site_perl/5.16.0 /Users/admin/perl5/perlbrew/perls/perl-5.16.0/lib/5.16.0/darwin-2level /Users/admin/perl5/perlbrew/perls/perl-5.16.0/lib/5.16.0 .) at myfile.pl line 3. BEGIN failed--compilation aborted at myfile.pl line 3.

After installing twig , now i get this error

Use of uninitialized value $MIPS_file in string at myfile.pl line 7.
Can't locate object method "XMLin" via package "XML::Twig" at myfile.pl line 7.
2
What is the error?Toto
You have to install XML::Simple from CPANToto
Or better, don't XML::Simple is discouraged and use XML::Twig or XML::LibXML instead.Sobrique
Are you on Windows or Unix? And what's the specific file you're using? (URL).Sobrique
cpan install XML::Twig Or look at a the package manager. This might be relevant: apple.stackexchange.com/questions/75263/…Sobrique

2 Answers

1
votes

XML::Simple is not a part of the standard Perl installation. If you want to use it, then you will need to install it. This answer gives a good overview of how to do that.

However, you should read the documentation for XML::Simple, which says:

The use of this module in new code is discouraged. Other modules are available which provide more straightforward and consistent interfaces. In particular, XML::LibXML is highly recommended and XML::Twig is an excellent alternative.

I strongly recommend that you abandon your use of XML::Simple in favour of one of the other modules mentioned above.

Update: You've now installed XML::Twig, and have updated your question to add the error messages that you are getting.

Use of uninitialized value $MIPS_file in string at myfile.pl line 7.

Can't locate object method "XMLin" via package "XML::Twig" at myfile.pl line 7.

Line 7 seems to be this:

my $data = $xml->XMLin("$MIPS_file");

The variable $MIPS_file is given a value a few lines earlier in this line:

my $MIPS_file = $ARGV[0];

The @ARGV array is where you can access any command-line arguments passed to your program. The fact that $MIPS_file contains undef strongly implies that aren't passing any arguments to your program. You need to run it like this:

myfile.pl name_of_your_xml_file.xml

The second error is more interesting.

Can't locate object method "XMLin" via package "XML::Twig" at myfile.pl line 7.

You have switched from using XML::Simple to using XML::Twig. But to do that you have only changed the use line in your program. You haven't changed any of the actual code. XML::Simple and XML::Twig are completely different libraries. They don't work in the same way at all. XML::Twig doesn't have an XMLIn() method. You will need to read the documentation for XML::Twig and change your code to use the various functions that this module provides.

1
votes

Without knowing exactly which URL you're downloading, I can't give you a firm answer.

However, a very rough XML::Twig example that might do what you're looking for is:

#!/usr/bin/env perl
use strict;
use warnings;

use XML::Twig;

my $MIPS_file = $ARGV[0];
my $xml = XML::Twig->new();
$xml -> parsefile ( $MIPS_file );

#assuming ncbTaxId is an attribute - I don't know, this is part of the problem with XML::Simple
foreach my $element ( $xml -> get_xpath ( '//proteinInteractor/organism[@ncbiTaxId="9606"]/..' ) ) {
    $element -> print; #debugging;
    #assuming 'id' is an attrbute of 'primaryRef' subelement. 
    print $element -> get_xpath('.//primaryRef',0) -> att('id'); 
}

Note - this is a bit of a guess based on your XML::Simple code, rather than referencing the source XML (because I don't know which XML source you're using). This is part of the problem with XML::Simple - it can't represent XML completely (at least, not very easily)