def download_all_tables():
    with open("organisms.csv", "r") as f:
        for line in f.readlines()[1:]:
            organism, taxid = line.strip("\n").split(",")
            print("Retrieving %s (taxid %s)" % (organism, taxid))
            target = os.path.join("tables", "%s_%s.csv" % (organism, taxid))
            download_codons_table(taxid=taxid, target_file=target)
Ejemplo n.º 2
0
def test_readme_example():
    table = pct.get_codons_table("b_subtilis_1423")
    assert table["T"]["ACA"] == 0.4
    assert table["*"]["TAA"] == 0.61

    # LOAD ALL TABLES AT ONCE
    codons_tables = pct.get_all_available_codons_tables()
    assert codons_tables["c_elegans_6239"]["L"]["CTA"] == 0.09

    # GET A TABLE DIRECTLY FROM THE INTERNET
    table = pct.download_codons_table(taxid=316407)
    assert table["*"]["TGA"] == 0.29

    with pytest.raises(RuntimeError):
        table = pct.download_codons_table(taxid=000000000)  # does not exist
def test_readme_example():
    table = pct.get_codons_table("b_subtilis_1423")
    assert table['T']['ACA'] == 0.4
    assert table['*']['TAA'] == 0.61

    # LOAD ALL TABLES AT ONCE
    codons_tables = pct.get_all_available_codons_tables()
    assert codons_tables['c_elegans_6239']['L']['CTA'] == 0.09

    # GET A TABLE DIRECTLY FROM THE INTERNET
    table = pct.download_codons_table(taxid=316407)
    assert table['*']['UGA'] == 0.29
Ejemplo n.º 4
0
def codon_tables(taxid, table_path=None):
    """Download the codon use table for the given species and return it as
    a dictionary.

    Returns:
        int: The NCBI taxonomy ID for the supplied species.

    Args:
        taxid (int): NCBI taxonomy ID for the desrired species.
        table_path (str): Defaults to None. Path to a JSON-formatted file representing the
            codon usage to consider. If None, the table is fetched from the internet.

    Raises:
        ValueError: If the NCBI taxonomy ID is not associated with a codon
        usage table, raise a ``ValueError`` informing the user and directing
        them to the NCBI Taxonomy Browser.

    Returns:
        dict{str, float}: A dictionary with codons as keys and the frequency
        that the codon is used to encode its amino acid as values.
    """
    if table_path is None:
        try:
            taxid = int(taxid)
        except ValueError as exc:
            taxid = _tax_id_from_species(taxid, exc)
        logger.info(
            "Downloading host table for NCBI taxonomy ID {}...".format(taxid))
        codon_table_by_aa = pct.download_codons_table(taxid)
    else:
        # load table from disk -- JSON format
        with open(table_path, "r") as table:
            codon_table_by_aa = json.load(table)

    return_dict = {}
    for _, codon_dict in codon_table_by_aa.items():
        for codon, frequency in codon_dict.items():
            return_dict[codon] = frequency
    if not return_dict:
        raise ValueError(
            '"{}" is not a valid host id. '.format(taxid) +
            "Supported hosts (Latin and NCBI taxonomy IDs) can be found at " +
            _tax_id_url)

    return return_dict
def save_codon_table_to_disk(taxid, outfile):
    """Download a codon usage table and save it to disk for future use.

    Args:
        taxid (str): Name of the species to map.
        outfile (str): Full path to the file to write to store the codon
            usage information.
    """
    try:
        taxid = int(taxid)
    except ValueError as exc:
        taxid = _tax_id_from_species(taxid, exc)
    logger.info(
        "Downloading host table for NCBI taxonomy ID {}...".format(taxid))
    codon_table_by_aa = pct.download_codons_table(taxid)
    if not outfile.endswith(".json"):
        logger.warning(
            'Output file "{}" does not have a JSON file extension.'.format(
                outfile))
    with open(outfile, "w") as of:
        json.dump(codon_table_by_aa, of)
> python codon_usage_retriever.py 316407 e_coli_codon_usage.csv

To retrieve codon tables from all organisms in ``organisms.csv`` at once, use:

> python codon_usage_retriever.py all



"""
import sys
import os
from python_codon_tables import download_codons_table


def download_all_tables():
    with open("organisms.csv", "r") as f:
        for line in f.readlines()[1:]:
            organism, taxid = line.strip("\n").split(",")
            print("Retrieving %s (taxid %s)" % (organism, taxid))
            target = os.path.join("tables", "%s_%s.csv" % (organism, taxid))
            download_codons_table(taxid=taxid, target_file=target)


if __name__ == "__main__":
    print(" ".join(sys.argv))
    if sys.argv[1] == "all":
        download_all_tables()
    else:
        download_codons_table(sys.argv[1], sys.argv[2])
def test_download_codon_table(tmpdir):
    table = pct.download_codons_table(taxid=316407)
    assert table['*']['UAG'] == 0.07
    target = os.path.join(str(tmpdir), 'test.csv')
    table = pct.download_codons_table(taxid=316407, target_file=target)
Ejemplo n.º 8
0
def test_download_codon_table(tmpdir):
    table = pct.download_codons_table(taxid=316407)
    assert table["*"]["UAG"] == 0.07
    target = os.path.join(str(tmpdir), "test.csv")
    table = pct.download_codons_table(taxid=316407, target_file=target)