<commit_before>/*
 *                    BioJava development code
 *
 * This code may be freely distributed and modified under the
 * terms of the GNU Lesser General Public Licence.  This should
 * be distributed with the code.  If you do not have a copy,
 * see:
 *
 *      http://www.gnu.org/copyleft/lesser.html
 *
 * Copyright for this code is held jointly by the individual
 * authors.  These should be listed in @author doc comments.
 *
 * For more information on the BioJava project and its aims,
 * or to join the biojava-l mailing list, visit the home page
 * at:
 *
 *      http://www.biojava.org/
 *
 * Created on Apr 7, 2010
 * Author: <NAME> 
 *
 */

package org.biojava.bio.structure.align.util;

import org.biojava.bio.structure.Atom;
import org.biojava.bio.structure.Calc;
import org.biojava.bio.structure.SVDSuperimposer;
import org.biojava.bio.structure.StructureException;
import org.biojava.bio.structure.align.model.AFPChain;
import org.biojava.bio.structure.jama.Matrix;

public class AFPChainScorer
{

	public static double getTMScore(AFPChain align, Atom[] ca1, Atom[] ca2) throws StructureException
	{
		if ( align.getNrEQR() == 0)
			return -1;

		// Create new arrays for the subset of atoms in the alignment.
		Atom[] ca1aligned = new Atom[align.getOptLength()];
		Atom[] ca2aligned = new Atom[align.getOptLength()];
		int pos=0;
		int[] blockLens = align.getOptLen();
		int[][][] optAln = align.getOptAln();
		assert(align.getBlockNum() <= optAln.length);
		
		for(int block=0;block< align.getBlockNum();block++) {
		
			if ( ! ( blockLens[block] <= optAln[block][0].length)) {
				System.err.println("AFPChainScorer getTMScore: errors reconstructing alignment block ["+ block +"]. Length is " + blockLens[block] + " but should be <=" + optAln[block][0].length );
			}

			for(int i=0;i<blockLens[block];i++) {
				Atom a1 = ca1[optAln[block][0][i]];
				Atom a2 = (Atom) ca2[optAln[block][1][i]].clone();
								
				ca1aligned[pos] = a1;
				ca2aligned[pos] = a2;
				pos++;
			}
		}
		
		// this can happen when we load an old XML serialization which did not support modern ChemComp representation of modified residues.		
		if ( pos != align.getOptLength()){
			System.err.println("AFPChainScorer getTMScore: Problems reconstructing alignment! nr of loaded atoms is " + pos + " but should be " + align.getOptLength());
			// we need to resize the array, because we allocated too many atoms earlier on.
			ca1aligned = (Atom[]) resizeArray(ca1aligned, pos);
			ca2aligned = (Atom[]) resizeArray(ca2aligned, pos);<commit_msg>doing things step by step for readability<commit_after>/*
 *                    BioJava development code
 *
 * This code may be freely distributed and modified under the
 * terms of the GNU Lesser General Public Licence.  This should
 * be distributed with the code.  If you do not have a copy,
 * see:
 *
 *      http://www.gnu.org/copyleft/lesser.html
 *
 * Copyright for this code is held jointly by the individual
 * authors.  These should be listed in @author doc comments.
 *
 * For more information on the BioJava project and its aims,
 * or to join the biojava-l mailing list, visit the home page
 * at:
 *
 *      http://www.biojava.org/
 *
 * Created on Apr 7, 2010
 * Author: <NAME> 
 *
 */

package org.biojava.bio.structure.align.util;



import org.biojava.bio.structure.Atom;
import org.biojava.bio.structure.Calc;
import org.biojava.bio.structure.SVDSuperimposer;
import org.biojava.bio.structure.StructureException;
import org.biojava.bio.structure.align.model.AFPChain;
import org.biojava.bio.structure.jama.Matrix;

public class AFPChainScorer
{
	
	
	public  static double getTMScore(AFPChain align, Atom[] ca1, Atom[] ca2) throws StructureException
	{
		if ( align.getNrEQR() == 0)
			return -1;

		
		// Create new arrays for the subset of atoms in the alignment.
		Atom[] ca1aligned = new Atom[align.getOptLength()];
		Atom[] ca2aligned = new Atom[align.getOptLength()];
		int pos=0;
		int[] blockLens = align.getOptLen();
		int[][][] optAln = align.getOptAln();
		assert(align.getBlockNum() <= optAln.length);
		
		for(int block=0;block< align.getBlockNum();block++) {
		
			if ( ! ( blockLens[block] <= optAln[block][0].length)) {
				System.err.println("AFPChainScorer getTMScore: errors reconstructing alignment block ["+ block +"]. Length is " + blockLens[block] + " but should be <=" + optAln[block][0].length );
			}

			for(int i=0;i<blockLens[block];i++) {
				int pos1 = optAln[block][0][i];
				int pos2 = optAln[block][1][i];
//				if ( pos1 >= ca1.length) {
//					System.err.println("getTMScore: pos1 " + pos1 + " > ca1.length " + ca1.length + " " + align.getName1() + ":" +align.getName2());
//					System.err.println(ca1[0].getGroup().getChain().getChainID());
//					System.err.println(ca1[0].getGroup().getChain().getParent().getPDBCode());
//					
//				}
//				if ( pos2 >= ca2.length) {
//					System.err.println("getTMScore: pos2 " + pos2 + " > ca2.length " + ca2.length + " " + align.getName2() + ":" + align.getName1());
//					System.err.println(ca2[0].getGroup().getChain().getChainID());
//					System.err.println(ca2[0].getGroup().getChain().getParent().getPDBCode());
//				}
				Atom a1 = ca1[pos1];
				Atom a2 = (Atom) ca2[pos2].clone();
								
				ca1aligned[pos] = a1;
				ca2aligned[pos] = a2;
				pos++;
			}
		}
		
		// this can happen when we load an old XML serialization which did not support modern ChemComp representation of modified residues.		
		if ( pos != align.getOptLength()){
			System.err.println("AFPChainScorer getTMScore: Problems reconstructing alignment! nr of loaded atoms is " + pos + " but should be " + align.getOptLength());
			// we need to resize the array, because we allocated too many atoms earlier on.
			ca1aligned = (Atom[]) resizeArray(ca1aligned, pos);
			ca2aligned = (Atom[]) resizeArray(ca2aligned, pos);
{'fraction_non_alphanumeric': 0.13900273224043716, 'fraction_numerical': 0.015027322404371584, 'mean_word_length': 3.5297757153905645, 'pattern_counts': {'":': 2, '<': 15, '<?xml version=': 0, '>': 9, 'https://': 0, 'lorem ipsum': 0, 'www.': 4, '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': '1797921', 'n_tokens_mistral': 2143, 'n_tokens_neox': 1912, 'n_words': 521}