Skip to content

Instantly share code, notes, and snippets.

@pmenzel
Created February 8, 2023 15:33
Show Gist options
  • Save pmenzel/80c2c240df472489577dff3b57ec071d to your computer and use it in GitHub Desktop.
Save pmenzel/80c2c240df472489577dff3b57ec071d to your computer and use it in GitHub Desktop.
make-igv-genome-json.pl
#!/usr/bin/env perl
# make-igv-genome-json.pl
# Peter Menzel
#
# This script creates an IGV genome json file (https://github.com/igvteam/igv/wiki/JSON-Genome-Format)
# The arguments are the paths to fasta, fai and gff files.
#
# Example usage:
# datasets download genome accession GCF_002101575.1
# unzip ncbi_dataset.zip
# samtools faidx ncbi_dataset/data/GCF_002101575.1/GCF_002101575.1_ASM210157v1_genomic.fna
# make-igv-genome-json.pl ncbi_dataset/data/GCF_002101575.1/GCF_002101575.1_ASM210157v1_genomic.fna.fai ncbi_dataset/data/GCF_002101575.1/GCF_002101575.1_ASM210157v1_genomic.fna ncbi_dataset/data/GCF_002101575.1/genomic.gff
#
use strict;
use warnings;
my $id;
my $fasta;
my $fai;
my %tracks;
while(my $arg = pop(@ARGV)) {
die "$arg is not a readable file." unless -r $arg and ! -d $arg;
if($arg =~ /.*?([^\/]+)\.(fasta|fna|fa)$/) {
$fasta = $arg;
$id = $1;
}
elsif($arg =~ /\.fai$/) {
$fai = $arg;
}
elsif($arg =~ /\.(gff|gtf|gff3)$/) {
$tracks{$arg} = $1;
}
}
die "Missing FASTA file (.fa, .fna, or .fasta)" unless length $fasta;
die "Missing FASTA Index (.fai) file" unless length $fai;
print "{\n \"id\": \"$id\",\n \"name\": \"$id\",\n \"fastaURL\": \"$fasta\",\n \"indexURL\": \"$fai\"";
if(length keys %tracks) {
print ",\n \"tracks\": [\n";
my @list_track_ids = keys %tracks;
foreach my $i (0..$#list_track_ids) {
my $type = $tracks{$list_track_ids[$i]};
print " {\n \"name\": \"Genes ", $type, "\",\n \"url\": \"$list_track_ids[$i]\",\n \"format\": \"$type\"\n }";
print $i == $#list_track_ids ? "\n" : ",\n";
}
print " ]\n}\n";
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment