Skip to content

Instantly share code, notes, and snippets.

@sestaton
Last active May 31, 2016 20:23
Show Gist options
  • Save sestaton/88dc204b44942060fe62aa06bacf3669 to your computer and use it in GitHub Desktop.
Save sestaton/88dc204b44942060fe62aa06bacf3669 to your computer and use it in GitHub Desktop.
Simple script to get sequences from UniProt
# always include these lines! (implicit in recent Perl versions)
use strict; # follow the rules
use warnings; # warning when we make a mistake
# import method to perform the request
use LWP::UserAgent;
# make the format an option perhaps
my $format = 'fasta';
my $urlbase = 'http://www.uniprot.org/uniprot/';
# some code/loop here to get IDs
my $id = 'Q6GZW6';
# format your query
my $url = join '.', $urlbase.$id, $format;
my $out = join '.', $id, $format;
# get a response
my $ua = LWP::UserAgent->new;
# $ua is an LWP::UserAgent object (type "print ref($ua)" to see for yourself)
my $response = $ua->get($url);
# $response is an HTTP::Response object (type "print ref($response)" to see for yourself)
# check if it was successful!
unless ($response->is_success) {
die "Can't get url $url -- Status: ", $response->status, " -- Reason: ", $response->reason;
}
# print the results!
open my $outfh, '>', $out or die "\nERROR: Could not open file: $out";
print $outfh $response->content;
close $outfh;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment