fastq.c
/***************************************************************************
 *  Use auto-c2man to generate a man page from this comment
 *
 *  Library:
 *      #include <biolibc/fastq.h>
 *      -lbiolibc -lxtend
 *
 *  Description:
 *      Locate start of a low-quality 3' end in a FASTQ read.  This
 *      function uses the same algorithm as fastq and cutadapt as of the
 *      time of writing.  Namely, it starts at the 3' end of the quality
 *      string and sums (base quality - minimum quality) while moving in
 *      the 5' direction.  This sum will be < 0 as long as the average
 *      base quality is < minimum quality.  It also keeps track of where
 *      the minimum of this sum occurs.  When the sum become > 0, we have
 *      reached a point where the average quality of the 3' end is
 *      satisfactory, and it is assumed it will remain that way if we
 *      continue in the 5' direction.  ( Illumina reads tend to drop in
 *      quality near the 3' end. )  The location of the minimum sum is
 *      then returned, since the average quality of everything in the 5'
 *      direction must be satisfactory.
 *  
 *  Arguments:
 *      read        FASTQ read to be searched
 *      min_qual    Minimum quality of bases to keep
 *      phred_base  Offset into the ISO character set used by PHRED scores
 *                  (usually 33 for modern data)
 *
 *  Returns:
 *      Index of first low-quality base at the 3' end if found,
 *      index of NULL terminator otherwise
 *
 *  Examples:
 *      bl_fastq_t  read;
 *      
 *      ...
 *      index = bl_fastq_find_3p_low_qual(&read, 20, 33);
 *      bl_fastq_3p_trim(&read, index);
 *
 *  See also:
 *      bl_fastq_find_adapter_smart(3), bl_fastq_find_adapter_exact(3),
 *      bl_fastq_trim(3)
 *
 *  History: 
 *  Date        Name        Modification
 *  2022-01-02  Jason Bacon Begin
 ***************************************************************************/
Use auto-c2man to generate a man page from this comment
Library:
#include 
-lbiolibc -lxtend

Locate start of a low-quality 3' end in a FASTQ read.  This
function uses the same algorithm as fastq and cutadapt as of the
time of writing.  Namely, it starts at the 3' end of the quality
string and sums (base quality - minimum quality) while moving in
the 5' direction.  This sum will be < 0 as long as the average
base quality is < minimum quality.  It also keeps track of where
the minimum of this sum occurs.  When the sum become > 0, we have
reached a point where the average quality of the 3' end is
satisfactory, and it is assumed it will remain that way if we
continue in the 5' direction.  ( Illumina reads tend to drop in
quality near the 3' end. )  The location of the minimum sum is
then returned, since the average quality of everything in the 5'
direction must be satisfactory.

read        FASTQ read to be searched
min_qual    Minimum quality of bases to keep
phred_base  Offset into the ISO character set used by PHRED scores
(usually 33 for modern data)

Index of first low-quality base at the 3' end if found,
index of NULL terminator otherwise



index = bl_fastq_find_3p_low_qual(&read, 20, 33);
bl_fastq_3p_trim(&read, index).



Date        Name        Modification
2022-01-02  Jason Bacon Begin
Use auto - c2man to generate a man page from this comment Library : #include - lbiolibc - lxtend Locate start of a low - quality 3 ' end in a FASTQ read . This function uses the same algorithm as fastq and cutadapt as of the time of writing . Namely it starts at the 3 ' end of the quality string and sums ( base quality - minimum quality ) while moving in the 5 ' direction . This sum will be < 0 as long as the average base quality is < minimum quality . It also keeps track of where the minimum of this sum occurs . When the sum become > 0 we have reached a point where the average quality of the 3 ' end is satisfactory and it is assumed it will remain that way if we continue in the 5 ' direction . ( Illumina reads tend to drop in quality near the 3 ' end . ) The location of the minimum sum is then returned since the average quality of everything in the 5 ' direction must be satisfactory . read FASTQ read to be searched min_qual Minimum quality of bases to keep phred_base Offset into the ISO character set used by PHRED scores ( usually 33 for modern data ) Index of first low - quality base at the 3 ' end if found index of NULL terminator otherwise index = bl_fastq_find_3p_low_qual ( &read 20 33 ) ; bl_fastq_3p_trim ( &read index ) . Date Name Modification 2022 - 01 - 02 Jason Bacon Begin
size_t  bl_fastq_find_3p_low_qual(const bl_fastq_t *read, unsigned min_qual,
			unsigned phred_base)
{
    ssize_t     c,
		cut_pos;
    long        sum,
		min_sum;
    if ( read->seq_len != read->qual_len )
    {
	fprintf(stderr, "bl_fastq_find_3p_low_qual(): qual_len != seq_len.\n");
	exit(EX_DATAERR);
    }
    sum = min_sum = 0;
    c = read->qual_len - 1;
    cut_pos = read->seq_len;
    while ( (c >= 0) && (sum <= 0) )
    {
	sum = (long)sum + read->qual[c] - phred_base - min_qual;
	if ( sum < min_sum )
	{
	    min_sum = sum;
	    cut_pos = c;
	}
	--c;
    }
    return cut_pos;
}
size_t bl_fastq_find_3p_low_qual ( const bl_fastq_t * read , unsigned min_qual , unsigned phred_base ) { ssize_t c , cut_pos ; long sum , min_sum ; if ( read -> seq_len != read -> qual_len ) { fprintf ( stderr , " \n " ) ; exit ( EX_DATAERR ) ; } sum = min_sum = 0 ; c = read -> qual_len - 1 ; cut_pos = read -> seq_len ; while ( ( c >= 0 ) && ( sum <= 0 ) ) { sum = ( long ) sum + read -> qual [ c ] - phred_base - min_qual ; if ( sum < min_sum ) { min_sum = sum ; cut_pos = c ; } -- c ; } return cut_pos ; }
Use auto-c2man to generate a man page from this comment
  Library:
      #include <biolibc/fastq.h>
      -lbiolibc -lxtend
Use auto - c2man to generate a man page from this comment Library : #include <biolibc / fastq . h > - lbiolibc - lxtend
/*
     *  Use same algorithm as BWA/cutadapt
     *  https://cutadapt.readthedocs.io/en/stable/algorithms.html#quality-trimming-algorithm
     *  score-minimum will be negative for bases we want to remove
     *  Sum score-minimum for each base starting at end until sum > 0
     *  Use the position of the minimum sum as the trim point
     *  Verified using 42, 40, 26, 27, 8, 7, 11, 4, 2, 3 example from link
     */ // Revert promotions to unsigned // fprintf(stderr, "%zu %c %c %ld\n", c, read->seq[c], read->qual[c], sum); // fprintf(stderr, "Returning %zd\n", cut_pos);