Commit 3275684e authored by Aaron Petkau's avatar Aaron Petkau

Initial commit

parents
This diff is collapsed.
#!/usr/bin/env perl
use strict;
use warnings;
use HTTP::Tiny;
use Getopt::Long qw(GetOptions);
use Parse::CPAN::Meta;
use Term::ANSIColor;
use Data::Dumper;
use Time::Piece;
my $usage = <<"HELP";
Usage:
$0 [--help] --username USERNAME [--password PASSWORD] [--api-url URL] [--client-id CLIENT_ID] [--client-secret CLIENT_SECRET] [--report FILENAME]
--help Print this message
--username Your IRIDA/NGS Archive username
--password Your IRIDA/NGS Archive password (if you do not provide a value, you will be prompted to enter one)
--api-url The IRIDA API that you want to connect to (default: NML IRIDA)
--client-id The Client ID you were issued (default: NML IRIDA)
--client-secret The Client secret you were issued (default: NML IRIDA)
--project-id The particular project ID to check.
--reference-size The reference genome size (in bp).
--report The filename you'd like to use for writing out the report (default: irida-report.tsv)
HELP
my $api = 'default';
my $username;
my $password;
my $client_id = 'default';
my $client_secret = 'default';
my $report_name = "irida-report.tsv";
my $reference_size;
my $project_id;
my $help;
GetOptions(
'help' => \$help,
'api-url=s' => \$api,
'username=s' => \$username,
'password=s' => \$password,
'client-id=s' => \$client_id,
'client-secret=s' => \$client_secret,
'project-id=i' => \$project_id,
'reference-size=i' => \$reference_size,
'report=s' => \$report_name );
my $oauth_token_url = "$api/oauth/token";
if ($help or not $username or not $project_id or not $reference_size) {
print color('bold red');
print "--username is a required option.\n" if (not $username);
print "--project-id is a required option.\n" if (not $project_id);
print "--reference-size is a required option.\n" if (not $reference_size);
print color('reset');
print $usage;
exit(not $help);
}
if (not $password) {
# allow user to type in password here:
print 'Password: ';
system('stty -echo');
$password = <STDIN>;
chomp $password;
system('stty echo');
print "\n";
}
my $response = HTTP::Tiny->new->post_form($oauth_token_url, {
client_id => $client_id,
client_secret => $client_secret,
grant_type => "password",
username => $username,
password => $password });
# To future explorers:
# I'm abusing this because it comes with core and I don't want to explain to
# people how to install perl modules if they don't have them. (JSON is not
# in core, but Parse::CPAN::Meta is).
my $oauth_info = Parse::CPAN::Meta->load_json_string($response->{'content'});
my $oauth_access_token = $oauth_info->{'access_token'};
unless ($oauth_access_token) {
print color('bold red');
print "Could not log in to IRIDA, please check your username and password.\n";
print color('reset');
print $usage;
exit(1);
}
my $client = HTTP::Tiny->new( default_headers => {
'Authorization' => "Bearer $oauth_access_token" },
agent => "IRIDA Sample Searcher v1.0 ");
my @projects = ($project_id);
open my $report, '>', $report_name;
print $report "Project Name\tSample Name\tFile Pair\tMean Coverage\tTotal Bases\tReference Size\tProject ID\tSample ID\tFile Pair ID\n";
foreach my $project (@projects) {
my $project_url = "$api/projects/$project";
my $project_info = Parse::CPAN::Meta->load_json_string(
$client->get($project_url)->{'content'})->{'resource'};
my ($samples_link) = grep { $_->{'rel'} eq 'project/samples' } @{$project_info->{'links'}};
my $samples = Parse::CPAN::Meta->load_json_string(
$client->get($samples_link->{'href'})->{'content'})->{'resource'}->{'resources'};
foreach my $sample (@{$samples}) {
my ($files_link) = grep { $_->{'rel'} eq 'sample/sequenceFiles' } @{$sample->{'links'}};
my $files = Parse::CPAN::Meta->load_json_string(
$client->get($files_link->{'href'})->{'content'})->{'resource'};
my ($pairs_link) = grep { $_->{'rel'} eq 'sample/sequenceFiles/pairs' } @{$files->{'links'}};
my ($unpaired_link) = grep { $_->{'rel'} eq 'sample/sequenceFiles/unpaired' } @{$files->{'links'}};
my $pairs = Parse::CPAN::Meta->load_json_string(
$client->get($pairs_link->{'href'})->{'content'})->{'resource'}->{'resources'};
my $unpaired_list = Parse::CPAN::Meta->load_json_string(
$client->get($unpaired_link->{'href'})->{'content'})->{'resource'}->{'resources'};
for my $unpaired (@$unpaired_list) {
print STDERR "Skipping unpaired sequence file [id=$unpaired->{'identifier'}, label=$unpaired->{'label'}] in sample [id=$sample->{'identifier'}, name=$sample->{'sampleName'}], project [id=$project]\n";
}
foreach my $sequence_file_pair (@$pairs) {
my ($forward_link) = grep { $_->{'rel'} eq 'pair/forward' } @{$sequence_file_pair->{'links'}};
my ($reverse_link) = grep { $_->{'rel'} eq 'pair/reverse' } @{$sequence_file_pair->{'links'}};
my $forward = Parse::CPAN::Meta->load_json_string(
$client->get($forward_link->{'href'})->{'content'})->{'resource'};
my $reverse = Parse::CPAN::Meta->load_json_string(
$client->get($reverse_link->{'href'})->{'content'})->{'resource'};
my ($forward_qc_link) = grep { $_->{'rel'} eq 'sequencefile/qc' } @{$forward->{'links'}};
my ($reverse_qc_link) = grep { $_->{'rel'} eq 'sequencefile/qc' } @{$reverse->{'links'}};
my $forward_qc = Parse::CPAN::Meta->load_json_string(
$client->get($forward_qc_link->{'href'})->{'content'})->{'resource'};
my $reverse_qc = Parse::CPAN::Meta->load_json_string(
$client->get($reverse_qc_link->{'href'})->{'content'})->{'resource'};
my $total_bases = $forward_qc->{'totalBases'} + $reverse_qc->{'totalBases'};
my $coverage = sprintf "%0.0f",($total_bases/$reference_size);
my $project_date = localtime($project_info->{'createdDate'}/1000)->strftime('%F %T');
my $sample_date = localtime($sample->{'createdDate'}/1000)->strftime('%F %T');
my $sample_updated_date = localtime($sample->{'modifiedDate'}/1000)->strftime('%F %T');
my $file_pair_label = $sequence_file_pair->{'label'};
# Remove whitespace separating file pair labels
$file_pair_label =~ s/\s//g;
# comma to semicolon to less impact defaults for importing CSV
$file_pair_label =~ s/,/;/g;
print $report "$project_info->{'name'}\t$sample->{'sampleName'}\t\"$file_pair_label\"\t$coverage\t$total_bases\t$reference_size\t$project_info->{'identifier'}\t$sample->{'identifier'}\t$sequence_file_pair->{'identifier'}\n";
}
}
}
close $report;
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment