Skip to content

Instantly share code, notes, and snippets.

@egormkn
Created April 8, 2020 15:20
Show Gist options
  • Save egormkn/2533f9e53a4895e87b33a106bc239a88 to your computer and use it in GitHub Desktop.
Save egormkn/2533f9e53a4895e87b33a106bc239a88 to your computer and use it in GitHub Desktop.
Script for updating local genome-scale model database
#!/bin/bash
check_installed() {
command -v "$1" >/dev/null 2>&1 || {
echo >&2 "$1 required but it's not installed. Aborting."
exit 1
}
}
# Check required commands
check_installed 'curl'
check_installed 'jq'
# Get a list of models
MODELS=$(curl -s 'http://bigg.ucsd.edu/api/v2/models')
# Save models info to file
echo "$MODELS" | jq '.' > 'models.json'
# Get a number of models in list
MODEL_COUNT=$(echo "$MODELS" | jq '.results_count')
# Set a counter
MODEL_INDEX=0
echo "$MODELS" | jq -c '.results[]' | while read -r LINE; do
MODEL_INDEX=$(( MODEL_INDEX + 1 ))
ID=$(echo "$LINE" | jq -r '.bigg_id')
INFO=$(echo "$LINE" | jq -r '"\"\(.organism)\" (\(.bigg_id), \(.gene_count)/\(.reaction_count)/\(.metabolite_count))"')
echo -e "($MODEL_INDEX/$MODEL_COUNT) Downloading $INFO: \c"
mkdir -p "$ID"
XML_URL="http://bigg.ucsd.edu/static/models/$ID.xml.gz"
XML_FILE="./$ID/$ID.xml.gz"
ETAG_FILE="./$ID/$ID.etag"
ETAG=$([[ -f "$ETAG_FILE" ]] && cat "$ETAG_FILE" || echo "None")
RESPONSE_CODE=$(curl -I -H "If-None-Match: \"$ETAG\"" -s -o /dev/null -w "%{http_code}" "$XML_URL")
if [ "$RESPONSE_CODE" == "304" ]; then
echo "Not changed"
else
curl -s "http://bigg.ucsd.edu/api/v2/models/$ID" | jq '.' > "./$ID/model.json"
curl -s -D - "$XML_URL" -o "$XML_FILE" | grep -ioP 'Etag:\s*"?\K[^"]+' > "$ETAG_FILE" && echo "Saved" || echo "Error"
fi
done
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment