<commit_before>from django.shortcuts import render
from django.http import HttpResponse
from django.db.models import Min, Count, Max
from django.conf import settings
from django.views.decorators.cache import cache_page
from django import forms

from construct.models import *
from structure.models import Structure
from protein.models import ProteinConformation, Protein, ProteinSegment, ProteinFamily
from alignment.models import AlignmentConsensus
from common.definitions import AMINO_ACIDS, AMINO_ACID_GROUPS, STRUCTURAL_RULES, STRUCTURAL_SWITCHES

import json
from collections import OrderedDict
import re
import xlrd
import yaml
import os
import time
import pickle

Alignment = getattr(__import__('common.alignment_' + settings.SITE_NAME, fromlist=['Alignment']), 'Alignment')

class FileUploadForm(forms.Form):
    file_source = forms.FileField()

def parse_excel(path):
    workbook = xlrd.open_workbook(path)
    worksheets = workbook.sheet_names()
    d = {}
    for worksheet_name in worksheets:
        if worksheet_name in d:
            print('Error, worksheet with this name already loaded')
            continue
        d[worksheet_name] = {}
        #d[worksheet_name] = OrderedDict()
        worksheet = workbook.sheet_by_name(worksheet_name)

        num_rows = worksheet.nrows - 1
        num_cells = worksheet.ncols
        curr_row = 0 #skip first, otherwise -1

        headers = []
        for i in range(num_cells):
            h = worksheet.cell_value(0, i)
            if h=="":
                h = "i_"+str(i)
            if h in headers:
                h += "_"+str(i)
            headers.append(worksheet.cell_value(0, i))
        for curr_row in range(1,num_rows+1):
            row = worksheet.row(curr_row)
            key = worksheet.cell_value(curr_row, 0)

            if key=='':
                continue
            if key not in d[worksheet_name]:
                d[worksheet_name][key] = []
            temprow = OrderedDict()
            for curr_cell in range(num_cells):
                cell_value = worksheet.cell_value(curr_row, curr_cell)
                if headers[curr_cell] not in temprow:
                    temprow[headers[curr_cell]] = cell_value
            d[worksheet_name][key].append(temprow)
    return d

def compare_family_slug(a,b):
    a = a.split("_")
    b = b.split("_")

    if a[0]!=b[0]:
        if a[0] == '001':
            # class A doesnt borrow from other classes
            return -1, 'ignore'
        elif a[0] == '002':
            # if Class B1 only use if B2
            if b[0]!= '003':
                return -1, 'ignore'
        elif a[0] == '003':
            # if Class B1 only use if B2
            if b[0]!= '002':
                return -1, 'ignore'
        elif a[0] == '004':
            # if Class C ignore others
            return -1, 'ignore'
        elif a[0] == '005':
            # if Class F ignore others
            return -1, 'ignore'
        elif a[0] == '006':
            # if Class Taste take for A
            if b[0]!= '001':
                return -1, 'ignore'
        return 0,"Different Class"
    elif a[1]!=b[1]:
        return 1,"Class"
    elif a[2]!=b[2]:
        # return 2,"Ligand Type" Ignore Ligand Type level for construct Design purposes.
        return 1,"Class"
    elif a[3]!=b[3]:
        return 3,"Receptor Family"
    else:
        return 4,"Receptor"

def new_tool(request):

    simple_selection = request.session.get('selection', False)
    proteins = []
    for target in simple_selection.targets:
        if target.type == 'protein':
            proteins.append(target.item)
    context = {}

    context['target'] = proteins[0]

    level = proteins[0].family.slug
    if level.split("_")[0]=='001':
        c_level = 'A'
    elif level.split("_")[0]=='002':
        c_level = 'B'
    elif level.split("_")[0]=='003':
        c_level = 'B'
    elif level.split("_")[0]=='004':
        c_level = 'C'
    elif level.split("_")[0]=='005':
        c_level = 'F'
    else:
        c_level = ''

    states = list(Structure.objects.filter(protein_conformation__protein__family__slug__startswith=level.split("_")[0]).all().values_list('state__slug', flat = True).distinct())
    if 'active' in states:
        active_xtals = True
    else:
        active_xtals = False


    rs = Residue.objects.filter(protein_conformation__protein=proteins[0]).prefetch_related('protein_segment','display_generic_number','generic_number')

    residues = {}
    residues_gn = {}
    residues_pos = {}
    for r in rs:
        segment = r.protein_segment.slug
        segment = segment.replace("-","")
        if segment not in residues:
            residues[segment] = []
        residues[segment].append(r)
        label = ''
        if r.generic_number:
            residues_gn[r.generic_number.label] = r
            label = r.display_generic_number.label

        residues_pos[r.sequence_number] = [r.amino_acid,r.protein_segment.slug,label]


    cons = Construct.objects.all().prefetch_related('crystal', 'protein__family','deletions','structure__state','insertions__insert_type')
    
    inserts = {}
    inserts['fusions'] = []
    inserts['other'] = {}
    for ins in ConstructInsertionType.objects.all().order_by('name','subtype'):
       # print(ins.name,ins.subtype,ins.sequence)
        if ins.name == 'fusion':
            inserts['fusions'].append(ins.subtype)
        else:
            if ins.name not in inserts['other']:
                inserts['other'][ins.name] = []
            if ins.subtype not in inserts['other'][ins.name]:
                inserts['other'][ins.name].append(ins.subtype)
        # fusion, f_results = c.fusion()
        # if fusion:
        #     f_protein = f_results[0][2]
        #     if f_protein not in inserts['fusions']:
        #         inserts['fusions'].append(f_protein)
        # else:
        #     for ins in c.insertions.all():
        #         print(ins)
    context['ICL_max'] = {'ICL2': residues['ICL2'][-1].sequence_number, 'ICL3': residues['ICL3'][-1].sequence_number}
    context['ICL_min'] = {'ICL2': residues['ICL2'][0].sequence_number,'ICL3': residues['ICL3'][0].sequence_number}
    context['residues'] = residues
    context['residues_gn'] = residues_gn
    context['residues_pos'] = residues_pos
    context['class'] = c_level
    context['active_xtals'] = active_xtals
    context['inserts'] = inserts
    context['form'] = FileUploadForm


    context['signal_p'] = None
    with open('construct/signal_p.txt', "r", encoding='UTF-8') as signal_p:
            for row in signal_p:
                r = row.split()
                if r[0]==proteins[0].entry_name:<commit_msg>Fix path to signal_p.txt for construct design tool<commit_after>from django.shortcuts import render
from django.http import HttpResponse
from django.db.models import Min, Count, Max
from django.conf import settings
from django.views.decorators.cache import cache_page
from django import forms

from construct.models import *
from structure.models import Structure
from protein.models import ProteinConformation, Protein, ProteinSegment, ProteinFamily
from alignment.models import AlignmentConsensus
from common.definitions import AMINO_ACIDS, AMINO_ACID_GROUPS, STRUCTURAL_RULES, STRUCTURAL_SWITCHES

import json
from collections import OrderedDict
import re
import xlrd
import yaml
import os
import time
import pickle

Alignment = getattr(__import__('common.alignment_' + settings.SITE_NAME, fromlist=['Alignment']), 'Alignment')

class FileUploadForm(forms.Form):
    file_source = forms.FileField()

def parse_excel(path):
    workbook = xlrd.open_workbook(path)
    worksheets = workbook.sheet_names()
    d = {}
    for worksheet_name in worksheets:
        if worksheet_name in d:
            print('Error, worksheet with this name already loaded')
            continue
        d[worksheet_name] = {}
        #d[worksheet_name] = OrderedDict()
        worksheet = workbook.sheet_by_name(worksheet_name)

        num_rows = worksheet.nrows - 1
        num_cells = worksheet.ncols
        curr_row = 0 #skip first, otherwise -1

        headers = []
        for i in range(num_cells):
            h = worksheet.cell_value(0, i)
            if h=="":
                h = "i_"+str(i)
            if h in headers:
                h += "_"+str(i)
            headers.append(worksheet.cell_value(0, i))
        for curr_row in range(1,num_rows+1):
            row = worksheet.row(curr_row)
            key = worksheet.cell_value(curr_row, 0)

            if key=='':
                continue
            if key not in d[worksheet_name]:
                d[worksheet_name][key] = []
            temprow = OrderedDict()
            for curr_cell in range(num_cells):
                cell_value = worksheet.cell_value(curr_row, curr_cell)
                if headers[curr_cell] not in temprow:
                    temprow[headers[curr_cell]] = cell_value
            d[worksheet_name][key].append(temprow)
    return d

def compare_family_slug(a,b):
    a = a.split("_")
    b = b.split("_")

    if a[0]!=b[0]:
        if a[0] == '001':
            # class A doesnt borrow from other classes
            return -1, 'ignore'
        elif a[0] == '002':
            # if Class B1 only use if B2
            if b[0]!= '003':
                return -1, 'ignore'
        elif a[0] == '003':
            # if Class B1 only use if B2
            if b[0]!= '002':
                return -1, 'ignore'
        elif a[0] == '004':
            # if Class C ignore others
            return -1, 'ignore'
        elif a[0] == '005':
            # if Class F ignore others
            return -1, 'ignore'
        elif a[0] == '006':
            # if Class Taste take for A
            if b[0]!= '001':
                return -1, 'ignore'
        return 0,"Different Class"
    elif a[1]!=b[1]:
        return 1,"Class"
    elif a[2]!=b[2]:
        # return 2,"Ligand Type" Ignore Ligand Type level for construct Design purposes.
        return 1,"Class"
    elif a[3]!=b[3]:
        return 3,"Receptor Family"
    else:
        return 4,"Receptor"

def new_tool(request):

    simple_selection = request.session.get('selection', False)
    proteins = []
    for target in simple_selection.targets:
        if target.type == 'protein':
            proteins.append(target.item)
    context = {}

    context['target'] = proteins[0]

    level = proteins[0].family.slug
    if level.split("_")[0]=='001':
        c_level = 'A'
    elif level.split("_")[0]=='002':
        c_level = 'B'
    elif level.split("_")[0]=='003':
        c_level = 'B'
    elif level.split("_")[0]=='004':
        c_level = 'C'
    elif level.split("_")[0]=='005':
        c_level = 'F'
    else:
        c_level = ''

    states = list(Structure.objects.filter(protein_conformation__protein__family__slug__startswith=level.split("_")[0]).all().values_list('state__slug', flat = True).distinct())
    if 'active' in states:
        active_xtals = True
    else:
        active_xtals = False


    rs = Residue.objects.filter(protein_conformation__protein=proteins[0]).prefetch_related('protein_segment','display_generic_number','generic_number')

    residues = {}
    residues_gn = {}
    residues_pos = {}
    for r in rs:
        segment = r.protein_segment.slug
        segment = segment.replace("-","")
        if segment not in residues:
            residues[segment] = []
        residues[segment].append(r)
        label = ''
        if r.generic_number:
            residues_gn[r.generic_number.label] = r
            label = r.display_generic_number.label

        residues_pos[r.sequence_number] = [r.amino_acid,r.protein_segment.slug,label]


    cons = Construct.objects.all().prefetch_related('crystal', 'protein__family','deletions','structure__state','insertions__insert_type')
    
    inserts = {}
    inserts['fusions'] = []
    inserts['other'] = {}
    for ins in ConstructInsertionType.objects.all().order_by('name','subtype'):
       # print(ins.name,ins.subtype,ins.sequence)
        if ins.name == 'fusion':
            inserts['fusions'].append(ins.subtype)
        else:
            if ins.name not in inserts['other']:
                inserts['other'][ins.name] = []
            if ins.subtype not in inserts['other'][ins.name]:
                inserts['other'][ins.name].append(ins.subtype)
        # fusion, f_results = c.fusion()
        # if fusion:
        #     f_protein = f_results[0][2]
        #     if f_protein not in inserts['fusions']:
        #         inserts['fusions'].append(f_protein)
        # else:
        #     for ins in c.insertions.all():
        #         print(ins)
    context['ICL_max'] = {'ICL2': residues['ICL2'][-1].sequence_number, 'ICL3': residues['ICL3'][-1].sequence_number}
    context['ICL_min'] = {'ICL2': residues['ICL2'][0].sequence_number,'ICL3': residues['ICL3'][0].sequence_number}
    context['residues'] = residues
    context['residues_gn'] = residues_gn
    context['residues_pos'] = residues_pos
    context['class'] = c_level
    context['active_xtals'] = active_xtals
    context['inserts'] = inserts
    context['form'] = FileUploadForm


    context['signal_p'] = None
    path_to_signal_p = os.sep.join([settings.BASE_DIR, "construct","signal_p.txt"])
    with open(path_to_signal_p, "r", encoding='UTF-8') as signal_p:
            for row in signal_p:
                r = row.split()
                if r[0]==proteins[0].entry_name:
{'fraction_non_alphanumeric': 0.1281000968198406, 'fraction_numerical': 0.01623594250391003, 'mean_word_length': 2.428133775848864, 'pattern_counts': {'":': 2, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '574375', 'n_tokens_mistral': 4379, 'n_tokens_neox': 4150, 'n_words': 935}