Gff To Json

From Biowiki
Jump to: navigation, search

Converts GFF to JSON, in the worst possible way. This was just to generate test data for client-side experimentation. There's a chance that I'll want to come back and make it something other than a horrible hack, so I'm recording this here. Specifically, things like translating the GFF3 tag-value pair URL escaping method to the JSON backslash escaping method.

This is the most heinous 1-liner I have ever done, but while it's dirty it was quick:

for chrom in 2L 2R 3L 3R 4 X; do for type in `grep "^$chrom" dmel-$chrom-r5.1.gff | cut -f3 | sort | uniq`; do (echo '['; cat dmel-$chrom-r5.1.gff | perl -e 'while (<STDIN>) {@F = split("\t"); %atts = ($F[8] =~ /([^;=]+)=([^;]*);/g); print "{type:\"$F[2]\",start:$F[3],end:$F[4]" . ($F[8] ? "," : "") . join(",", map {$_ . ":\"" . $atts{$_} . "\""} keys %atts) . "}\n" if ($F[2] eq $ARGV[0]);}' $type; echo ']') > $chrom/$type.json; done; done

stab #2, find types first, add more fields:

for chrom in 2L 2R 3L 3R 4 X; do grep "^$chrom" dmel-$chrom-r5.1.gff | cut -f3 | sort | uniq > $chrom/types; done

for chrom in 2L 2R 3L 3R X; do for type in `cat 4/types`; do (echo '['; cat dmel-$chrom-r5.1.gff | perl -e 'while (<STDIN>) {exit(0) if /^###/; @F = split("\t"); %atts = ($F[8] =~ /([^;=]+)=([^;]*);/g); print "{type:\"$F[2]\",start:$F[3],end:$F[4]" . ($F[5] ne "." ? ",score:$F[5]" : "") . ($F[6] ne "." ? ",strand:\"$F[6]\"" : "") . ($F[7] ne "." ? ",phase:$F[7]" : "") . ($F[8] ? "," : "") . join(",", map {$_ . ":\"" . $atts{$_} . "\""} keys %atts) . "},\n" if ($F[2] eq $ARGV[0]);}' $type; echo ']') > $chrom/$type.json; done; done

-- Mitch Skinner - 13 Mar 2007