### Install pyfastx from source Source: https://pyfastx.readthedocs.io/en/latest/_sources/installation.rst.txt After cloning the repository, navigate to the pyfastx directory and run this command to install the library using its setup script. Ensure you have the necessary build dependencies. ```bash cd pyfastx python setup.py install ``` -------------------------------- ### Install pyfastx using pip Source: https://pyfastx.readthedocs.io/en/latest/_sources/installation.rst.txt Use this command to install the latest version of pyfastx from the Python Package Index. Ensure pip is installed and up-to-date. ```bash pip install pyfastx ``` -------------------------------- ### Parallel Sequence Access with Pyfastx and Multiprocessing Source: https://pyfastx.readthedocs.io/en/latest/advance.html Use this example to parallelize sequence access from a FASTA file using Pyfastx and Python's multiprocessing module. Ensure the index file is created in the main process before starting the pool. Each worker process recreates the Fasta object. ```python import random import pyfastx import multiprocessing as mp # process worker # randomly fetch five sequences and print to stdout def worker(woker_num, seq_counts): #recreate the Fasta object in subprocess fa = pyfastx.Fasta('test.fa') for i in random.sample(range(seq_counts), 5): print("worker {} print:\n{}".format(worker_num, fa[i].raw)) if __name__ == '__main__': #ensure index file has been created in main process fa = pyfastx.Fasta('test.fa') #get sequence counts c = len(fa) #start the process pool pool = mp.Pool() #add five task workers to run for n in range(5): pool.apply_async(worker, args=(n, c)) #wait for tasks to finish pool.close() pool.join() ``` -------------------------------- ### Build pyfastx extensions from source Source: https://pyfastx.readthedocs.io/en/latest/_sources/installation.rst.txt Navigate to the pyfastx directory and use this command to build the C extensions for pyfastx. This is part of the source installation process. ```bash cd pyfastx python setup.py build_ext -i ``` -------------------------------- ### Clone pyfastx repository Source: https://pyfastx.readthedocs.io/en/latest/_sources/installation.rst.txt Clone the pyfastx source code repository from GitHub to install from source. This is an alternative to installing via pip. ```bash git clone https://github.com/lmdu/pyfastx.git ``` -------------------------------- ### FASTQ Get Read by Name or Index Source: https://pyfastx.readthedocs.io/en/latest/_sources/usage.rst.txt Demonstrates how to access individual FASTQ reads using their name or index. ```APIDOC ## FASTQ Get Read by Name or Index ### Description Retrieve a specific read from a FASTQ object using its unique name (header line) or its numerical index. Supports accessing reads by positive and negative indices. ### Method N/A (Illustrative examples) ### Endpoint N/A (Illustrative examples) ### Parameters #### Query Parameters - **fq** (object) - Required - The FASTQ object. - **read_name** (string) - Required - The full header line of the read to retrieve. - **index** (integer) - Required - The numerical index of the read to retrieve. ### Request Example ```python # Assuming 'fq' is a loaded FASTQ object # Get read by name r1 = fq['A00129:183:H77K2DMXX:1:1101:4752:1047'] print(r1) # Get read by positive index r2 = fq[10] print(r2) # Get read by negative index (last read) r3 = fq[-1] print(r3) # Check if a read exists 'A00129:183:H77K2DMXX:1:1101:4752:1047' in fq ``` ### Response Example ``` A00129:183:H77K2DMXX:1:1101:4752:1047 with length of 150 A00129:183:H77K2DMXX:1:1101:18041:1078 with length of 150 A00129:183:H77K2DMXX:1:1101:31575:4726 with length of 150 True ``` ``` -------------------------------- ### FASTQ Get Information Source: https://pyfastx.readthedocs.io/en/latest/_sources/usage.rst.txt Provides methods to retrieve various statistics and information about a FASTQ file. ```APIDOC ## FASTQ Get Information ### Description Retrieve various statistics and information about a FASTQ file, such as read counts, total bases, GC content, base composition, quality encoding, and length statistics. ### Method N/A (Illustrative examples) ### Endpoint N/A (Illustrative examples) ### Parameters #### Query Parameters - **fq** (object) - Required - The FASTQ object. ### Request Example ```python # Assuming 'fq' is a loaded FASTQ object len(fq) fq.size fq.gc_content fq.composition fq.phred fq.avglen fq.maxlen fq.minlen fq.maxqual fq.minqual fq.encoding_type ``` ### Response Example ```python # For len(fq) 800 # For fq.size 120000 # For fq.gc_content 66.17471313476562 # For fq.composition {'A': 20501, 'C': 39705, 'G': 39704, 'T': 20089, 'N': 1} # For fq.phred 33 # For fq.avglen 150.0 # For fq.maxlen 150 # For fq.minlen 150 # For fq.maxqual 70 # For fq.minqual 35 # For fq.encoding_type ['Sanger Phred+33', 'Illumina 1.8+ Phred+33'] ``` ``` -------------------------------- ### Update pyfastx using pip Source: https://pyfastx.readthedocs.io/en/latest/_sources/installation.rst.txt Run this command to update an existing pyfastx installation to the latest version available on PyPI. ```bash pip install -U pyfastx ``` -------------------------------- ### Get sequence length statistics Source: https://pyfastx.readthedocs.io/en/latest/_sources/usage.rst.txt Retrieve the mean and median sequence lengths. ```python >>> # get sequence average length >>> fa.mean 408 >>> # get sequence median length >>> fa.median 430 ``` -------------------------------- ### FastqKeys - Get Fastq Keys Source: https://pyfastx.readthedocs.io/en/latest/_sources/usage.rst.txt Retrieve all read names from a Fastq file as a list-like object. Supports checking for key existence, iteration, and conversion to a list. ```APIDOC ## Get Fastq Keys ### Description Get all names of read as a list-like object. ### Method `fq.keys()` ### Parameters None ### Request Example ```python ids = fq.keys() print(ids) print(len(ids)) print(ids[0]) print('A00129:183:H77K2DMXX:1:1101:14416:1031' in ids) ``` ### Response - **ids** (): A list-like object containing Fastq read identifiers. ### Response Example ``` contains 800 keys 800 A00129:183:H77K2DMXX:1:1101:6804:1031 True ``` ``` -------------------------------- ### FastaKeys - Get Fasta Keys Source: https://pyfastx.readthedocs.io/en/latest/_sources/usage.rst.txt Retrieve all sequence names from a Fasta file as a list-like object. Supports checking for key existence, iteration, and conversion to a list. ```APIDOC ## Get Fasta Keys ### Description Get all names of sequence as a list-like object. ### Method `fa.keys()` ### Parameters None ### Request Example ```python ids = fa.keys() print(ids) print(len(ids)) print(ids[0]) print('JZ822577.1' in ids) for name in ids: print(name) print(list(ids)) ``` ### Response - **ids** (): A list-like object containing Fasta sequence identifiers. ### Response Example ``` contains 211 keys 211 JZ822577.1 True ``` ``` -------------------------------- ### Search for subsequence in FASTA Source: https://pyfastx.readthedocs.io/en/latest/usage.html Find the one-based start position of a subsequence in sense or antisense strands, or check for existence. ```python >>> # search subsequence in sense strand >>> fa[0].search('GCTTCAATACA') 262 >>> # check subsequence weather in sequence >>> 'GCTTCAATACA' in fa[0] True >>> # search subsequence in antisense strand >>> fa[0].search('CCTCAAGT', '-') 301 ``` -------------------------------- ### Get FASTA File Information Source: https://pyfastx.readthedocs.io/en/latest/usage.html Retrieve various statistics and information about a FASTA file, including sequence counts, total length, GC content, GC skews, nucleotide composition, file type, and gzip status. ```python >>> # get sequence counts in FASTA >>> len(fa) 211 ``` ```python >>> # get total sequence length of FASTA >>> fa.size 86262 ``` ```python >>> # get GC content of DNA sequences in FASTA >>> fa.gc_content 43.529014587402344 ``` ```python >>> # get GC skew of DNA sequences in FASTA >>> # New in pyfastx 0.3.8 >>> fa.gc_skews 0.004287730902433395 ``` ```python >>> # get composition of nucleotides in FASTA >>> fa.composition {'A': 24534, 'C': 18694, 'G': 18855, 'T': 24179} ``` ```python >>> # get fasta type (DNA, RNA, or protein) >>> # New in pyfastx 0.5.4 >>> fa.type 'DNA' ``` ```python >>> # check fasta file is gzip compressed >>> # New in pyfastx 0.5.4 >>> fa.is_gzip True ``` -------------------------------- ### Sample sequences using pyfastx Source: https://pyfastx.readthedocs.io/en/latest/_sources/commandline.rst.txt Displays the help menu for the sample command, which allows random sampling of sequences from a file. ```bash $ pyfastx sample -h usage: pyfastx sample [-h] (-n int | -p float) [-s int] [--sequential-read] [-o str] fastx positional arguments: fastx fasta or fastq file, gzip support optional arguments: -h, --help show this help message and exit -n int number of sequences to be sampled -p float proportion of sequences to be sampled, 0~1 -s int, --seed int random seed, default is the current system time --sequential-read start sequential reading, particularly suitable for sampling large numbers of sequences -o str, --out-file str output file, default: output to stdout ``` -------------------------------- ### Display CLI Help Source: https://pyfastx.readthedocs.io/en/latest/_sources/commandline.rst.txt Displays the main help menu for the pyfastx command line tool. ```bash $ pyfastx -h usage: pyfastx COMMAND [OPTIONS] A command line tool for FASTA/Q file manipulation optional arguments: -h, --help show this help message and exit -v, --version show program version number and exit Commands: index build index for fasta/q file stat show detailed statistics information of fasta/q file split split fasta/q file into multiple files fq2fa convert fastq file to fasta file subseq get subsequences from fasta file by region sample randomly sample sequences from fasta or fastq file extract extract full sequences or reads from fasta/q file ``` -------------------------------- ### Initialize FASTQ Object Source: https://pyfastx.readthedocs.io/en/latest/_sources/usage.rst.txt Read a plain or gzipped FASTQ file and build an index for random access. ```python >>> import pyfastx >>> fq = pyfastx.Fastq('tests/data/test.fq.gz') >>> fq tests/data/test.fq.gz contains 100 reads ``` -------------------------------- ### Get FASTA Sequence Counts by Length Source: https://pyfastx.readthedocs.io/en/latest/usage.html Get the count of sequences in a FASTA file whose length is greater than or equal to a specified length. ```python >>> # get counts of sequences with length >= 200 bp >>> fa.count(200) 173 ``` ```python >>> # get counts of sequences with length >= 500 bp >>> fa.count(500) 70 ``` -------------------------------- ### Build FASTA/Q Index Source: https://pyfastx.readthedocs.io/en/latest/_sources/commandline.rst.txt Builds an index for a FASTA or FASTQ file. Use the --full option to calculate base composition and speed up GC content calculations. ```bash $ pyfastx index -h usage: pyfastx index [-h] [-f] fastx [fastx ...] positional arguments: fastx fasta or fastq file, gzip support optional arguments: -h, --help show this help message and exit -f, --full build full index, base composition will be calculated ``` -------------------------------- ### flank() Method Source: https://pyfastx.readthedocs.io/en/latest/api_reference.html Get the flank sequence of a given subsequence. ```APIDOC ## flank(chrom, start, end, flank_length=50, use_cache=False) ### Description Get the left and right flank sequence of a given subsequence defined by start and end positions. ### Parameters - **chrom** (str) - Required - Chromosome name or sequence name - **start** (int) - Required - 1-based start position - **end** (int) - Required - 1-based end position - **flank_length** (int) - Optional - Length of flank sequence, default: 50 - **use_cache** (bool) - Optional - Cache the whole sequence, default: False ### Response - **Returns** (tuple) - Left flank and right flank sequence ``` -------------------------------- ### Read FASTA File and Build Index Source: https://pyfastx.readthedocs.io/en/latest/usage.html Read a plain or gzipped FASTA file and build an index for random access. Index building may take time but can be reused. ```python >>> import pyfastx >>> fa = pyfastx.Fasta('test/data/test.fa.gz') >>> fa test/data/test.fa.gz contains 211 seqs ``` -------------------------------- ### Get FASTA statistics Source: https://pyfastx.readthedocs.io/en/latest/_sources/usage.rst.txt Retrieve metadata and sequence statistics from a Fasta object. ```python >>> # get sequence counts in FASTA >>> len(fa) 211 >>> # get total sequence length of FASTA >>> fa.size 86262 >>> # get GC content of DNA sequences in FASTA >>> fa.gc_content 43.529014587402344 >>> # get GC skew of DNA sequences in FASTA >>> # New in pyfastx 0.3.8 >>> fa.gc_skews 0.004287730902433395 >>> # get composition of nucleotides in FASTA >>> fa.composition {'A': 24534, 'C': 18694, 'G': 18855, 'T': 24179} >>> # get fasta type (DNA, RNA, or protein) >>> # New in pyfastx 0.5.4 >>> fa.type 'DNA' >>> # check fasta file is gzip compressed >>> # New in pyfastx 0.5.4 >>> fa.is_gzip True ``` -------------------------------- ### Get Read Length Source: https://pyfastx.readthedocs.io/en/latest/_sources/usage.rst.txt Determine the length of a read using the built-in len() function. ```python >>> len(r) 150 ``` -------------------------------- ### Read FASTQ file Source: https://pyfastx.readthedocs.io/en/latest/usage.html Initialize a FASTQ object from a file to enable random access to reads. ```python >>> import pyfastx >>> fq = pyfastx.Fastq('tests/data/test.fq.gz') >>> fq ``` -------------------------------- ### Get longest and shortest sequences Source: https://pyfastx.readthedocs.io/en/latest/_sources/usage.rst.txt Access the longest and shortest sequences in a FASTA file. ```python >>> # get longest sequence >>> s = fa.longest >>> s JZ822609.1 with length of 821 >>> s.name 'JZ822609.1' >>> len(s) 821 >>> # get shortest sequence >>> s = fa.shortest >>> s JZ822617.1 with length of 118 >>> s.name 'JZ822617.1' >>> len(s) 118 ``` -------------------------------- ### FASTQ Read Plain or Gzipped File Source: https://pyfastx.readthedocs.io/en/latest/_sources/usage.rst.txt Explains how to read plain or gzipped FASTQ files and build an index for random access. ```APIDOC ## FASTQ Read Plain or Gzipped File ### Description Read plain or gzipped FASTQ files, build an index, and support random access to reads. This allows for efficient retrieval of specific reads from large FASTQ files. ### Method N/A (Illustrative examples) ### Endpoint N/A (Illustrative examples) ### Parameters #### Query Parameters - **filename** (string) - Required - Path to the FASTQ file. - **build_index** (boolean) - Optional - Whether to build an index for random access. Defaults to True. ### Request Example ```python import pyfastx fq = pyfastx.Fastq('tests/data/test.fq.gz') print(fq) ``` ### Response Example ``` tests/data/test.fq.gz contains 100 reads ``` ``` -------------------------------- ### pyfastx.Fasta Constructor Source: https://pyfastx.readthedocs.io/en/latest/api_reference.html Initializes a new Fasta object to read and parse a FASTA file. ```APIDOC ## pyfastx.Fasta ### Description Read and parse fasta files. The object can be used as a dict or list to access sequences by index or name. ### Parameters - **file_name** (str) - Required - The file path of input FASTA file - **index_file** (str) - Optional - The index file of FASTA file, default .fxi - **uppercase** (bool) - Optional - Always output uppercase sequence, default: True - **build_index** (bool) - Optional - Build index for random access, default: True - **full_index** (bool) - Optional - Calculate character composition during indexing, default: False - **full_name** (bool) - Optional - Use full header line as identifier, default: False - **memory_index** (bool) - Optional - Keep index in memory, default: False - **key_func** (function) - Optional - Lambda expression to obtain shortened identifier, default: None ``` -------------------------------- ### pyfastx.version API Source: https://pyfastx.readthedocs.io/en/latest/api_reference.html Provides functions to get the current version of pyfastx and check related library versions. ```APIDOC ## GET pyfastx.version() ### Description Retrieves the current version of the pyfastx library. Optionally, it can return versions of dependent libraries like zlib, sqlite3, and zran if debug mode is enabled. ### Method GET ### Endpoint /pyfastx/version ### Parameters #### Query Parameters - **debug** (bool) - Optional - If true, returns versions of pyfastx, zlib, sqlite3, and zran. ### Response #### Success Response (200) - **version** (str) - The version string of pyfastx. ### Response Example ```json { "version": "2.1.0" } ``` ``` ```APIDOC ## GET pyfastx.gzip_check() ### Description Checks if a given file is compressed using gzip. ### Method GET ### Endpoint /pyfastx/gzip_check ### Parameters #### Query Parameters - **file_name** (str) - Required - The path to the file to check. ### Response #### Success Response (200) - **is_gzip** (bool) - True if the file is gzip compressed, False otherwise. ### Response Example ```json { "is_gzip": true } ``` ``` ```APIDOC ## GET pyfastx.reverse_complement() ### Description Calculates the reverse complement of a given DNA sequence. ### Method GET ### Endpoint /pyfastx/reverse_complement ### Parameters #### Query Parameters - **seq** (str) - Required - The DNA sequence for which to calculate the reverse complement. ### Response #### Success Response (200) - **reverse_complement** (str) - The reverse complement sequence. ### Response Example ```json { "reverse_complement": "AGCT" } ``` ``` -------------------------------- ### Show File Statistics Source: https://pyfastx.readthedocs.io/en/latest/_sources/commandline.rst.txt Displays statistics for FASTA or FASTQ files. The info command provides detailed metrics like sequence counts, GC content, and length distributions. ```bash $ pyfastx stat -h usage: pyfastx stat [-h] fastx [fastx ...] positional arguments: fastx input fasta or fastq file, gzip support optional arguments: -h, --help show this help message and exit ``` ```bash $ pyfastx info tests/data/*.fa* fileName seqType seqCounts totalBases GC% avgLen medianLen maxLen minLen N50 L50 protein.fa protein 17 2265 - 133.24 80.0 419 23 263 4 rna.fa RNA 2 720 65.283 360.0 360.0 360 360 360 1 test.fa DNA 211 86262 43.529 408.82 386.0 821 118 516 66 test.fa.gz DNA 211 86262 43.529 408.82 386.0 821 118 516 66 ``` ```bash $ pyfastx info tests/data/*.fq* fileName readCounts totalBases GC% avgLen maxLen minLen maxQual minQual qualEncodingSystem test.fq 800 120000 66.175 150.0 150 150 70 35 Sanger Phred+33,Illumina 1.8+ Phred+33 test.fq.gz 800 120000 66.175 150.0 150 150 70 35 Sanger Phred+33,Illumina 1.8+ Phred+33 ``` -------------------------------- ### Get reverse complement sequence Source: https://pyfastx.readthedocs.io/en/latest/_sources/usage.rst.txt Retrieves the reverse complement of a sequence slice. This operation is performed on the sequence object itself. ```python >>> # get sliced sequence >>> fa[0][10:20].seq 'GTCAATTTCC' ``` ```python >>> # get reverse of sliced sequence >>> fa[0][10:20].reverse.complement.seq 'GGAAATTGAC' ``` -------------------------------- ### Extract sequences using pyfastx Source: https://pyfastx.readthedocs.io/en/latest/_sources/commandline.rst.txt Displays the help menu for the extract command, used to retrieve specific sequences from a FASTA or FASTQ file. ```bash $ pyfastx extract -h usage: pyfastx extract [-h] [-l str] [--reverse-complement] [--out-fasta] [-o str] [--sequential-read] fastx [name [name ...]] positional arguments: fastx fasta or fastq file, gzip support name sequence name or read name, multiple names were separated by space optional arguments: -h, --help show this help message and exit -l str, --list-file str a file containing sequence or read names, one name per line --reverse-complement output reverse complement sequence --out-fasta output fasta format when extract reads from fastq, default output fastq format -o str, --out-file str output file, default: output to stdout --sequential-read start sequential reading, particularly suitable for extracting large numbers of sequences ``` -------------------------------- ### nl() Method Source: https://pyfastx.readthedocs.io/en/latest/api_reference.html Calculate assembly N50 and L50 statistics. ```APIDOC ## nl(quantile) ### Description Calculate assembly N50 and L50 values for the FASTA file. ### Parameters - **quantile** (int) - Required - A number between 0 and 100, default: 50 ### Response - **Returns** (tuple) - (N50, L50) ``` -------------------------------- ### FASTQ Records Iteration (No Index) Source: https://pyfastx.readthedocs.io/en/latest/_sources/usage.rst.txt Demonstrates iterating through FASTQ records without building an index, returning name, sequence, and quality. ```APIDOC ## FASTQ Records Iteration (No Index) ### Description Parse plain or gzipped FASTQ files efficiently without building an index. Iteration returns a tuple containing the read name, sequence, and quality string. ### Method N/A (Illustrative examples) ### Endpoint N/A (Illustrative examples) ### Parameters #### Query Parameters - **filename** (string) - Required - Path to the FASTQ file. - **build_index** (boolean) - Optional - Set to False to disable index building. Defaults to True. - **full_name** (boolean) - Optional - If True, use the full header line as the read identifier. New in pyfastx 0.8.0. ### Request Example ```python import pyfastx # Iterate without index for name, seq, qual in pyfastx.Fastq('tests/data/test.fq.gz', build_index=False): print(name) print(seq) print(qual) # Iterate with full name for name, seq, qual in pyfastx.Fastq('test/data/test.fq', build_index=False, full_name=True): print(name, seq, qual) ``` ``` -------------------------------- ### CLI: pyfastx sample Source: https://pyfastx.readthedocs.io/en/latest/commandline.html Samples a subset of sequences from a FASTA or FASTQ file. ```APIDOC ## CLI Command: pyfastx sample ### Description Sample a specific number or proportion of sequences from a FASTA or FASTQ file. ### Parameters #### Positional Arguments - **fastx** (str) - Required - Path to the FASTA or FASTQ file (gzip supported). #### Optional Arguments - **-n** (int) - Optional - Number of sequences to be sampled. - **-p** (float) - Optional - Proportion of sequences to be sampled (0-1). - **-s, --seed** (int) - Optional - Random seed, default is current system time. - **--sequential-read** (flag) - Optional - Enable sequential reading for large files. - **-o, --out-file** (str) - Optional - Output file path, defaults to stdout. ``` -------------------------------- ### Get FASTA Sequence Mean and Median Length Source: https://pyfastx.readthedocs.io/en/latest/usage.html Calculate the average (mean) and median length of sequences within a FASTA file. ```python >>> # get sequence average length >>> fa.mean 408 ``` ```python >>> # get sequence median length >>> fa.median 430 ``` -------------------------------- ### Convert FASTQ to FASTA Source: https://pyfastx.readthedocs.io/en/latest/_sources/commandline.rst.txt Converts a FASTQ file into FASTA format, optionally specifying an output file. ```bash $ pyfastx fq2fa -h usage: pyfastx fq2fa [-h] [-o str] fastx positional arguments: fastx input fastq file, gzip support optional arguments: -h, --help show this help message and exit -o str, --out-file str output file, default: output to stdout ``` -------------------------------- ### Get Fastq Read Names Source: https://pyfastx.readthedocs.io/en/latest/_sources/usage.rst.txt Retrieve all read names (keys) from a Fastq file as a list-like object. Requires pyfastx version 0.8.0 or later. ```python >>> ids = fq.keys() >>> ids contains 800 keys ``` ```python >>> # get count of read >>> len(ids) 800 ``` ```python >>> # get key by index >>> ids[0] 'A00129:183:H77K2DMXX:1:1101:6804:1031' ``` ```python >>> # check key whether in fasta >>> 'A00129:183:H77K2DMXX:1:1101:14416:1031' in ids True ``` -------------------------------- ### FASTQ File Handling Source: https://pyfastx.readthedocs.io/en/latest/usage.html Methods for reading FASTQ files, iterating over records, and retrieving file-level statistics. ```APIDOC ## FASTQ Operations ### Description Read plain or gzipped FASTQ files, iterate over records, and retrieve metadata such as GC content, read counts, and quality encoding. ### Methods - **Initialization**: `pyfastx.Fastq(file_path, build_index=True, full_name=False)` - **Iteration**: `for name, seq, qual in fq:` ### Parameters - **file_path** (str) - Required - Path to the FASTQ file. - **build_index** (bool) - Optional - Whether to build an index for random access. Defaults to True. - **full_name** (bool) - Optional - Use full header line as identifier. Defaults to False. ### Response - **fq.size** (int) - Total bases in file. - **fq.gc_content** (float) - GC content percentage. - **fq.encoding_type** (list) - Detected quality encoding systems. ``` -------------------------------- ### Get Fasta Sequence Names Source: https://pyfastx.readthedocs.io/en/latest/_sources/usage.rst.txt Retrieve all sequence names (keys) from a Fasta file as a list-like object. Supports checking for key existence and iteration. ```python >>> ids = fa.keys() >>> ids contains 211 keys ``` ```python >>> # get count of sequence >>> len(ids) 211 ``` ```python >>> # get key by index >>> ids[0] 'JZ822577.1' ``` ```python >>> # check key whether in fasta >>> 'JZ822577.1' in ids True ``` ```python >>> # iterate over keys >>> for name in ids: >>> print(name) ``` ```python >>> # convert to a list >>> list(ids) ``` -------------------------------- ### FASTA Read Sequence Line by Line Source: https://pyfastx.readthedocs.io/en/latest/_sources/usage.rst.txt Shows how to iterate through a FASTA sequence line by line. ```APIDOC ## FASTA Read Sequence Line by Line ### Description Iterate through a FASTA sequence object line by line as they appear in the FASTA file. This feature is available from pyfastx version 0.3.0. Note that sliced sequences cannot be read line by line. ### Method N/A (Illustrative examples) ### Endpoint N/A (Illustrative examples) ### Parameters #### Query Parameters - **fa** (object) - Required - The FASTA object to iterate over. ### Request Example ```python # Assuming 'fa' is a loaded FASTA object for line in fa[0]: print(line) ``` ### Response Example ``` CTCTAGAGATTACTTCTTCACATTCCAGATCACTCAGGCTCTTTGTCATTTTAGTTTGACTAGGATATCG AGTATTCAAGCTCATCGCTTTTGGTAATCTTTGCGGTGCATGCCTTTGCATGCTGTATTGCTGCTTCATC ... ``` ``` -------------------------------- ### Get Read Quality ASCII String Source: https://pyfastx.readthedocs.io/en/latest/_sources/usage.rst.txt Access the quality score string for a read. This string uses ASCII characters to represent quality values. ```python >>> r.qual 'FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF:FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF:FF,FFFFFFFFFFFFFFFFFFFFFFFFFF,F:FFFFFFFFF:' ``` -------------------------------- ### FASTA Search for Subsequence Source: https://pyfastx.readthedocs.io/en/latest/_sources/usage.rst.txt Demonstrates how to search for a subsequence within a FASTA sequence and check for its existence. ```APIDOC ## FASTA Search for Subsequence ### Description Search for a subsequence within a given FASTA sequence and retrieve the one-based start position of its first occurrence. This feature is available from pyfastx version 0.3.6. ### Method N/A (Illustrative examples) ### Endpoint N/A (Illustrative examples) ### Parameters #### Query Parameters - **fa[0]** (object) - Required - The FASTA sequence object. - **subsequence** (string) - Required - The subsequence to search for. - **strand** (string) - Optional - Specifies the strand to search in. Default is sense strand ('+'). Use '-' for antisense strand. ### Request Example ```python # Search subsequence in sense strand fa[0].search('GCTTCAATACA') # Check if subsequence exists in sequence 'GCTTCAATACA' in fa[0] # Search subsequence in antisense strand fa[0].search('CCTCAAGT', '-') ``` ### Response Example ```python # For fa[0].search('GCTTCAATACA') 262 # For 'GCTTCAATACA' in fa[0] True # For fa[0].search('CCTCAAGT', '-') 301 ``` ``` -------------------------------- ### Get Longest and Shortest FASTA Sequence Source: https://pyfastx.readthedocs.io/en/latest/usage.html Retrieve the longest and shortest sequences from a FASTA file. Provides access to the sequence name, length, and the sequence itself. ```python >>> # get longest sequence >>> s = fa.longest >>> s JZ822609.1 with length of 821 >>> s.name 'JZ822609.1' >>> len(s) 821 ``` ```python >>> # get shortest sequence >>> s = fa.shortest >>> s JZ822617.1 with length of 118 >>> s.name 'JZ822617.1' >>> len(s) 118 ``` -------------------------------- ### pyfastx Library Overview Source: https://pyfastx.readthedocs.io/en/latest/genindex.html This section lists the primary classes and utility functions available in the pyfastx library. ```APIDOC ## pyfastx Library Classes and Functions ### Classes - **pyfastx.Fasta**: Class for handling FASTA files. - **pyfastx.Fastq**: Class for handling FASTQ files. - **pyfastx.Sequence**: Class representing a sequence object. - **pyfastx.Read**: Class representing a read object. - **pyfastx.FastaKeys / pyfastx.FastqKeys**: Classes for managing keys in FASTA/FASTQ files. ### Utility Functions - **pyfastx.gzip_check()**: Checks if a file is gzipped. - **pyfastx.reverse_complement()**: Computes the reverse complement of a sequence. - **pyfastx.version()**: Returns the current library version. ``` -------------------------------- ### Get Read Quality Integer Values Source: https://pyfastx.readthedocs.io/en/latest/_sources/usage.rst.txt Retrieve the quality scores for a read as a list of integers. The values are derived from the ASCII string by subtracting 33 or 64. ```python >>> r.quali [37, 37, 37, 37, 37, 37, 37, 37, 37, 37, 37, 37, 37, 37, 37, 37, 37, 37, 37, 37, 37, 37, 37, 37, 37, 37, 37, 37, 37, 37, 37, 37, 37, 37, 37, 37, 37, 37, 37, 37, 37, 37, 37, 37, 37, 37, 37, 37, 37, 37, 25, 37, 37, 37, 37, 37, 37, 37, 37, 37, 37, 37, 37, 37, 37, 37, 37, 37, 37, 37, 37, 37, 37, 37, 37, 37, 37, 37, 37, 37, 37, 37, 37, 37, 37, 37, 37, 37, 37, 37, 37, 37, 37, 37, 37, 37, 37, 37, 37, 37, 37, 37, 37, 37, 37, 37, 37, 25, 37, 37, 11, 37, 37, 37, 37, 37, 37, 37, 37, 37, 37, 37, 37, 37, 37, 37, 37, 37, 37, 37, 37, 37, 37, 37, 37, 37, 37, 11, 37, 25, 37, 37, 37, 37, 37, 37, 37, 37, 37, 25]' ``` -------------------------------- ### Fetch subsequence by coordinates Source: https://pyfastx.readthedocs.io/en/latest/_sources/usage.rst.txt Retrieves a subsequence from a FASTA file using specified start and end coordinates. Coordinates are 1-based. Can fetch multiple intervals or specify strand. ```python >>> # get subsequence with start and end position >>> interval = (1, 10) >>> fa.fetch('JZ822577.1', interval) 'CTCTAGAGAT' ``` ```python >>> # get subsequences with a list of start and end position >>> intervals = [(1, 10), (50, 60)] >>> fa.fetch('JZ822577.1', intervals) 'CTCTAGAGATTTTAGTTTGAC' ``` ```python >>> # get subsequences with reverse strand >>> fa.fetch('JZ822577.1', (1, 10), strand='-') 'ATCTCTAGAG' ``` -------------------------------- ### Split FASTA/Q Files Source: https://pyfastx.readthedocs.io/en/latest/_sources/commandline.rst.txt Splits a FASTA or FASTQ file into multiple smaller files based on count or size. ```bash $ pyfastx split -h usage: pyfastx split [-h] (-n int | -c int) [-o str] fastx positional arguments: fastx fasta or fastq file, gzip support optional arguments: -h, --help show this help message and exit -n int split a fasta/q file into N new files with even size -c int split a fasta/q file into multiple files containing the same sequence counts -o str, --out-dir str output directory, default is current folder ``` -------------------------------- ### Get flank sequences Source: https://pyfastx.readthedocs.io/en/latest/_sources/usage.rst.txt Retrieves sequences flanking a specified subsequence. Returns a tuple of left and right flank sequences. Use `use_cache=True` for performance when processing many subsequences from the same sequence. ```python >>> # get flank sequences with length of 20 for subsequence JZ822577:50-60 >>> fa.flank('JZ822577.1', 50, 60, 20) ('TCACTCAGGCTCTTTGTCAT', 'TAGGATATCGAGTATTCAAG') ``` ```python >>> # get flank sequences for a single base or SNP at position 100 >>> fa.flank('JZ822577.1', 100, 100, 20) ('GCTCATCGCTTTTGGTAATC', 'TTGCGGTGCATGCCTTTGCA') ``` ```python >>> # get flank sequences by buffer cache >>> fa.flank('JZ822577.1', 70, 90, flank_length=20, use_cache=True) ('TTTAGTTTGACTAGGATATC', 'TTGGTAATCTTTGCGGTGCA') ``` -------------------------------- ### Extract subsequences Source: https://pyfastx.readthedocs.io/en/latest/commandline.html Displays help for extracting subsequences using regions, BED files, or region files. ```bash $ pyfastx subseq -h usage: pyfastx subseq [-h] [-r str | -b str] [-o str] fastx [region [region ...]] positional arguments: fastx input fasta file, gzip support region format is chr:start-end, start and end position is 1-based, multiple regions were separated by space optional arguments: -h, --help show this help message and exit -r str, --region-file str tab-delimited file, one region per line, both start and end position are 1-based -b str, --bed-file str tab-delimited BED file, 0-based start position and 1-based end position -o str, --out-file str output file, default: output to stdout ``` -------------------------------- ### Get sequence information Source: https://pyfastx.readthedocs.io/en/latest/_sources/usage.rst.txt Accesses various attributes of a sequence object, including its ID, name, description, sequence string, raw header and sequence, length, GC content, and nucleotide composition. ```python >>> s = fa[-1] >>> s JZ840318.1 with length of 134 ``` ```python >>> # get sequence order number in FASTA file >>> # New in pyfastx 0.3.7 >>> s.id 211 ``` ```python >>> # get sequence name >>> s.name 'JZ840318.1' ``` ```python >>> # get sequence description, New in pyfastx 0.3.1 >>> s.description 'R283 cDNA library of flower petals in tree peony by suppression subtractive hybridization Paeonia suffruticosa cDNA, mRNA sequence' ``` ```python >>> # get sequence string >>> s.seq 'ACTGGAGGTTCTTCTTCCTGTGGAAAGTAACTTGTTTTGCCTTCACCTGCCTGTTCTTCACATCAACCTTGTTCCCACACAAAACAATGGGAATGTTCTCACACACCCTGCAGAGATCACGATGCCATGTTGGT' ``` ```python >>> # get sequence raw string, New in pyfastx 0.6.3 >>> print(s.raw) >JZ840318.1 R283 cDNA library of flower petals in tree peony by suppression subtractive hybridization Paeonia suffruticosa cDNA, mRNA sequence ACTGGAGGTTCTTCTTCCTGTGGAAAGTAACTTGTTTTGCCTTCACCTGCCTGTTCTTCACATCAACCTT GTTCCCACACAAAACAATGGGAATGTTCTCACACACCCTGCAGAGATCACGATGCCATGTTGGT ``` ```python >>> # get sequence length >>> len(s) 134 ``` ```python >>> # get GC content if dna sequence >>> s.gc_content 46.26865768432617 ``` ```python >>> # get nucleotide composition if dna sequence >>> s.composition {'A': 31, 'C': 37, 'G': 25, 'T': 41, 'N': 0} ``` -------------------------------- ### pyfastx.Fastq API Source: https://pyfastx.readthedocs.io/en/latest/api_reference.html Provides methods for reading, parsing, and analyzing FASTQ files. ```APIDOC ## Class pyfastx.Fastq ### Description Represents a FASTQ file, allowing access to its reads and associated quality scores. ### Attributes - **`file_name`** (str) - The name of the FASTQ file. - **`size`** (int) - The total number of reads in the FASTQ file. - **`is_gzip`** (bool) - Indicates if the FASTQ file is gzip compressed. - **`gc_content`** (float) - The overall GC content of all sequences in the file. - **`avglen`** (float) - The average length of reads in the file. - **`maxlen`** (int) - The length of the longest read in the file. - **`minlen`** (int) - The length of the shortest read in the file. - **`maxqual`** (int) - The maximum quality score value in the file. - **`minqual`** (int) - The minimum quality score value in the file. - **`composition`** (dict) - A dictionary representing the nucleotide composition of all sequences. - **`phred`** (str) - The Phred encoding type of the quality scores. - **`encoding_type`** (str) - The encoding type of the quality scores. ### Methods - **`build_index()`** - Builds an index for the FASTQ file for faster access. - **`keys()`** - Returns an iterator for the keys (read identifiers) in the FASTQ file. ``` -------------------------------- ### pyfastx.Fastx Class Source: https://pyfastx.readthedocs.io/en/latest/_sources/api_reference.rst.txt A Python binding of kseq.h for iterating over sequences in FASTA/FASTQ files. ```APIDOC ## pyfastx.Fastx Class ### Description A python binding of kseq.h, provide a simple api for iterating over sequences in fasta/q file. ### Parameters #### Path Parameters - **file_name** (str) - Required - input fasta or fastq file path - **format** (str) - Optional - the input file format, can be "fasta" or "fastq", default: "auto", automatically detect the format of sequence file - **uppercase** (bool) - Optional - always output uppercase sequence, only work for fasta file, default: False ### Attributes - **seq** - get read sequence string - **qual** - get read quality ascii string - **quali** - get read quality integer value (ascii - phred), return a list - **comment** - get the raw string (with header, sequence, comment and quality lines) of read as it appeared in file ``` -------------------------------- ### Manage FASTQ Keys Source: https://pyfastx.readthedocs.io/en/latest/usage.html Access sequence identifiers in a FASTQ file. ```python >>> ids = fq.keys() >>> ids contains 800 keys >>> # get count of read >>> len(ids) 800 >>> # get key by index >>> ids[0] 'A00129:183:H77K2DMXX:1:1101:6804:1031' >>> # check key whether in fasta >>> 'A00129:183:H77K2DMXX:1:1101:14416:1031' in ids True ``` -------------------------------- ### pyfastx index Source: https://pyfastx.readthedocs.io/en/latest/commandline.html Builds an index for a FASTA or FASTQ file to enable faster processing. ```APIDOC ## pyfastx index ### Description Builds an index for a FASTA or FASTQ file. The --full option can be used to calculate base composition and speed up GC content calculation. ### Parameters #### Positional Arguments - **fastx** (string) - Required - The input FASTA or FASTQ file (gzip supported). #### Optional Arguments - **-f, --full** (flag) - Optional - Build full index, base composition will be calculated. ```