from adams import db_maker
from adams import matcher
adams/__init__.py
young55775/ADAMS-developing
"""
Direct-extract-from-SAM-file
    Copyright (C) 2023  Guo Zhengyang

    This program is free software: you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation, either version 3 of the License, or
    (at your option) any later version.

    This program is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

    You should have received a copy of the GNU General Public License
    along with this program.  If not, see <http://www.gnu.org/licenses/>.
"""
from tool_kit import *
import os
from scipy.spatial import distance_matrix
import cupy as cp

def db_maker(pdb_folder, out, mode='compatible'):
    files = os.listdir(pdb_folder)
    data_dict = {}
    if mode == 'compatible':
        data_dict['mode'] = 'compatible'
    if mode == 'cuda':
        data_dict['mode'] = 'cuda'
    for i in files:
        name_lst = []
        length_lst = []
        feature_lst = []
        if i[:-3] == 'pdb':
            try:
                ca = get_coordinate(os.path.join(pdb_folder, i), 'X')
                dist = distance_matrix(ca, ca)
                dist = extract_features(dist)
                dist = norm(dist)
                if mode == 'compatible':
                    data_dict[i[:-4]] = dist
                if mode == 'cuda':
                    data_dict[i[:-4]] = cp.asarray(dist)
                name_lst.append(i[:-4])
                length_lst.append(len(ca))
                feature_lst.append(dist.shape[0])
            except:
                raise  ValueError(f'{i} can not be parsed')
    with open(out, 'wb') as f:
        pkl.dump(data_dict, f)
    with open(out + '_report.csv','w') as f:
        f.write('name,aa_length,feature_length\n')
        for i in range(len(name_lst)):
            f.write(f"{name_lst[i]},{length_lst[i]},{feature_lst[i]}\n")


adams/db_maker.py
young55775/ADAMS-developing
"""
Direct-extract-from-SAM-file
    Copyright (C) 2023
    2q3

    This program is free software: you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation, either version 3 of the License, or
    (at your option) any later version.

    This program is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

    You should have received a copy of the GNU General Public License
    along with this program.  If not, see <http://www.gnu.org/licenses/>.
"""

from tool_kit import *
import cupy as cp
from cupyx.scipy.spatial import distance_matrix
from scipy.spatial import distance_matrix as dm
import numpy as np
from functools import partial
from tqdm.contrib.concurrent import process_map
from tqdm import tqdm

def compare(des, t, data):
    # try:
    #dt = cp.asarray(data[1])
    dist = cp.dot(des, data[1].T)
    n = cp.sum(cp.sum(dist > float(t), axis=1) > 0)
    return f"{data[0]},{n}\n"

def compare_c(des, t, data):
    # try:
    #dt = cp.asarray(data[1])
    dist = np.dot(des, data[1].T)
    n = np.sum(np.sum(dist > float(t), axis=1) > 0)
    return f"{data[0]},{n}\n"

def matcher(db, ref, out, t=0.95, core=1, chunk=100):
    d = open(db, 'rb')
    data = pkl.load(d)
    d.close()
    data = list(data.items())
    ca = get_coordinate(ref, 'X')
    mat = dm(ca, ca)
    content = []
    mat = cv2.normalize(mat, None, 0, 255, cv2.NORM_MINMAX).astype('uint8')
    des = extract_features(mat)
    des = norm(des)
    if data['mode'] == 'cuda':
        des = cp.asarray(des)
        compare_partial = partial(compare, des, t)
        for i in tqdm(data):
            if i[0] != 'mode'
            content.append(compare_partial(i))
        #    content = process_map(compare_partial,data,chunksize = chunk, max_workers = core)
        with open(out, 'w+') as f:
            f.write('protein,match\n')
            for i in content:
                try:
                    f.write(i)
                except:
                    continue
    else:
        raise Warning('CPU is super slow in current code')
        compare_partial = partial(compare_c, des, t)
        data = [n for n in data if n[0] != 'mode']
        for i in tqdm(data):
            content = process_map(compare_partial,data,chunksize = chunk, max_workers = core)
        with open(out, 'w+') as f:
            f.write('protein,match\n')
            for i in content:
                try:
                    f.write(i)
                except:
                    continue


adams/matcher.py
young55775/ADAMS-developing
"""
Direct-extract-from-SAM-file
    Copyright (C) 2023  Guo Zhengyang

    This program is free software: you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation, either version 3 of the License, or
    (at your option) any later version.

    This program is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

    You should have received a copy of the GNU General Public License
    along with this program.  If not, see <http://www.gnu.org/licenses/>.
"""

import numpy as np
import cupy as cp
from cupyx.scipy.spatial import distance_matrix as dm
from scipy.spatial import distance_matrix
from Bio.PDB import *
import cv2
import os
from tqdm import tqdm
import pickle as pkl

def get_coordinate(pdb_path, name):
    p = PDBParser()
    st = p.get_structure(name, pdb_path)
    ca = np.array([atom.coord for atom in st.get_atoms() if atom.name == 'CA'])
    return ca

def extract_features(mat):
    mat = cv2.normalize(mat, None, 0, 255, cv2.NORM_MINMAX).astype('uint8')
    sift = cv2.SIFT_create()
    kp, des = sift.detectAndCompute(mat, None)
    return des

def norm(x):
    x = x.T
    x1 = np.linalg.norm(x=x, ord=2, axis=0, keepdims=True)
    x = x / x1
    return x.T

adams/tool_kit.py
young55775/ADAMS-developing
import setuptools
install = ['numpy >= 1.17.2','opencv-python >= 4.7.0.12','scipy == 1.11.2','tqdm == 4.66.1','biopython == 1.81']
setup = ['numpy','scipy','opencv-python','tqdm','pickle','biopython']


import setuptools
setuptools.setup(
    name='dia',
    version='1.0',
    description='this is a program for dia',
    author='Guo Zhengyang',
    author_email='guozy23@mails.tsinghua.edu.cn',
	setup_requires=setup,
	install_requires=install,
    packages=setuptools.find_packages(),
   )

setup.py
young55775/ADAMS-developing