<commit_before>package org.rcsb.mmtf.encoder;

import java.io.Serializable;
import java.util.Collections;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Map;
import java.util.Set;

import org.rcsb.mmtf.api.StructureDataInterface;
import org.rcsb.mmtf.decoder.DecoderUtils;
import org.rcsb.mmtf.encoder.AdapterToStructureData;
import org.rcsb.mmtf.encoder.EncoderUtils;

/**
 * Converts a full (all-atom) MMTF structure data representation to a reduced version.
 * The reduced version contains only the C-alpha atoms of polypeptide and the
 * P atom of polynucleotide chains. Alternative locations of the C-alpha and P atoms
 * are excluded, as well as any water molecules.
 * 
 * @author <NAME>
 * @author <NAME>
 *
 */
public class ReducedEncoder  implements Serializable {
	private static final long serialVersionUID = -528732924956087110L;
	
	private static final String CALPHA_NAME = "CA";
	private static final String CARBON_ELEMENT = "C";
	private static final String PHOSPHATE_NAME = "P";
	private static final String PHOSPHATE_ELEMENT = "P";

	/**
	 * Gets the reduced form of the input {@link StructureDataInterface}.
	 * @param full the input {@link StructureDataInterface} 
	 * @return the reduced form of the {@link StructureDataInterface} as another {@link StructureDataInterface}
	 */
	public static StructureDataInterface getReduced(StructureDataInterface full) {
		// maps atom indices in full structure to reduced structure
        Map<Integer, Integer> atomMap = new HashMap<>();
			
		// get indices to C-alpha and P atoms for polypeptides and polynucleotides, respectively
		Integer[] centerAtomIndices = getCenterAtomGroupIndices(full);
		
		// Set header and metadata
		AdapterToStructureData reduced = new AdapterToStructureData();
		
		reduced.setMmtfProducer(full.getMmtfProducer());
		SummaryData dataSummary = getDataSummaryData(full, centerAtomIndices);
		reduced.initStructure(dataSummary.numBonds, dataSummary.numAtoms, dataSummary.numGroups, 
				dataSummary.numChains, full.getNumModels(), full.getStructureId());
		
		DecoderUtils.addXtalographicInfo(full, reduced);
		DecoderUtils.addHeaderInfo(full, reduced);
		DecoderUtils.generateBioAssembly(full, reduced);		
		DecoderUtils.addEntityInfo(full, reduced);

		// traverse data structure and copy data to reduced representation.
		// Note, atomCount, groupCount, and chainCount keep track of the total number of atoms, groups, and chains.
		// They are required to index the data structure.
		
		for (int i = 0, atomCount = 0, groupCount = 0, chainCount = 0, reducedAtomCount = 0; i<full.getNumModels(); i++) {
			int numChains = full.getChainsPerModel()[i];
			reduced.setModelInfo(i, numChains);
			
			for (int j = 0; j < numChains; j++, chainCount++){
				String chainType = EncoderUtils.getTypeFromChainId(full, chainCount);
				int reducedGroupsPerChain = 0;

				for (int k = 0; k < full.getGroupsPerChain()[chainCount]; k++, groupCount++) {
					int groupType = full.getGroupTypeIndices()[groupCount];
					
					Set<Integer> atomIndicesToAdd = getIndicesToAdd(full, groupType, chainType, centerAtomIndices);
					int bondsToAdd = getNumIntraGroupBonds(atomIndicesToAdd, full, groupType, centerAtomIndices);
					
					if (atomIndicesToAdd.size() > 0) {
						
						// Set Group information
						reduced.setGroupInfo(full.getGroupName(groupType), full.getGroupIds()[groupCount], 
								full.getInsCodes()[groupCount], full.getGroupChemCompType(groupType), atomIndicesToAdd.size(),
								bondsToAdd, full.getGroupSingleLetterCode(groupType), full.getGroupSequenceIndices()[groupCount], <commit_msg>Corrected initialization of reducedAtomCount, needs to start at -1, not 0.<commit_after>package org.rcsb.mmtf.encoder;

import java.io.Serializable;
import java.util.Collections;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Map;
import java.util.Set;

import org.rcsb.mmtf.api.StructureDataInterface;
import org.rcsb.mmtf.decoder.DecoderUtils;
import org.rcsb.mmtf.encoder.AdapterToStructureData;
import org.rcsb.mmtf.encoder.EncoderUtils;

/**
 * Converts a full (all-atom) MMTF structure data representation to a reduced version.
 * The reduced version contains only the C-alpha atoms of polypeptide and the
 * P atom of polynucleotide chains. Alternative locations of the C-alpha and P atoms
 * are excluded, as well as any water molecules.
 * 
 * @author <NAME>
 * @author <NAME>
 *
 */
public class ReducedEncoder  implements Serializable {
	private static final long serialVersionUID = -528732924956087110L;
	
	private static final String CALPHA_NAME = "CA";
	private static final String CARBON_ELEMENT = "C";
	private static final String PHOSPHATE_NAME = "P";
	private static final String PHOSPHATE_ELEMENT = "P";

	/**
	 * Gets the reduced form of the input {@link StructureDataInterface}.
	 * @param full the input {@link StructureDataInterface} 
	 * @return the reduced form of the {@link StructureDataInterface} as another {@link StructureDataInterface}
	 */
	public static StructureDataInterface getReduced(StructureDataInterface full) {
		// maps atom indices in full structure to reduced structure
        Map<Integer, Integer> atomMap = new HashMap<>();
			
		// get indices to C-alpha and P atoms for polypeptides and polynucleotides, respectively
		Integer[] centerAtomIndices = getCenterAtomGroupIndices(full);
		
		// Set header and metadata
		AdapterToStructureData reduced = new AdapterToStructureData();
		
		reduced.setMmtfProducer(full.getMmtfProducer());
		SummaryData dataSummary = getDataSummaryData(full, centerAtomIndices);
		reduced.initStructure(dataSummary.numBonds, dataSummary.numAtoms, dataSummary.numGroups, 
				dataSummary.numChains, full.getNumModels(), full.getStructureId());
		
		DecoderUtils.addXtalographicInfo(full, reduced);
		DecoderUtils.addHeaderInfo(full, reduced);
		DecoderUtils.generateBioAssembly(full, reduced);		
		DecoderUtils.addEntityInfo(full, reduced);

		// traverse data structure and copy data to reduced representation.
		// Note, atomCount, groupCount, and chainCount keep track of the total number of atoms, groups, and chains.
		// They are required to index the data structure.
		
		for (int i = 0, atomCount = 0, groupCount = 0, chainCount = 0, reducedAtomCount = -1; i<full.getNumModels(); i++) {
			int numChains = full.getChainsPerModel()[i];
			reduced.setModelInfo(i, numChains);
			
			for (int j = 0; j < numChains; j++, chainCount++){
				String chainType = EncoderUtils.getTypeFromChainId(full, chainCount);
				int reducedGroupsPerChain = 0;

				for (int k = 0; k < full.getGroupsPerChain()[chainCount]; k++, groupCount++) {
					int groupType = full.getGroupTypeIndices()[groupCount];
					
					Set<Integer> atomIndicesToAdd = getIndicesToAdd(full, groupType, chainType, centerAtomIndices);
					int bondsToAdd = getNumIntraGroupBonds(atomIndicesToAdd, full, groupType, centerAtomIndices);
					
					if (atomIndicesToAdd.size() > 0) {
						
						// Set Group information
						reduced.setGroupInfo(full.getGroupName(groupType), full.getGroupIds()[groupCount], 
								full.getInsCodes()[groupCount], full.getGroupChemCompType(groupType), atomIndicesToAdd.size(),
								bondsToAdd, full.getGroupSingleLetterCode(groupType), full.getGroupSequenceIndices()[groupCount], 
{'fraction_non_alphanumeric': 0.09632776103041929, 'fraction_numerical': 0.007673335160317895, 'mean_word_length': 4.729199372056515, 'pattern_counts': {'":': 0, '<': 19, '<?xml version=': 0, '>': 15, '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': '3315886', 'n_tokens_mistral': 2399, 'n_tokens_neox': 2077, 'n_words': 606}