| 1 |
#!/bin/bash |
| 2 |
|
| 3 |
# Public domain notice for all NCBI EDirect scripts is located at: |
| 4 |
# https://www.ncbi.nlm.nih.gov/books/NBK179288/#chapter6.Public_Domain_Notice |
| 5 |
|
| 6 |
# gff-sort |
| 7 |
|
| 8 |
# HERE document for mapping feature keys to sort order (other keys ending with RNA separately mapped to 2) |
| 9 |
IFS='' read -r -d '' TYPEMAP <<'EOF' |
| 10 |
gene 1 |
| 11 |
pseudogene 1 |
| 12 |
mRNA 2 |
| 13 |
primary_transcript 2 |
| 14 |
C_region 2 |
| 15 |
D_segment 2 |
| 16 |
J_segment 2 |
| 17 |
N_region 2 |
| 18 |
S_region 2 |
| 19 |
V_region 2 |
| 20 |
V_segment 2 |
| 21 |
CDS 3 |
| 22 |
exon 4 |
| 23 |
intron 4 |
| 24 |
EOF |
| 25 |
|
| 26 |
temp1=$(mktemp /tmp/GFF_TEMP1.XXXXXXXXX) |
| 27 |
temp2=$(mktemp /tmp/GFF_TEMP2.XXXXXXXXX) |
| 28 |
|
| 29 |
grep '.' | |
| 30 |
sed '/^#/d' | |
| 31 |
# read GFF3 tab-delimited data into XML structure, taking field names from the command line |
| 32 |
tbl2xml -rec Rec SeqID Source Type Start End Score Strand Phase Attributes | |
| 33 |
# use xtract -with and -split arguments to separate individual tag=value attributes, |
| 34 |
# also use HERE document and suffix test to convert feature type to sort order number |
| 35 |
xtract -transform <( echo -e "$TYPEMAP" ) -rec Rec \ |
| 36 |
-pattern Rec \ |
| 37 |
-group Rec -pkg Fields \ |
| 38 |
-block "Rec/*" -element "*" \ |
| 39 |
-block Type -if Type -ends-with RNA -wrp Feat -lbl 2 \ |
| 40 |
-else -def 5 -wrp Feat -translate Type \ |
| 41 |
-group Rec -pkg Content -wrp Item -with ";" -split Attributes | |
| 42 |
# use xtract prefix and suffix trimming constructs to isolate tag and value, making new |
| 43 |
# XML objects with the extracted tag as the object name: <tag>value</tag> |
| 44 |
xtract -rec Rec \ |
| 45 |
-pattern Rec \ |
| 46 |
-group Fields -element "*" \ |
| 47 |
-group Content -pkg Content \ |
| 48 |
-block Item -TAG "Item[|=]" -wrp "&TAG" -element "Item[=|]" | |
| 49 |
transmute -mixed -format > $temp1 |
| 50 |
|
| 51 |
cat "$temp1" | |
| 52 |
# generate table with identifier, parent, feature key, and sort order columns |
| 53 |
xtract -pattern Rec -if Content/ID -def "-" -element ID Parent Type Feat | |
| 54 |
# convert to table with identifier and calculated lineage columns |
| 55 |
transmute -p2l > $temp2 |
| 56 |
|
| 57 |
cat "$temp1" | |
| 58 |
xtract -transform "$temp2" \ |
| 59 |
-pattern Rec \ |
| 60 |
-group "Fields/*" -element "~" \ |
| 61 |
-group Content -def "-" -translate ID | |
| 62 |
sort-table -k 1,1Vf -k 11,11f -k 7,7f -k 4,4n -k 5,5nr -k 10,10n | |
| 63 |
cut -f 1-9 |
| 64 |
|
| 65 |
rm "$temp2" |
| 66 |
rm "$temp1" |