<commit_before>/*
 * 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 26.04.2004
 * @author <NAME>
 *
 */
package org.biojava.bio.structure.io;

import org.biojava.bio.structure.* ;

import java.util.ArrayList ;
import java.util.HashMap ;
import java.text.DecimalFormat;

// xml writer ...
import org.biojava.utils.xml.*;
import java.io.IOException ;

/** Methods to convert a structure object into different file formats.
 * @author <NAME>
 * @since 1.4
 */
public class FileConvert {
    Structure structure ;

    //static String DEFAULTCHAIN = "_" ;
    /**
     * Constructs a FileConvert object.
     *
     * @param struc  a Structure object
     */
    public FileConvert(Structure struc) {
	structure = struc ;
    }

    /** align a string to the right
     * length is the total length the new string should take, inlcuding spaces on the left
     * incredible that this tool is missing in java !!!
     */
    private String alignRight(String input, int length){

	String spaces = "                           " ;
	int n = input.length();
	int diff = length - n ;
	String s = "";

	if (n < length) {
	    s = spaces.substring(0,diff) + input;
	} else {
	    // does not work
	    return input ;
	}
	return s;
    }


    /** Convert a structure into a PDB file.
     * @return a String representing a PDB file.
     */
    public String toPDB() {
	

	StringBuffer str = new StringBuffer();
	int i = 0 ;
	
	DecimalFormat d3 = new DecimalFormat("0.000");
	DecimalFormat d2 = new DecimalFormat("0.00");

	// do for all models
	int nrModels = structure.nrModels() ;
	if ( structure.isNmr()) {
	    str.append("EXPDTA    NMR, "+ nrModels+" STRUCTURES\n") ;
	}
	for (int m = 0 ; m < nrModels ; m++) {
	    ArrayList model = (ArrayList)structure.getModel(m);
	    // todo support NMR structures ...
	    if ( structure.isNmr()) {
		str.append("MODEL      " + (m+1)+"\n");
	    }
	    // do for all chains
	    int nrChains = model.size();
	    for ( int c =0; c<nrChains;c++) {
		Chain  chain   = (Chain)model.get(c);
		String chainID = chain.getName();
		//if ( chainID.equals(DEFAULTCHAIN) ) chainID = " ";
		// do for all groups
		int nrGroups = chain.getLength();
		for ( int h=0; h<nrGroups;h++){

		    Group g= chain.getGroup(h);
		    String type = g.getType() ;

		    String record = "" ;
		    if ( type.equals("hetatm") ) {
			record = "HETATM";
		
		    } else {
			record = "ATOM  ";
		    }
		   
		    
		    // format output ...
		    int groupsize  = g.size();
		    String resName = g.getPDBName();
		    String pdbcode = g.getPDBCode();
		    String line    = "" ;

		    // iteratate over all atoms ...
		    for ( int atompos = 0 ; atompos < groupsize; atompos++) {
			Atom a = null ;
			try {
			    a = g.getAtom(atompos);
			} catch ( StructureException e) {
			    System.err.println(e);
			    continue ;
			}

			int    seri       = a.getPDBserial()        ;
			String serial     = alignRight(""+seri,5)   ;<commit_msg>fixed bug related to Locale and DecimalFormat<commit_after>/*
 * 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 26.04.2004
 * @author <NAME>
 *
 */
package org.biojava.bio.structure.io;

import org.biojava.bio.structure.*  ;

import java.util.ArrayList          ;
import java.util.HashMap            ;
import java.text.DecimalFormat      ;

// xml writer ...
import org.biojava.utils.xml.*      ;
import java.io.IOException          ;

// for formatting on numbers
import java.util.Locale             ;
import java.text.NumberFormat       ;

/** Methods to convert a structure object into different file formats.
 * @author <NAME>
 * @since 1.4
 */
public class FileConvert {
    Structure structure ;

    //static String DEFAULTCHAIN = "_" ;
    /**
     * Constructs a FileConvert object.
     *
     * @param struc  a Structure object
     */
    public FileConvert(Structure struc) {
	structure = struc ;
    }

    /** align a string to the right
     * length is the total length the new string should take, inlcuding spaces on the left
     * incredible that this tool is missing in java !!!
     */
    private String alignRight(String input, int length){

	String spaces = "                           " ;
	int n = input.length();
	int diff = length - n ;
	String s = "";

	if (n < length) {
	    s = spaces.substring(0,diff) + input;
	} else {
	    // does not work
	    return input ;
	}
	return s;
    }


    /** Convert a structure into a PDB file.
     * @return a String representing a PDB file.
     */
    public String toPDB() {
	

	StringBuffer str = new StringBuffer();
	int i = 0 ;
	
	// Locale should be english, e.g. in DE separator is "," -> PDB files have "." !
	DecimalFormat d3 = (DecimalFormat)NumberFormat.getInstance(java.util.Locale.UK);
	d3.setMaximumIntegerDigits(3);	
	d3.setMinimumFractionDigits(3);
	d3.setMaximumFractionDigits(3);

	DecimalFormat d2 = (DecimalFormat)NumberFormat.getInstance(java.util.Locale.UK);
	d2.setMaximumIntegerDigits(3);	
	d3.setMinimumFractionDigits(3);
	d3.setMaximumFractionDigits(3);


	// do for all models
	int nrModels = structure.nrModels() ;
	if ( structure.isNmr()) {
	    str.append("EXPDTA    NMR, "+ nrModels+" STRUCTURES\n") ;
	}
	for (int m = 0 ; m < nrModels ; m++) {
	    ArrayList model = (ArrayList)structure.getModel(m);
	    // todo support NMR structures ...
	    if ( structure.isNmr()) {
		str.append("MODEL      " + (m+1)+"\n");
	    }
	    // do for all chains
	    int nrChains = model.size();
	    for ( int c =0; c<nrChains;c++) {
		Chain  chain   = (Chain)model.get(c);
		String chainID = chain.getName();
		//if ( chainID.equals(DEFAULTCHAIN) ) chainID = " ";
		// do for all groups
		int nrGroups = chain.getLength();
		for ( int h=0; h<nrGroups;h++){
		    
		    Group g= chain.getGroup(h);
		    String type = g.getType() ;

		    String record = "" ;
		    if ( type.equals("hetatm") ) {
			record = "HETATM";
		    } else {
			record = "ATOM  ";
		    }
		   
		    
		    // format output ...
		    int groupsize  = g.size();
		    String resName = g.getPDBName();
		    String pdbcode = g.getPDBCode();
		    String line    = "" ;

		    // iteratate over all atoms ...
		    for ( int atompos = 0 ; atompos < groupsize; atompos++) {
			Atom a = null ;
			try {
			    a = g.getAtom(atompos);
			} catch ( StructureException e) {
			    System.err.println(e);
			    continue ;
			}

			int    seri       = a.getPDBserial()        ;
			String serial     = alignRight(""+seri,5)   ;
{'fraction_non_alphanumeric': 0.12385814078452445, 'fraction_numerical': 0.007925846319183236, 'mean_word_length': 2.3415619389587072, 'pattern_counts': {'":': 0, '<': 17, '<?xml version=': 0, '>': 8, 'https://': 0, 'lorem ipsum': 0, 'www.': 4, 'xml': 4}, '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': '4885834', 'n_tokens_mistral': 2563, 'n_tokens_neox': 2331, 'n_words': 720}