0

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.
Dave Cross
  • 68,119
  • 3
  • 51
  • 97
  • What is the error? – Toto Nov 15 '16 at 10:43
  • You have to install `XML::Simple` from CPAN – Toto Nov 15 '16 at 10:47
  • 2
    Or better, don't [`XML::Simple is discouraged`](http://stackoverflow.com/questions/33267765/why-is-xmlsimple-discouraged) and use `XML::Twig` or `XML::LibXML` instead. – Sobrique Nov 15 '16 at 10:48
  • Are you on Windows or Unix? And what's the specific file you're using? (URL). – Sobrique Nov 15 '16 at 10:52
  • `cpan install XML::Twig` Or look at a the package manager. This might be relevant: http://apple.stackexchange.com/questions/75263/how-to-install-cpan-modules-from-the-command-line-on-os-x – Sobrique Nov 15 '16 at 10:54
  • @LearnerAlgorithm: Google search for [installing perl modules on osx](https://www.google.co.uk/search?q=installing+perl+modules+on+osx) – Dave Cross Nov 15 '16 at 10:54
  • @Toto I revised my question now – Learner Algorithm Nov 15 '16 at 15:08
  • @Sobrique I revised my question – Learner Algorithm Nov 15 '16 at 15:09
  • Still need a URL. But `XML::Twig` doesn't work like that. It's never going to. For starters - you need `$twig -> parse` rather than `XMLin` – Sobrique Nov 15 '16 at 15:13
  • @Sobrique can you give me a solution because i am really new in this program and it will take me forever to do it – Learner Algorithm Nov 15 '16 at 15:16
  • Yes. But I'll need some example XML source, (edited into the question - I can't tell which file you're downloading) and which values you actually want to extract from it. It's easier than you think. – Sobrique Nov 15 '16 at 15:17
  • @LearnerAlgorithm: StackOverflow doesn't exist to give solutions to people. It is for programmers to help each other to solve problems in code that they have written. If you want someone to write code for you, then you should employ a programmer. – Dave Cross Nov 15 '16 at 15:18
  • @LearnerAlgorithm: I've just edited your question to put back the original error, which you had removed. It's a bad idea to remove old parts of the question like that as it invalidates a lot of the other discussion on the page. Please take more care in the future. – Dave Cross Nov 15 '16 at 15:23

2 Answers2

1

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.

Community
  • 1
  • 1
Dave Cross
  • 68,119
  • 3
  • 51
  • 97
  • 1
    Once there's a sensible parser in use, Stack Overflow is a good place to come back to for an example of how to do it easily. – Sobrique Nov 15 '16 at 10:52
  • @Dave Cross after I installed twig , I am getting again error and that is why I did not accept your answer. I revised my question above – Learner Algorithm Nov 15 '16 at 15:08
1

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)

Sobrique
  • 52,974
  • 7
  • 60
  • 101