<commit_before>package org.biojava.bio.structure.io.mmcif;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileOutputStream;
import java.io.FilenameFilter;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.io.StringWriter;
import java.net.HttpURLConnection;
import java.net.URL;
import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.atomic.AtomicBoolean;
import java.util.zip.GZIPOutputStream;


import org.biojava.bio.structure.align.ce.AbstractUserArgumentProcessor;
import org.biojava.bio.structure.align.util.HTTPConnectionTools;
import org.biojava.bio.structure.io.mmcif.model.ChemComp;
import org.biojava3.core.util.InputStreamProvider;



/** This provider of chemical components can download and cache chemical component definition files from the RCSB PDB web site.
 *  It is the default way to access these definitions.
 *  If this provider is called he first time, it will download and install all chemical 
 *  component definitions in a local directory. 
 *  Once the definition files have been installed, it has quick startup time and low memory requirements. 
 *
 *  An alternative provider, that keeps all definitions in memory is the {@link AllChemCompProvider}. Another provider, that
 *  does not require any network access, but only can support a limited set of chemical component definitions, is the {@link ReducedChemCompProvider}. 
 *  
 * 
 * @author <NAME>
 *
 */
public class DownloadChemCompProvider implements ChemCompProvider {

	static String path; 
	private static final String FILE_SEPARATOR = System.getProperty("file.separator");
	static final String NEWLINE = System.getProperty("line.separator");

	public static String CHEM_COMP_CACHE_DIRECTORY = "chemcomp";

	private static String serverLocation = "http://www.rcsb.org/pdb/files/ligand/";

	// flags to make sure there is only one thread running that is loading the dictionary
	static AtomicBoolean loading = new AtomicBoolean(false);

	static final List<String> protectedIDs = new ArrayList<String> ();
	static {
		protectedIDs.add("CON");
		protectedIDs.add("PRN");
		protectedIDs.add("AUX");
		protectedIDs.add("NUL");
	}
	
	/** by default we will download only some of the files. User has to request that all files should be downloaded...
	 * 
	 */
	boolean downloadAll = false;

	public DownloadChemCompProvider(){
		//System.out.println("USING DOWNLOAD CHEM COMP PROVIDER");		
	}

	/** checks if the chemical components already have been installed into the PDB directory.
	 *  If not, will download the chemical components definitions file and split it up into small
	 *  subfiles.
	 */
	public void checkDoFirstInstall(){

		if ( ! downloadAll ) {
			return;
		}
		
		if ( path == null)
			path =  System.getProperty(AbstractUserArgumentProcessor.CACHE_DIR);
		if (path == null || path.equals(""))
			path = System.getProperty(AbstractUserArgumentProcessor.PDB_DIR);
		
		String filename = path + 	
		DownloadChemCompProvider.CHEM_COMP_CACHE_DIRECTORY +
		FILE_SEPARATOR + 
		"components.cif.gz";

		File f = new File(filename);

		if ( ! f.exists()) {

			downloadAllDefinitions();

		} else {
			// file exists.. did it get extracted?
			String directoryName = path + 	
			DownloadChemCompProvider.CHEM_COMP_CACHE_DIRECTORY +
			FILE_SEPARATOR;

			File dir = new File(directoryName);

			FilenameFilter filter =new FilenameFilter() {

				public boolean accept(File dir, String file) {
					return file.endsWith(".cif.gz");
				}
			};
			String[] files = dir.list(filter);
			if ( files.length < 500) {
				// not all did get unpacked
				split();
			}
		}
	}

	private void split(){

		System.out.println("Installing individual chem comp files ...");
		
		String filename = path + 
		DownloadChemCompProvider.CHEM_COMP_CACHE_DIRECTORY + FILE_SEPARATOR +
		"components.cif.gz";<commit_msg>adding a check if the path is set to null<commit_after>package org.biojava.bio.structure.io.mmcif;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileOutputStream;
import java.io.FilenameFilter;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.io.StringWriter;
import java.net.HttpURLConnection;
import java.net.URL;
import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.atomic.AtomicBoolean;
import java.util.zip.GZIPOutputStream;


import org.biojava.bio.structure.align.ce.AbstractUserArgumentProcessor;
import org.biojava.bio.structure.align.util.HTTPConnectionTools;
import org.biojava.bio.structure.io.mmcif.model.ChemComp;
import org.biojava3.core.util.InputStreamProvider;



/** This provider of chemical components can download and cache chemical component definition files from the RCSB PDB web site.
 *  It is the default way to access these definitions.
 *  If this provider is called he first time, it will download and install all chemical 
 *  component definitions in a local directory. 
 *  Once the definition files have been installed, it has quick startup time and low memory requirements. 
 *
 *  An alternative provider, that keeps all definitions in memory is the {@link AllChemCompProvider}. Another provider, that
 *  does not require any network access, but only can support a limited set of chemical component definitions, is the {@link ReducedChemCompProvider}. 
 *  
 * 
 * @author <NAME>
 *
 */
public class DownloadChemCompProvider implements ChemCompProvider {

	static String path; 
	private static final String FILE_SEPARATOR = System.getProperty("file.separator");
	static final String NEWLINE = System.getProperty("line.separator");

	public static String CHEM_COMP_CACHE_DIRECTORY = "chemcomp";

	private static String serverLocation = "http://www.rcsb.org/pdb/files/ligand/";

	// flags to make sure there is only one thread running that is loading the dictionary
	static AtomicBoolean loading = new AtomicBoolean(false);

	static final List<String> protectedIDs = new ArrayList<String> ();
	static {
		protectedIDs.add("CON");
		protectedIDs.add("PRN");
		protectedIDs.add("AUX");
		protectedIDs.add("NUL");
	}
	
	/** by default we will download only some of the files. User has to request that all files should be downloaded...
	 * 
	 */
	boolean downloadAll = false;

	public DownloadChemCompProvider(){
		//System.out.println("USING DOWNLOAD CHEM COMP PROVIDER");		
	}

	/** checks if the chemical components already have been installed into the PDB directory.
	 *  If not, will download the chemical components definitions file and split it up into small
	 *  subfiles.
	 */
	public void checkDoFirstInstall(){

		if ( ! downloadAll ) {
			return;
		}
		
		if ( path == null)
			path =  System.getProperty(AbstractUserArgumentProcessor.CACHE_DIR);
		if (path == null || path.equals(""))
			path = System.getProperty(AbstractUserArgumentProcessor.PDB_DIR);
		
		String filename = path + 	
		DownloadChemCompProvider.CHEM_COMP_CACHE_DIRECTORY +
		FILE_SEPARATOR + 
		"components.cif.gz";

		File f = new File(filename);

		if ( ! f.exists()) {

			downloadAllDefinitions();

		} else {
			// file exists.. did it get extracted?
			String directoryName = path + 	
			DownloadChemCompProvider.CHEM_COMP_CACHE_DIRECTORY +
			FILE_SEPARATOR;

			File dir = new File(directoryName);

			FilenameFilter filter =new FilenameFilter() {

				public boolean accept(File dir, String file) {
					return file.endsWith(".cif.gz");
				}
			};
			String[] files = dir.list(filter);
			if ( files.length < 500) {
				// not all did get unpacked
				split();
			}
		}
	}

	private void split(){

		System.out.println("Installing individual chem comp files ...");
		
		if ( path == null)
			path = System.getProperty("java.io.tmpdir");
		
		String filename = path + 
		DownloadChemCompProvider.CHEM_COMP_CACHE_DIRECTORY + FILE_SEPARATOR +
		"components.cif.gz";
{'fraction_non_alphanumeric': 0.09475145092101943, 'fraction_numerical': 0.0010093363613424174, 'mean_word_length': 4.137394685677252, 'pattern_counts': {'":': 0, '<': 11, '<?xml version=': 0, '>': 9, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 26, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '3026208', 'n_tokens_mistral': 2463, 'n_tokens_neox': 2183, 'n_words': 707}