• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

cokelaer / sequana / 7117302237

06 Dec 2023 04:23PM UTC coverage: 75.482% (+1.8%) from 73.729%
7117302237

push

github

cokelaer
Update version

13709 of 18162 relevant lines covered (75.48%)

2.26 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

35.42
/sequana/scripts/main/fasta.py
1
#  This file is part of Sequana software
2
#
3
#  Copyright (c) 2016-2020 - Sequana Development Team
4
#
5
#  Distributed under the terms of the 3-clause BSD license.
6
#  The full license is in the LICENSE file, distributed with this software.
7
#
8
#  website: https://github.com/sequana/sequana
9
#  documentation: http://sequana.readthedocs.io
10
#
11
##############################################################################
12
import glob
3✔
13
import os
3✔
14
import subprocess
3✔
15
import sys
3✔
16

17
import colorlog
3✔
18
import rich_click as click
3✔
19

20
from sequana.fasta import FastA
3✔
21
from sequana.scripts.utils import CONTEXT_SETTINGS
3✔
22

23
logger = colorlog.getLogger(__name__)
3✔
24

25

26
@click.command(context_settings=CONTEXT_SETTINGS)
3✔
27
@click.argument("filename", type=click.STRING, nargs=-1)
3✔
28
@click.option(
3✔
29
    "-o",
30
    "--output",
31
    help="filename where to save results. to be used with --head, --tail",
32
)
33
@click.option("--count-sequences", is_flag=True)
3✔
34
@click.option("--merge", is_flag=True, help="merge all compressed input fasta files into a single file")
3✔
35
@click.option("--save-contig-name", help="save sequence corresponding to this contig name")
3✔
36
@click.option("--explode", is_flag=True, help="Create a fasta file for each sequence found in the original files")
3✔
37
def fasta(**kwargs):
3✔
38
    """Set of useful utilities for FastA manipulation."""
39
    filenames = kwargs["filename"]
×
40
    # users may provide a wildcards such as "A*gz" or list of files.
41
    if len(filenames) == 1:
×
42
        # if existing files or glob, a glob would give the same answer.
43
        filenames = glob.glob(filenames[0])
×
44
    for filename in filenames:
×
45
        os.path.exists(filename)
×
46

47
    # could be simplified calling count_reads only once
48
    if kwargs["count_sequences"]:
×
49
        for filename in filenames:
×
50
            f = FastA(filename)
×
51
            Nreads = len(f)
×
52
            print(f"Number of reads in {filename}: {Nreads}")
×
53
    elif kwargs["merge"]:
×
54
        # merge all input files (assuming gz extension)
55
        extensions = [filename.split(".")[-1] for filename in filenames]
×
56
        if set(extensions) != set(["gz"]):
×
57
            raise ValueError("Your input FastA files must be zipped")
×
58
        output_filename = kwargs["output"]
×
59
        if output_filename is None:
×
60
            logger.error("You must use --output filename.gz")
×
61
            sys.exit(1)
×
62
        if output_filename.endswith(".gz") is False:
×
63
            raise ValueError("your output file must end in .gz")
×
64
        p1 = subprocess.Popen(["zcat"] + list(filenames), stdout=subprocess.PIPE)
×
65
        fout = open(output_filename, "wb")
×
66
        subprocess.run(["pigz"], stdin=p1.stdout, stdout=fout)
×
67
    elif kwargs["explode"]:
×
68
        for filename in filenames:
×
69
            f = FastA(filename)
×
70
            f.explode()
×
71
    elif kwargs["save_contig_name"]:
×
72
        f = FastA(filename)
×
73
        outname = kwargs["output"]
×
74
        f.save_ctg_to_fasta(kwargs["save_contig_name"], outname)
×
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2026 Coveralls, Inc