<commit_before>/*
    FASTX-toolkit - FASTA/FASTQ preprocessing tools.
    Copyright (C) 2009  <NAME> (<EMAIL>)

    This program is free software: you can redistribute it and/or modify
    it under the terms of the GNU Affero General Public License as
    published by the Free Software Foundation, either version 3 of the
    License, or (at your option) any later version.

    This program is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU Affero General Public License for more details.

    You should have received a copy of the GNU Affero General Public License
    along with this program.  If not, see <http://www.gnu.org/licenses/>.
*/
#include <limits.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <getopt.h>
#include <errno.h>
#include <err.h>

#include <config.h>

#include "fastx.h"
#include "fastx_args.h"

#define MAX_ADAPTER_LEN 100

const char* usage=
"usage: fastx_trimmer [-h] [-f N] [-l N] [-z] [-v] [-i INFILE] [-o OUTFILE]\n" \
"Part of " PACKAGE_STRING " by <NAME> (<EMAIL>)\n" \
"\n" \
"   [-h]         = This helpful help screen.\n" \
"   [-f N]       = First base to keep. Default is 1 (=first base).\n" \
"   [-l N]       = Last base to keep. Default is entire read.\n" \
"   [-z]         = Compress output with GZIP.\n" \
"   [-i INFILE]  = FASTA/Q input file. default is STDIN.\n" \
"   [-o OUTFILE] = FASTA/Q output file. default is STDOUT.\n" \
"\n";

#define DO_NOT_TRIM_LAST_BASE (0)

int keep_first_base=1;
int keep_last_base=DO_NOT_TRIM_LAST_BASE;

FASTX fastx;

int parse_program_args(int __attribute__((unused)) optind, int optc, char* optarg)
{
	switch(optc) {
	case 'f':
		if (optarg==NULL) 
			errx(1, "[-f] parameter requires an argument value");
		keep_first_base = strtoul(optarg,NULL,10);
		if (keep_first_base<=0 || keep_first_base>=MAX_SEQ_LINE_LENGTH) 
			errx(1,"Invalid number bases to keep (-f %s)", optarg);
		break;

	case 'l':
		if (optarg==NULL) 
			errx(1, "[-l] parameter requires an argument value");
		keep_last_base = strtoul(optarg,NULL,10);
		if (keep_last_base<=0 ||  keep_last_base>=MAX_SEQ_LINE_LENGTH) 
			errx(1,"Invalid number bases to keep (-l %s)", optarg);
		break;

	default:
		errx(1, __FILE__ ":%d: Unknown argument (%c)", __LINE__, optc ) ;

	}
	return 1;
}


int main(int argc, char* argv[])
{
	size_t i;
	
	fastx_parse_cmdline(argc, argv, "l:f:", parse_program_args);

	fastx_init_reader(&fastx, get_input_filename(), 
		FASTA_OR_FASTQ, ALLOW_N, REQUIRE_UPPERCASE, get_fastq_ascii_quality_offset() );

	fastx_init_writer(&fastx, get_output_filename(), OUTPUT_SAME_AS_INPUT, compress_output_flag());

	while ( fastx_read_next_record(&fastx) ) {

		if (keep_last_base != DO_NOT_TRIM_LAST_BASE) {
			fastx.nucleotides[keep_last_base] = 0 ;
		}

		if (keep_first_base != 1) {
			for (i=0; i < strlen(fastx.nucleotides)-keep_first_base+1 ; i++) {
				fastx.nucleotides[i] = fastx.nucleotides[i+keep_first_base-1];
				fastx.quality[i] = fastx.quality[i+keep_first_base-1];
			}
			fastx.nucleotides[i] = 0 ;
		}<commit_msg>Bugfix: trimming too-short sequences causes segfault.<commit_after>/*
    FASTX-toolkit - FASTA/FASTQ preprocessing tools.
    Copyright (C) 2009  <NAME> (<EMAIL>)

    This program is free software: you can redistribute it and/or modify
    it under the terms of the GNU Affero General Public License as
    published by the Free Software Foundation, either version 3 of the
    License, or (at your option) any later version.

    This program is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU Affero General Public License for more details.

    You should have received a copy of the GNU Affero General Public License
    along with this program.  If not, see <http://www.gnu.org/licenses/>.
*/
#include <limits.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <getopt.h>
#include <errno.h>
#include <err.h>

#include <config.h>

#include "fastx.h"
#include "fastx_args.h"

#define MAX_ADAPTER_LEN 100

const char* usage=
"usage: fastx_trimmer [-h] [-f N] [-l N] [-z] [-v] [-i INFILE] [-o OUTFILE]\n" \
"Part of " PACKAGE_STRING " by <NAME> (<EMAIL>)\n" \
"\n" \
"   [-h]         = This helpful help screen.\n" \
"   [-f N]       = First base to keep. Default is 1 (=first base).\n" \
"   [-l N]       = Last base to keep. Default is entire read.\n" \
"   [-z]         = Compress output with GZIP.\n" \
"   [-i INFILE]  = FASTA/Q input file. default is STDIN.\n" \
"   [-o OUTFILE] = FASTA/Q output file. default is STDOUT.\n" \
"\n";

#define DO_NOT_TRIM_LAST_BASE (0)

int keep_first_base=1;
int keep_last_base=DO_NOT_TRIM_LAST_BASE;

FASTX fastx;

int parse_program_args(int __attribute__((unused)) optind, int optc, char* optarg)
{
	switch(optc) {
	case 'f':
		if (optarg==NULL) 
			errx(1, "[-f] parameter requires an argument value");
		keep_first_base = strtoul(optarg,NULL,10);
		if (keep_first_base<=0 || keep_first_base>=MAX_SEQ_LINE_LENGTH) 
			errx(1,"Invalid number bases to keep (-f %s)", optarg);
		break;

	case 'l':
		if (optarg==NULL) 
			errx(1, "[-l] parameter requires an argument value");
		keep_last_base = strtoul(optarg,NULL,10);
		if (keep_last_base<=0 ||  keep_last_base>=MAX_SEQ_LINE_LENGTH) 
			errx(1,"Invalid number bases to keep (-l %s)", optarg);
		break;

	default:
		errx(1, __FILE__ ":%d: Unknown argument (%c)", __LINE__, optc ) ;

	}
	return 1;
}


int main(int argc, char* argv[])
{
	size_t i;
	
	fastx_parse_cmdline(argc, argv, "l:f:", parse_program_args);

	fastx_init_reader(&fastx, get_input_filename(), 
		FASTA_OR_FASTQ, ALLOW_N, REQUIRE_UPPERCASE, get_fastq_ascii_quality_offset() );

	fastx_init_writer(&fastx, get_output_filename(), OUTPUT_SAME_AS_INPUT, compress_output_flag());

	while ( fastx_read_next_record(&fastx) ) {

		if (keep_last_base != DO_NOT_TRIM_LAST_BASE) {
			fastx.nucleotides[keep_last_base] = 0 ;
		}

		if (keep_first_base != 1) {
			if ( strlen(fastx.nucleotides) < (size_t)keep_first_base ) //sequence too short - remove it
				continue ;
			for (i=0; i < strlen(fastx.nucleotides)-keep_first_base+1 ; i++) {
				fastx.nucleotides[i] = fastx.nucleotides[i+keep_first_base-1];
				fastx.quality[i] = fastx.quality[i+keep_first_base-1];
			}
			fastx.nucleotides[i] = 0 ;
		}
{'fraction_non_alphanumeric': 0.1326436071649166, 'fraction_numerical': 0.00926497838171711, 'mean_word_length': 3.787139689578714, 'pattern_counts': {'":': 2, '<': 36, '<?xml version=': 0, '>': 33, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, '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': '7639176', 'n_tokens_mistral': 2553, 'n_tokens_neox': 2355, 'n_words': 668}