<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/
 *
 */
package org.biojava3.ws.alignment.qblast;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.net.MalformedURLException;
import java.net.URL;
import java.net.URLConnection;
import java.util.HashMap;

import org.biojava3.core.sequence.template.Sequence;
import org.biojava3.core.sequence.template.Compound;

import org.biojava3.ws.alignment.RemotePairwiseAlignmentProperties;
import org.biojava3.ws.alignment.RemotePairwiseAlignmentService;
import org.biojava3.ws.alignment.RemotePairwiseAlignmentOutputProperties;

/**
 * NCBIQBlastService - A simple way of submitting BLAST request to the QBlast
 * service at NCBI.
 * 
 * <p>
 * NCBI provides a Blast server through a CGI-BIN interface. NCBIQBlastService
 * simply encapsulates an access to it by giving users access to get/set methods
 * to fix sequence, program and database as well as advanced options.
 * </p>
 * 
 * <p>
 * The philosophy behind NCBIQBlastService is to disconnect submission of Blast
 * requests from collection of Blast results. This is done so to allow a user to
 * submit multiple Blast requests while allowing recovery of the reports at a
 * later time.
 * </p>
 * 
 * <p>
 * Presently, only blastall programs are accessible. blastpgp and megablast are
 * high-priorities.
 * </p>
 * 
 * <p>
 * Many thanks to <NAME> for helping in debugging after the migration
 * from BJ1.7 to BJ3.0.
 * </p>
 * 
 * @author <NAME>, <NAME>
 * @since Biojava 3
 * 
 */
public class NCBIQBlastService implements RemotePairwiseAlignmentService {

	private static String baseurl = "http://www.ncbi.nlm.nih.gov/blast/Blast.cgi";
	private URL aUrl;
	private URLConnection uConn;
	private OutputStreamWriter fromQBlast;
	private BufferedReader rd;

	private String email = "<EMAIL>";
	private String tool = "biojava3";

	private String cmd = null;
	private String seq = null;
	private String tmp = null;

	private String rid;
	private long step;
	private long start;
	private HashMap<String, Long> holder;

	/**
	 * The constructor for a QBlast service request.
	 * 
	 * @throws Exception
	 *             if the NCBI URL is unresponsive
	 * 
	 */
	public NCBIQBlastService() throws Exception {
		try {
			this.aUrl = new URL(baseurl);
			this.uConn = setQBlastServiceProperties(aUrl.openConnection());
			this.holder = new HashMap<String, Long>();
		}
		/*
		 * Needed but should never be thrown since the URL is static and known
		 * to exist
		 */
		catch (MalformedURLException e) {
			throw new Exception(
					"It looks like the URL for NCBI QBlast service is wrong.\n");
		}
		/*
		 * Intercept if the program can't connect to QBlast service
		 */
		catch (IOException e) {
			throw new Exception(
					"Impossible to connect to QBlast service at this time. Check your network connection.\n");
		}
	}

	/**
	 * 
	 * This class is the actual worker doing all the dirty stuff related to
	 * sending the Blast request to the CGI_BIN interface. It should never be
	 * used as is but any method wanting to send a Blast request should manage
	 * to use it by feeding it the right parameters.
	 * 
	 * @param str
	 *            : a <code>String</code> representation of a sequence from
	 *            either of the three wrapper methods: Sequence, sequence
	 *            string, gid
	 * @param rpa
	 *            :a <code>RemotePairwiseAlignmentProperties</code> object
	 * 
	 * @return rid : the ID of this request on the NCBI QBlast server
	 * 
	 * @throws Exception
	 *             if unable to connect to the NCBI QBlast service
	 * 
	 */
	private String sendActualAlignementRequest(String str,
			NCBIQBlastAlignmentProperties rpa) throws Exception {

		rpa.setBlastCommandsToQBlast();

		if (str.length() == 0 || str == null) {
			throw new Exception(
					"Impossible to execute QBlast request. Your sequence info has not been set correctly.\n");
		} else {
			seq = "&QUERY=" + str;
		}

		cmd = rpa.getBlastCommandsToQBlast() + "&TOOL=" + this.getTool()
				+ "&EMAIL=" + this.getEmail();

		// Let's end with the sequence's string
		cmd = cmd + seq;

		/*
		 * DEBUG LINE
		 */
		System.out.println(cmd);
<commit_msg>Reversed conditions in if statement so null check was first.  Prevents NPE.<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/
 *
 */
package org.biojava3.ws.alignment.qblast;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.net.MalformedURLException;
import java.net.URL;
import java.net.URLConnection;
import java.util.HashMap;

import org.biojava3.core.sequence.template.Sequence;
import org.biojava3.core.sequence.template.Compound;

import org.biojava3.ws.alignment.RemotePairwiseAlignmentProperties;
import org.biojava3.ws.alignment.RemotePairwiseAlignmentService;
import org.biojava3.ws.alignment.RemotePairwiseAlignmentOutputProperties;

/**
 * NCBIQBlastService - A simple way of submitting BLAST request to the QBlast
 * service at NCBI.
 * 
 * <p>
 * NCBI provides a Blast server through a CGI-BIN interface. NCBIQBlastService
 * simply encapsulates an access to it by giving users access to get/set methods
 * to fix sequence, program and database as well as advanced options.
 * </p>
 * 
 * <p>
 * The philosophy behind NCBIQBlastService is to disconnect submission of Blast
 * requests from collection of Blast results. This is done so to allow a user to
 * submit multiple Blast requests while allowing recovery of the reports at a
 * later time.
 * </p>
 * 
 * <p>
 * Presently, only blastall programs are accessible. blastpgp and megablast are
 * high-priorities.
 * </p>
 * 
 * <p>
 * Many thanks to <NAME> for helping in debugging after the migration
 * from BJ1.7 to BJ3.0.
 * </p>
 * 
 * @author <NAME>, <NAME>
 * @since Biojava 3
 * 
 */
public class NCBIQBlastService implements RemotePairwiseAlignmentService {

	private static String baseurl = "http://www.ncbi.nlm.nih.gov/blast/Blast.cgi";
	private URL aUrl;
	private URLConnection uConn;
	private OutputStreamWriter fromQBlast;
	private BufferedReader rd;

	private String email = "<EMAIL>";
	private String tool = "biojava3";

	private String cmd = null;
	private String seq = null;
	private String tmp = null;

	private String rid;
	private long step;
	private long start;
	private HashMap<String, Long> holder;

	/**
	 * The constructor for a QBlast service request.
	 * 
	 * @throws Exception
	 *             if the NCBI URL is unresponsive
	 * 
	 */
	public NCBIQBlastService() throws Exception {
		try {
			this.aUrl = new URL(baseurl);
			this.uConn = setQBlastServiceProperties(aUrl.openConnection());
			this.holder = new HashMap<String, Long>();
		}
		/*
		 * Needed but should never be thrown since the URL is static and known
		 * to exist
		 */
		catch (MalformedURLException e) {
			throw new Exception(
					"It looks like the URL for NCBI QBlast service is wrong.\n");
		}
		/*
		 * Intercept if the program can't connect to QBlast service
		 */
		catch (IOException e) {
			throw new Exception(
					"Impossible to connect to QBlast service at this time. Check your network connection.\n");
		}
	}

	/**
	 * 
	 * This class is the actual worker doing all the dirty stuff related to
	 * sending the Blast request to the CGI_BIN interface. It should never be
	 * used as is but any method wanting to send a Blast request should manage
	 * to use it by feeding it the right parameters.
	 * 
	 * @param str
	 *            : a <code>String</code> representation of a sequence from
	 *            either of the three wrapper methods: Sequence, sequence
	 *            string, gid
	 * @param rpa
	 *            :a <code>RemotePairwiseAlignmentProperties</code> object
	 * 
	 * @return rid : the ID of this request on the NCBI QBlast server
	 * 
	 * @throws Exception
	 *             if unable to connect to the NCBI QBlast service
	 * 
	 */
	private String sendActualAlignementRequest(String str,
			NCBIQBlastAlignmentProperties rpa) throws Exception {

		rpa.setBlastCommandsToQBlast();

		if (str == null || str.length() == 0) {
			throw new Exception(
					"Impossible to execute QBlast request. Your sequence info has not been set correctly.\n");
		} else {
			seq = "&QUERY=" + str;
		}

		cmd = rpa.getBlastCommandsToQBlast() + "&TOOL=" + this.getTool()
				+ "&EMAIL=" + this.getEmail();

		// Let's end with the sequence's string
		cmd = cmd + seq;

		/*
		 * DEBUG LINE
		 */
		System.out.println(cmd);

{'fraction_non_alphanumeric': 0.08837497418955192, 'fraction_numerical': 0.0026842865992153624, 'mean_word_length': 3.273048081164535, 'pattern_counts': {'":': 0, '<': 39, '<?xml version=': 0, '>': 39, 'https://': 0, 'lorem ipsum': 0, 'www.': 6, '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': '3231851', 'n_tokens_mistral': 3063, 'n_tokens_neox': 2762, 'n_words': 1067}