zampboy has asked for the wisdom of the Perl Monks concerning the following question:

Hey all, I'm using Mechanize to run a batch pathway analysis by looping over a set of files containing gene names (ie I have about 500 txt files each containing a list of genes. I want to upload the list of genes in each file to the website, get an output and then move on to the next file). My code works for the first run through the loop but then gives me an error "Can't call method "value" on an undefined value at /Library/Perl/5.12/WWW/Mechanize.pm line 1407, <IN> line 1." For some reason if I close IN or if I try to empty out $geneList, it destroys this variable. However, if I don't do this, it concatenates ALL of the genes together from all of the files (which is not what I want). I'm not sure where I'm messing up, so any help would be appreciated! FYI each file is formatted such that the genes are on a single line separated by a space.

#!/usr/bin/perl #use strict; use WWW::Mechanize; # this package is based on LWP, but behaves mor +e like a browser # we need it here for easy managment of cookies, n +eeded for logon use Switch; # may not be used here, implements case:switch my $numArgs = $#ARGV+1; if ($numArgs != 2) { print "\n"; print "usage: gseaRobot <inputDir> <outputDir>\n"; exit(); } my $inputDir = $ARGV[0]; my $outputDir = $ARGV[1]; my $username = 'kburns@jhmi.edu'; my $loginUrl = 'http://www.broadinstitute.org/gsea/login.jsp'; my $queryUrl = 'http://www.broadinstitute.org/gsea/msigdb/annotate.jsp +'; my $geneList = "$geneList"; #print $geneList,"\n"; my $geneSetName = ""; my $platform = "GENE_SYMBOL"; my $m = WWW::Mechanize->new(); ## LOGIN $m->get($loginUrl); $m->form_name('loginForm'); $m->field('j_username',$username); $m->field('j_password','password'); $response = $m->submit(); opendir (DIR, $inputDir) or die $!; while ($filename = readdir(DIR) ) { if($filename=~/.*\.txt/){ open IN, "</$inputDir/$filename"; #@genes=<IN>; while ($line=<IN>){ $geneList=$line; print "$geneList\n"; $m->get($queryUrl); $m->form_name('computeOverlapsForm'); $m->field('platform',$platform); $m->field('geneList',$geneList); $m->field('geneSetName',''); $m->field('collection','C2'); ##'CP:KEGG', ##'CP:REACTOME', ##'CP', ##'CGP', ##'CP:BIOCARTA'; $response = $m->submit(); ## PERFORM DOWNLOAD $m->form_name('downloadForm'); $m->field('platform',$platform); $m->field('maxShown','10'); $m->field('geneList',$geneList); $m->field('geneSetName',''); $m->field('collection','C2'); $response = $m->submit(); ###Write to outfile in output directory stated on c +ommand line $outfile=$outputDir."/".$filename; $m->save_content($outfile); print $response->content(); close IN; } } }

Replies are listed 'Best First'.
Re: Mechanize FAIL
by Anonymous Monk on Jun 10, 2012 at 07:24 UTC
      I agree...I just don't know where the problem is scope is :(