Unverified Commit 1a499fb2 authored by Carlos Eduardo Ribas's avatar Carlos Eduardo Ribas Committed by GitHub
Browse files

Fetch SVG images from FTP server (#514)

* Fetch SVG images from FTP server
* Layout from the FTP
* Check the database if FTP does not have a 2D
parent 9ad4ab96
...@@ -12,6 +12,8 @@ See the License for the specific language governing permissions and ...@@ -12,6 +12,8 @@ See the License for the specific language governing permissions and
limitations under the License. limitations under the License.
""" """
import re import re
import requests
import zlib
from itertools import chain from itertools import chain
from django.http import Http404, HttpResponse from django.http import Http404, HttpResponse
...@@ -362,21 +364,45 @@ class SecondaryStructureSVGImage(generics.ListAPIView): ...@@ -362,21 +364,45 @@ class SecondaryStructureSVGImage(generics.ListAPIView):
permission_classes = (AllowAny,) permission_classes = (AllowAny,)
def get(self, request, pk=None, format=None): def get(self, request, pk=None, format=None):
ftp = "http://ftp.ebi.ac.uk/pub/databases/RNAcentral/current_release/.secondary-structure/secondary-structure/{}.svg.gz"
upi = list(self.kwargs['pk'])
upi_path = "".join(upi[0:3]) + "/" \
+ "".join(upi[3:5]) + "/" \
+ "".join(upi[5:7]) + "/" \
+ "".join(upi[7:9]) + "/" \
+ "".join(upi[9:11]) + "/"
url = ftp.format(upi_path + "".join(upi))
try:
response = requests.get(url)
response.raise_for_status()
svg_ftp = zlib.decompress(response.content, zlib.MAX_WBITS | 32)
except requests.exceptions.HTTPError as e:
svg_ftp = None
try: try:
upi = self.kwargs['pk'] svg_bd = SecondaryStructureWithLayout.objects.get(urs="".join(upi))
image = SecondaryStructureWithLayout.objects.get(urs=upi) svg_bd = svg_bd.layout
except SecondaryStructureWithLayout.DoesNotExist: except SecondaryStructureWithLayout.DoesNotExist:
svg_bd = None
if not svg_ftp and not svg_bd:
return Response(status=status.HTTP_404_NOT_FOUND) return Response(status=status.HTTP_404_NOT_FOUND)
return HttpResponse(self.generate_thumbnail(image.layout, upi), content_type='image/svg+xml') return HttpResponse(
self.generate_thumbnail(svg_ftp if svg_ftp else svg_bd, "".join(upi)), content_type='image/svg+xml'
)
def generate_thumbnail(self, image, upi): def generate_thumbnail(self, image, upi):
move_to_start_position = None move_to_start_position = None
color = ColorHash(upi).hex color = ColorHash(upi).hex
points = [] points = []
width = []
height = []
for i, line in enumerate(image.split('\n')): for i, line in enumerate(image.split('\n')):
if i == 0: if not width:
width = re.findall(r'width="(\d+(\.\d+)?)"', line) width = re.findall(r'width="(\d+(\.\d+)?)"', line)
if not height:
height = re.findall(r'height="(\d+(\.\d+)?)"', line) height = re.findall(r'height="(\d+(\.\d+)?)"', line)
for nt in re.finditer('<text x="(\d+)(\.\d+)?" y="(\d+)(\.\d+)?".*?</text>', line): for nt in re.finditer('<text x="(\d+)(\.\d+)?" y="(\d+)(\.\d+)?".*?</text>', line):
if 'numbering-label' in nt.group(0): if 'numbering-label' in nt.group(0):
......
...@@ -16,6 +16,7 @@ import operator as op ...@@ -16,6 +16,7 @@ import operator as op
import itertools as it import itertools as it
from collections import Counter, defaultdict from collections import Counter, defaultdict
import re import re
import zlib
from caching.base import CachingMixin, CachingManager from caching.base import CachingMixin, CachingManager
from django.conf import settings from django.conf import settings
...@@ -541,6 +542,23 @@ class Rna(CachingMixin, models.Model): ...@@ -541,6 +542,23 @@ class Rna(CachingMixin, models.Model):
if not layout: if not layout:
return {} return {}
# added for release-16. Layout comes from the FTP
ftp = "http://ftp.ebi.ac.uk/pub/databases/RNAcentral/current_release/.secondary-structure/secondary-structure/{}.svg.gz"
upi = list(self.pk)
upi_path = "".join(upi[0:3]) + "/" \
+ "".join(upi[3:5]) + "/" \
+ "".join(upi[5:7]) + "/" \
+ "".join(upi[7:9]) + "/" \
+ "".join(upi[9:11]) + "/"
url = ftp.format(upi_path + "".join(upi))
try:
response = requests.get(url)
response.raise_for_status()
svg = zlib.decompress(response.content, zlib.MAX_WBITS | 32)
except requests.exceptions.HTTPError as e:
svg = None
# model_name = layout.template.model_name # model_name = layout.template.model_name
# if model_name.count('.') >= 2: # if model_name.count('.') >= 2:
# template_source = 'CRW' # template_source = 'CRW'
...@@ -557,7 +575,7 @@ class Rna(CachingMixin, models.Model): ...@@ -557,7 +575,7 @@ class Rna(CachingMixin, models.Model):
'secondary_structure': layout.secondary_structure, 'secondary_structure': layout.secondary_structure,
'source': layout.template.model_source, 'source': layout.template.model_source,
'model_id': layout.template.model_name, 'model_id': layout.template.model_name,
'layout': layout.layout, 'layout': svg if svg else layout.layout,
'template_species': layout.template.taxid.name, 'template_species': layout.template.taxid.name,
'template_lineage': layout.template.taxid.lineage, 'template_lineage': layout.template.taxid.lineage,
} }
......
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