All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.actelion.research.chem.interactionstatistics.InteractionDistanceStatistics Maven / Gradle / Ivy

There is a newer version: 2024.11.2
Show newest version
package com.actelion.research.chem.interactionstatistics;

import com.actelion.research.util.FastSpline;
import com.actelion.research.util.SmoothingSplineInterpolator;

import java.io.*;
import java.net.URL;
import java.nio.charset.StandardCharsets;
import java.util.*;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.atomic.AtomicInteger;
import java.util.stream.Collectors;
import java.util.stream.IntStream;

public class InteractionDistanceStatistics {
	
	private static volatile InteractionDistanceStatistics instance = new InteractionDistanceStatistics(); //eager initialization
	private static final String BASE_PATH = "/resources/interactionstatistics/";
	private volatile Map pairPotentials;
	public static final double OCCURENCE_CUTOFF = 500;
	public static final double CUTOFF_RADIUS = 6.0;
	public static final double BIN_SIZE = 0.2;
	public static final double CHI = 1e-04;
	public static final double MAX = 10;
	private final Map interactionStatistics;

	
	public static InteractionDistanceStatistics getInstance() {

		if (instance==null) { 
			synchronized(InteractionDistanceStatistics.class) {
				if (instance==null) {
					instance = new InteractionDistanceStatistics();
				}
			}
		}
		return instance;
	}
	
	public int getInteractionClasses() {
		return pairPotentials.keySet().size();
	}

	public Map getPairPotentials() {
		return pairPotentials;
	}

	public List getAtomTypes(){
		HashSet hs = new HashSet<>();

		for (long pair : pairPotentials.keySet()) {
			int [] a = splitLongToInts(pair);
			hs.add(a[0]);
			hs.add(a[1]);
		}
		return new ArrayList<>(hs);
	}

	public Set getAtomKeySet() {
		Set atomKeySet = new HashSet();
		for(long l : pairPotentials.keySet()) {
			int[] pair = splitLongToInts(l);
			int a = getKey(pair[0]);
			int b = getKey(pair[1]);
			atomKeySet.add(a);
			atomKeySet.add(b);
		}
		return atomKeySet;
	}


	
	private synchronized void calculatePotentials() {
		splineCalculation();
	}
	
	private InteractionDistanceStatistics() {
		interactionStatistics = new ConcurrentHashMap();
		initialize();

	}
	
	private void initialize() {
	try {
		readFromFile();
	} catch (IOException e) {
		// TODO Auto-generated catch block
		e.printStackTrace();
	}
	calculatePotentials();
	}
	
	
	
	
	public long combineIntsToLong(int a, int b) {
		int a1;
		int a2;
		if(a> 32);
		int b = (int)l;
		
		return new int[] {a,b};
	}
	
	private boolean isGenericAtomPair(int a, int b) {
		return (isGenericAtomType(a) && isGenericAtomType(b));
	}
	
	private boolean isGenericAtomType(int a) {
		return Integer.toBinaryString(a).length()<=InteractionAtomTypeCalculator.AtomFlagCount.BASIC_ATOM_FLAG_COUNT.getCount();
	}
	
	//private boolean isExtendedAtomType(int a) {
	//	return Integer.toBinaryString(a).length()<=InteractionAtomTypeCalculator.AtomFlagCount.EXTENDED_ATOM_FLAG_COUNT.getCount();
	//}
	
	private boolean isSpecificAtomType(int a) {
		return Integer.toBinaryString(a).length()<=InteractionAtomTypeCalculator.AtomFlagCount.FUNC_GROUP_FLAG_COUNT.getCount();
	}
	
	public int getKey(int atomType)  {
		if (isSpecificAtomType(atomType)) {
			return (atomType & InteractionAtomTypeCalculator.AtomPropertyMask.SPECIFIC.getMask());
		}
		else return atomType;
	}
	
	private boolean isGenericAtomPair(long l) {
		int[] pair = splitLongToInts(l);
		return isGenericAtomPair(pair[0], pair[1]);
	}

 	
	public void addInteraction(int atom1, int atom2, double dist) {
		if (dist>=CUTOFF_RADIUS) return;
		//InteractionAtomPair ap =new InteractionAtomPair(atom1,atom2);
		long l = combineIntsToLong(atom1,atom2);
		interactionStatistics.putIfAbsent(l, new int[(int)(CUTOFF_RADIUS/BIN_SIZE)]);
		int index = (int) (0.5+dist/BIN_SIZE);
		int[] occurences = interactionStatistics.get(l);
		if(index();

			double[] referenceSum = new double[(int)(CUTOFF_RADIUS/BIN_SIZE)];


			
			
			interactionStatistics.entrySet().stream().forEach(e -> {
				SplineFunction potential = new SplineFunction();
				potential.setOccurencesArray(e.getValue());
				pairPotentials.putIfAbsent(e.getKey(), potential);
			});

			
			Map discreteFunctions = interactionStatistics.entrySet().stream()
				    .collect(Collectors.toMap(e -> e.getKey(), e -> distanceNormalization(e.getValue()))); //check this line
			
			AtomicInteger runCount = new AtomicInteger(0);
			discreteFunctions.entrySet().stream().filter(e -> isGenericAtomPair(e.getKey())).
			forEach(statistics -> { 
				runCount.getAndIncrement();
				IntStream.range(0,statistics.getValue().length).forEach(i -> {
					referenceSum[i]+=statistics.getValue()[i];
				}
				);
				
			});
			for(int i=0;i normalize(v,referenceSum));
			double[] X = new double[referenceSum.length];
			IntStream.range(0, X.length).forEach(i-> X[i]= (i+0.5)*BIN_SIZE);
			//System.out.println(Arrays.toString(X));
			for(long l : discreteFunctions.keySet()) {
				
				double[] sigma = new double[X.length];
				Arrays.fill(sigma, 1);

				//
				//  Smoothing Spline
				//
				SmoothingSplineInterpolator interpolator = new SmoothingSplineInterpolator();
				interpolator.setLambda(0.005);
				interpolator.setSigma(sigma);

				FastSpline ff = interpolator.interpolate(X, discreteFunctions.get(l));
				
				double[] Y = new double[X.length];
				for(int i=0;iY[i] && Y[i+1]>Y[i]) { //minimum
						if(globalMinIndex==-1) globalMinIndex = i;
						else if(Y[i]=0 && firstMaxIndex=0) {
						Y[i] = Y[i+1]+derivative*BIN_SIZE;
						i--;
					}
					ff = interpolator.interpolate(X, Y);
				}
				
				SplineFunction potential = pairPotentials.get(l);
				potential.setSplineFunction(ff);
				potential.setDiscreteFunction(discreteFunctions.get(l));

			}
		}
					


		
		private double[] normalize(double[] arr, double[] reference) {
			IntStream.range(0,arr.length).forEach(i -> arr[i]=-Math.log((arr[i] + CHI) / (reference[i] + CHI)));
			return arr;
		}
		
		private double[] distanceNormalization(int[] Y) {
			double[] YNorm = new double[Y.length];
			double normalizedSum = 0;
			for(int index=0; indexOCCURENCE_CUTOFF) return spline;
			int a2 =  a & InteractionAtomTypeCalculator.AtomPropertyMask.EXTENDED.getMask();
			int b2 =  b & InteractionAtomTypeCalculator.AtomPropertyMask.EXTENDED.getMask();
			l = combineIntsToLong(a1,b2);

			spline = pairPotentials.get(l);
			if(spline!=null && spline.getTotalOccurences()>OCCURENCE_CUTOFF ) return spline;
			l = combineIntsToLong(a2,b1);

			spline = pairPotentials.get(l);
			if(spline!=null && spline.getTotalOccurences()>OCCURENCE_CUTOFF) return spline;
			l = combineIntsToLong(a2,b2);
			spline = pairPotentials.get(l);

			if(spline!=null && spline.getTotalOccurences()>OCCURENCE_CUTOFF) return spline;
			
			int a3 =  a & InteractionAtomTypeCalculator.AtomPropertyMask.BASIC.getMask();
			int b3 =  b & InteractionAtomTypeCalculator.AtomPropertyMask.BASIC.getMask();
			
			l = combineIntsToLong(a3,b1);
			spline = pairPotentials.get(l);

			if(spline!=null && spline.getTotalOccurences()>OCCURENCE_CUTOFF) return spline;
			l = combineIntsToLong(a1,b3);
			spline = pairPotentials.get(l);

			if(spline!=null && spline.getTotalOccurences()>OCCURENCE_CUTOFF) return spline;
			l = combineIntsToLong(a3,b2);
			spline = pairPotentials.get(l);

			if(spline!=null && spline.getTotalOccurences()>OCCURENCE_CUTOFF) return spline;
			l = combineIntsToLong(a2,b3);
			spline = pairPotentials.get(l);

			if(spline!=null && spline.getTotalOccurences()>OCCURENCE_CUTOFF) return spline;
			
			l = combineIntsToLong(a3,b3);

			spline = pairPotentials.get(l);
			return spline;
			

		}
	
			
			
	
	
	
	
	

	
	
	

}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy