biotite 0.41.1__cp310-cp310-macosx_10_16_arm64.whl
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Potentially problematic release.
This version of biotite might be problematic. Click here for more details.
- biotite/__init__.py +19 -0
- biotite/application/__init__.py +43 -0
- biotite/application/application.py +265 -0
- biotite/application/autodock/__init__.py +12 -0
- biotite/application/autodock/app.py +505 -0
- biotite/application/blast/__init__.py +14 -0
- biotite/application/blast/alignment.py +83 -0
- biotite/application/blast/webapp.py +421 -0
- biotite/application/clustalo/__init__.py +12 -0
- biotite/application/clustalo/app.py +238 -0
- biotite/application/dssp/__init__.py +12 -0
- biotite/application/dssp/app.py +152 -0
- biotite/application/localapp.py +306 -0
- biotite/application/mafft/__init__.py +12 -0
- biotite/application/mafft/app.py +122 -0
- biotite/application/msaapp.py +374 -0
- biotite/application/muscle/__init__.py +13 -0
- biotite/application/muscle/app3.py +254 -0
- biotite/application/muscle/app5.py +171 -0
- biotite/application/sra/__init__.py +18 -0
- biotite/application/sra/app.py +456 -0
- biotite/application/tantan/__init__.py +12 -0
- biotite/application/tantan/app.py +222 -0
- biotite/application/util.py +59 -0
- biotite/application/viennarna/__init__.py +18 -0
- biotite/application/viennarna/rnaalifold.py +304 -0
- biotite/application/viennarna/rnafold.py +269 -0
- biotite/application/viennarna/rnaplot.py +187 -0
- biotite/application/viennarna/util.py +72 -0
- biotite/application/webapp.py +77 -0
- biotite/copyable.py +71 -0
- biotite/database/__init__.py +23 -0
- biotite/database/entrez/__init__.py +15 -0
- biotite/database/entrez/check.py +61 -0
- biotite/database/entrez/dbnames.py +89 -0
- biotite/database/entrez/download.py +223 -0
- biotite/database/entrez/key.py +44 -0
- biotite/database/entrez/query.py +223 -0
- biotite/database/error.py +15 -0
- biotite/database/pubchem/__init__.py +21 -0
- biotite/database/pubchem/download.py +260 -0
- biotite/database/pubchem/error.py +20 -0
- biotite/database/pubchem/query.py +827 -0
- biotite/database/pubchem/throttle.py +99 -0
- biotite/database/rcsb/__init__.py +13 -0
- biotite/database/rcsb/download.py +167 -0
- biotite/database/rcsb/query.py +959 -0
- biotite/database/uniprot/__init__.py +13 -0
- biotite/database/uniprot/check.py +32 -0
- biotite/database/uniprot/download.py +134 -0
- biotite/database/uniprot/query.py +209 -0
- biotite/file.py +251 -0
- biotite/sequence/__init__.py +73 -0
- biotite/sequence/align/__init__.py +49 -0
- biotite/sequence/align/alignment.py +658 -0
- biotite/sequence/align/banded.cpython-310-darwin.so +0 -0
- biotite/sequence/align/banded.pyx +652 -0
- biotite/sequence/align/buckets.py +69 -0
- biotite/sequence/align/cigar.py +434 -0
- biotite/sequence/align/kmeralphabet.cpython-310-darwin.so +0 -0
- biotite/sequence/align/kmeralphabet.pyx +574 -0
- biotite/sequence/align/kmersimilarity.cpython-310-darwin.so +0 -0
- biotite/sequence/align/kmersimilarity.pyx +233 -0
- biotite/sequence/align/kmertable.cpython-310-darwin.so +0 -0
- biotite/sequence/align/kmertable.pyx +3400 -0
- biotite/sequence/align/localgapped.cpython-310-darwin.so +0 -0
- biotite/sequence/align/localgapped.pyx +892 -0
- biotite/sequence/align/localungapped.cpython-310-darwin.so +0 -0
- biotite/sequence/align/localungapped.pyx +279 -0
- biotite/sequence/align/matrix.py +405 -0
- biotite/sequence/align/matrix_data/BLOSUM100.mat +31 -0
- biotite/sequence/align/matrix_data/BLOSUM30.mat +31 -0
- biotite/sequence/align/matrix_data/BLOSUM35.mat +31 -0
- biotite/sequence/align/matrix_data/BLOSUM40.mat +31 -0
- biotite/sequence/align/matrix_data/BLOSUM45.mat +31 -0
- biotite/sequence/align/matrix_data/BLOSUM50.mat +31 -0
- biotite/sequence/align/matrix_data/BLOSUM50_13p.mat +25 -0
- biotite/sequence/align/matrix_data/BLOSUM50_14.3.mat +25 -0
- biotite/sequence/align/matrix_data/BLOSUM50_5.0.mat +25 -0
- biotite/sequence/align/matrix_data/BLOSUM55.mat +31 -0
- biotite/sequence/align/matrix_data/BLOSUM60.mat +31 -0
- biotite/sequence/align/matrix_data/BLOSUM62.mat +31 -0
- biotite/sequence/align/matrix_data/BLOSUM62_13p.mat +25 -0
- biotite/sequence/align/matrix_data/BLOSUM62_14.3.mat +25 -0
- biotite/sequence/align/matrix_data/BLOSUM62_5.0.mat +25 -0
- biotite/sequence/align/matrix_data/BLOSUM65.mat +31 -0
- biotite/sequence/align/matrix_data/BLOSUM70.mat +31 -0
- biotite/sequence/align/matrix_data/BLOSUM75.mat +31 -0
- biotite/sequence/align/matrix_data/BLOSUM80.mat +31 -0
- biotite/sequence/align/matrix_data/BLOSUM85.mat +31 -0
- biotite/sequence/align/matrix_data/BLOSUM90.mat +31 -0
- biotite/sequence/align/matrix_data/BLOSUMN.mat +31 -0
- biotite/sequence/align/matrix_data/CorBLOSUM49_5.0.mat +25 -0
- biotite/sequence/align/matrix_data/CorBLOSUM57_13p.mat +25 -0
- biotite/sequence/align/matrix_data/CorBLOSUM57_14.3.mat +25 -0
- biotite/sequence/align/matrix_data/CorBLOSUM61_5.0.mat +25 -0
- biotite/sequence/align/matrix_data/CorBLOSUM66_13p.mat +25 -0
- biotite/sequence/align/matrix_data/CorBLOSUM67_14.3.mat +25 -0
- biotite/sequence/align/matrix_data/DAYHOFF.mat +32 -0
- biotite/sequence/align/matrix_data/GONNET.mat +26 -0
- biotite/sequence/align/matrix_data/IDENTITY.mat +25 -0
- biotite/sequence/align/matrix_data/MATCH.mat +25 -0
- biotite/sequence/align/matrix_data/NUC.mat +25 -0
- biotite/sequence/align/matrix_data/PAM10.mat +34 -0
- biotite/sequence/align/matrix_data/PAM100.mat +34 -0
- biotite/sequence/align/matrix_data/PAM110.mat +34 -0
- biotite/sequence/align/matrix_data/PAM120.mat +34 -0
- biotite/sequence/align/matrix_data/PAM130.mat +34 -0
- biotite/sequence/align/matrix_data/PAM140.mat +34 -0
- biotite/sequence/align/matrix_data/PAM150.mat +34 -0
- biotite/sequence/align/matrix_data/PAM160.mat +34 -0
- biotite/sequence/align/matrix_data/PAM170.mat +34 -0
- biotite/sequence/align/matrix_data/PAM180.mat +34 -0
- biotite/sequence/align/matrix_data/PAM190.mat +34 -0
- biotite/sequence/align/matrix_data/PAM20.mat +34 -0
- biotite/sequence/align/matrix_data/PAM200.mat +34 -0
- biotite/sequence/align/matrix_data/PAM210.mat +34 -0
- biotite/sequence/align/matrix_data/PAM220.mat +34 -0
- biotite/sequence/align/matrix_data/PAM230.mat +34 -0
- biotite/sequence/align/matrix_data/PAM240.mat +34 -0
- biotite/sequence/align/matrix_data/PAM250.mat +34 -0
- biotite/sequence/align/matrix_data/PAM260.mat +34 -0
- biotite/sequence/align/matrix_data/PAM270.mat +34 -0
- biotite/sequence/align/matrix_data/PAM280.mat +34 -0
- biotite/sequence/align/matrix_data/PAM290.mat +34 -0
- biotite/sequence/align/matrix_data/PAM30.mat +34 -0
- biotite/sequence/align/matrix_data/PAM300.mat +34 -0
- biotite/sequence/align/matrix_data/PAM310.mat +34 -0
- biotite/sequence/align/matrix_data/PAM320.mat +34 -0
- biotite/sequence/align/matrix_data/PAM330.mat +34 -0
- biotite/sequence/align/matrix_data/PAM340.mat +34 -0
- biotite/sequence/align/matrix_data/PAM350.mat +34 -0
- biotite/sequence/align/matrix_data/PAM360.mat +34 -0
- biotite/sequence/align/matrix_data/PAM370.mat +34 -0
- biotite/sequence/align/matrix_data/PAM380.mat +34 -0
- biotite/sequence/align/matrix_data/PAM390.mat +34 -0
- biotite/sequence/align/matrix_data/PAM40.mat +34 -0
- biotite/sequence/align/matrix_data/PAM400.mat +34 -0
- biotite/sequence/align/matrix_data/PAM410.mat +34 -0
- biotite/sequence/align/matrix_data/PAM420.mat +34 -0
- biotite/sequence/align/matrix_data/PAM430.mat +34 -0
- biotite/sequence/align/matrix_data/PAM440.mat +34 -0
- biotite/sequence/align/matrix_data/PAM450.mat +34 -0
- biotite/sequence/align/matrix_data/PAM460.mat +34 -0
- biotite/sequence/align/matrix_data/PAM470.mat +34 -0
- biotite/sequence/align/matrix_data/PAM480.mat +34 -0
- biotite/sequence/align/matrix_data/PAM490.mat +34 -0
- biotite/sequence/align/matrix_data/PAM50.mat +34 -0
- biotite/sequence/align/matrix_data/PAM500.mat +34 -0
- biotite/sequence/align/matrix_data/PAM60.mat +34 -0
- biotite/sequence/align/matrix_data/PAM70.mat +34 -0
- biotite/sequence/align/matrix_data/PAM80.mat +34 -0
- biotite/sequence/align/matrix_data/PAM90.mat +34 -0
- biotite/sequence/align/matrix_data/RBLOSUM52_5.0.mat +25 -0
- biotite/sequence/align/matrix_data/RBLOSUM59_13p.mat +25 -0
- biotite/sequence/align/matrix_data/RBLOSUM59_14.3.mat +25 -0
- biotite/sequence/align/matrix_data/RBLOSUM64_5.0.mat +25 -0
- biotite/sequence/align/matrix_data/RBLOSUM69_13p.mat +25 -0
- biotite/sequence/align/matrix_data/RBLOSUM69_14.3.mat +25 -0
- biotite/sequence/align/multiple.cpython-310-darwin.so +0 -0
- biotite/sequence/align/multiple.pyx +620 -0
- biotite/sequence/align/pairwise.cpython-310-darwin.so +0 -0
- biotite/sequence/align/pairwise.pyx +587 -0
- biotite/sequence/align/permutation.cpython-310-darwin.so +0 -0
- biotite/sequence/align/permutation.pyx +305 -0
- biotite/sequence/align/primes.txt +821 -0
- biotite/sequence/align/selector.cpython-310-darwin.so +0 -0
- biotite/sequence/align/selector.pyx +956 -0
- biotite/sequence/align/statistics.py +265 -0
- biotite/sequence/align/tracetable.cpython-310-darwin.so +0 -0
- biotite/sequence/align/tracetable.pxd +64 -0
- biotite/sequence/align/tracetable.pyx +370 -0
- biotite/sequence/alphabet.py +566 -0
- biotite/sequence/annotation.py +829 -0
- biotite/sequence/codec.cpython-310-darwin.so +0 -0
- biotite/sequence/codec.pyx +155 -0
- biotite/sequence/codon.py +466 -0
- biotite/sequence/codon_tables.txt +202 -0
- biotite/sequence/graphics/__init__.py +33 -0
- biotite/sequence/graphics/alignment.py +1034 -0
- biotite/sequence/graphics/color_schemes/autumn.json +51 -0
- biotite/sequence/graphics/color_schemes/blossom.json +51 -0
- biotite/sequence/graphics/color_schemes/clustalx_dna.json +11 -0
- biotite/sequence/graphics/color_schemes/clustalx_protein.json +28 -0
- biotite/sequence/graphics/color_schemes/flower.json +51 -0
- biotite/sequence/graphics/color_schemes/jalview_buried.json +31 -0
- biotite/sequence/graphics/color_schemes/jalview_hydrophobicity.json +31 -0
- biotite/sequence/graphics/color_schemes/jalview_prop_helix.json +31 -0
- biotite/sequence/graphics/color_schemes/jalview_prop_strand.json +31 -0
- biotite/sequence/graphics/color_schemes/jalview_prop_turn.json +31 -0
- biotite/sequence/graphics/color_schemes/jalview_taylor.json +28 -0
- biotite/sequence/graphics/color_schemes/jalview_zappo.json +28 -0
- biotite/sequence/graphics/color_schemes/ocean.json +51 -0
- biotite/sequence/graphics/color_schemes/pb_flower.json +39 -0
- biotite/sequence/graphics/color_schemes/rainbow_dna.json +11 -0
- biotite/sequence/graphics/color_schemes/rainbow_protein.json +30 -0
- biotite/sequence/graphics/color_schemes/spring.json +51 -0
- biotite/sequence/graphics/color_schemes/sunset.json +51 -0
- biotite/sequence/graphics/color_schemes/wither.json +51 -0
- biotite/sequence/graphics/colorschemes.py +139 -0
- biotite/sequence/graphics/dendrogram.py +184 -0
- biotite/sequence/graphics/features.py +510 -0
- biotite/sequence/graphics/logo.py +110 -0
- biotite/sequence/graphics/plasmid.py +661 -0
- biotite/sequence/io/__init__.py +12 -0
- biotite/sequence/io/fasta/__init__.py +22 -0
- biotite/sequence/io/fasta/convert.py +273 -0
- biotite/sequence/io/fasta/file.py +278 -0
- biotite/sequence/io/fastq/__init__.py +19 -0
- biotite/sequence/io/fastq/convert.py +120 -0
- biotite/sequence/io/fastq/file.py +551 -0
- biotite/sequence/io/genbank/__init__.py +17 -0
- biotite/sequence/io/genbank/annotation.py +277 -0
- biotite/sequence/io/genbank/file.py +575 -0
- biotite/sequence/io/genbank/metadata.py +324 -0
- biotite/sequence/io/genbank/sequence.py +172 -0
- biotite/sequence/io/general.py +192 -0
- biotite/sequence/io/gff/__init__.py +26 -0
- biotite/sequence/io/gff/convert.py +133 -0
- biotite/sequence/io/gff/file.py +434 -0
- biotite/sequence/phylo/__init__.py +36 -0
- biotite/sequence/phylo/nj.cpython-310-darwin.so +0 -0
- biotite/sequence/phylo/nj.pyx +221 -0
- biotite/sequence/phylo/tree.cpython-310-darwin.so +0 -0
- biotite/sequence/phylo/tree.pyx +1169 -0
- biotite/sequence/phylo/upgma.cpython-310-darwin.so +0 -0
- biotite/sequence/phylo/upgma.pyx +164 -0
- biotite/sequence/profile.py +456 -0
- biotite/sequence/search.py +116 -0
- biotite/sequence/seqtypes.py +556 -0
- biotite/sequence/sequence.py +374 -0
- biotite/structure/__init__.py +132 -0
- biotite/structure/atoms.py +1455 -0
- biotite/structure/basepairs.py +1415 -0
- biotite/structure/bonds.cpython-310-darwin.so +0 -0
- biotite/structure/bonds.pyx +1933 -0
- biotite/structure/box.py +592 -0
- biotite/structure/celllist.cpython-310-darwin.so +0 -0
- biotite/structure/celllist.pyx +849 -0
- biotite/structure/chains.py +298 -0
- biotite/structure/charges.cpython-310-darwin.so +0 -0
- biotite/structure/charges.pyx +520 -0
- biotite/structure/compare.py +274 -0
- biotite/structure/density.py +114 -0
- biotite/structure/dotbracket.py +216 -0
- biotite/structure/error.py +31 -0
- biotite/structure/filter.py +585 -0
- biotite/structure/geometry.py +697 -0
- biotite/structure/graphics/__init__.py +13 -0
- biotite/structure/graphics/atoms.py +226 -0
- biotite/structure/graphics/rna.py +282 -0
- biotite/structure/hbond.py +409 -0
- biotite/structure/info/__init__.py +25 -0
- biotite/structure/info/atom_masses.json +121 -0
- biotite/structure/info/atoms.py +82 -0
- biotite/structure/info/bonds.py +145 -0
- biotite/structure/info/ccd/README.rst +8 -0
- biotite/structure/info/ccd/amino_acids.txt +1663 -0
- biotite/structure/info/ccd/carbohydrates.txt +1135 -0
- biotite/structure/info/ccd/components.bcif +0 -0
- biotite/structure/info/ccd/nucleotides.txt +798 -0
- biotite/structure/info/ccd.py +95 -0
- biotite/structure/info/groups.py +90 -0
- biotite/structure/info/masses.py +123 -0
- biotite/structure/info/misc.py +144 -0
- biotite/structure/info/radii.py +197 -0
- biotite/structure/info/standardize.py +196 -0
- biotite/structure/integrity.py +268 -0
- biotite/structure/io/__init__.py +30 -0
- biotite/structure/io/ctab.py +72 -0
- biotite/structure/io/dcd/__init__.py +13 -0
- biotite/structure/io/dcd/file.py +65 -0
- biotite/structure/io/general.py +257 -0
- biotite/structure/io/gro/__init__.py +14 -0
- biotite/structure/io/gro/file.py +343 -0
- biotite/structure/io/mmtf/__init__.py +21 -0
- biotite/structure/io/mmtf/assembly.py +214 -0
- biotite/structure/io/mmtf/convertarray.cpython-310-darwin.so +0 -0
- biotite/structure/io/mmtf/convertarray.pyx +341 -0
- biotite/structure/io/mmtf/convertfile.cpython-310-darwin.so +0 -0
- biotite/structure/io/mmtf/convertfile.pyx +501 -0
- biotite/structure/io/mmtf/decode.cpython-310-darwin.so +0 -0
- biotite/structure/io/mmtf/decode.pyx +152 -0
- biotite/structure/io/mmtf/encode.cpython-310-darwin.so +0 -0
- biotite/structure/io/mmtf/encode.pyx +183 -0
- biotite/structure/io/mmtf/file.py +233 -0
- biotite/structure/io/mol/__init__.py +20 -0
- biotite/structure/io/mol/convert.py +115 -0
- biotite/structure/io/mol/ctab.py +414 -0
- biotite/structure/io/mol/header.py +116 -0
- biotite/structure/io/mol/mol.py +193 -0
- biotite/structure/io/mol/sdf.py +916 -0
- biotite/structure/io/netcdf/__init__.py +13 -0
- biotite/structure/io/netcdf/file.py +63 -0
- biotite/structure/io/npz/__init__.py +20 -0
- biotite/structure/io/npz/file.py +152 -0
- biotite/structure/io/pdb/__init__.py +20 -0
- biotite/structure/io/pdb/convert.py +293 -0
- biotite/structure/io/pdb/file.py +1240 -0
- biotite/structure/io/pdb/hybrid36.cpython-310-darwin.so +0 -0
- biotite/structure/io/pdb/hybrid36.pyx +242 -0
- biotite/structure/io/pdbqt/__init__.py +15 -0
- biotite/structure/io/pdbqt/convert.py +107 -0
- biotite/structure/io/pdbqt/file.py +640 -0
- biotite/structure/io/pdbx/__init__.py +23 -0
- biotite/structure/io/pdbx/bcif.py +648 -0
- biotite/structure/io/pdbx/cif.py +1032 -0
- biotite/structure/io/pdbx/component.py +246 -0
- biotite/structure/io/pdbx/convert.py +1597 -0
- biotite/structure/io/pdbx/encoding.cpython-310-darwin.so +0 -0
- biotite/structure/io/pdbx/encoding.pyx +950 -0
- biotite/structure/io/pdbx/legacy.py +267 -0
- biotite/structure/io/tng/__init__.py +13 -0
- biotite/structure/io/tng/file.py +46 -0
- biotite/structure/io/trajfile.py +710 -0
- biotite/structure/io/trr/__init__.py +13 -0
- biotite/structure/io/trr/file.py +46 -0
- biotite/structure/io/xtc/__init__.py +13 -0
- biotite/structure/io/xtc/file.py +46 -0
- biotite/structure/mechanics.py +75 -0
- biotite/structure/molecules.py +353 -0
- biotite/structure/pseudoknots.py +642 -0
- biotite/structure/rdf.py +243 -0
- biotite/structure/repair.py +253 -0
- biotite/structure/residues.py +562 -0
- biotite/structure/resutil.py +178 -0
- biotite/structure/sasa.cpython-310-darwin.so +0 -0
- biotite/structure/sasa.pyx +322 -0
- biotite/structure/sequence.py +112 -0
- biotite/structure/sse.py +327 -0
- biotite/structure/superimpose.py +727 -0
- biotite/structure/transform.py +504 -0
- biotite/structure/util.py +98 -0
- biotite/temp.py +86 -0
- biotite/version.py +16 -0
- biotite/visualize.py +251 -0
- biotite-0.41.1.dist-info/METADATA +187 -0
- biotite-0.41.1.dist-info/RECORD +340 -0
- biotite-0.41.1.dist-info/WHEEL +4 -0
- biotite-0.41.1.dist-info/licenses/LICENSE.rst +30 -0
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
# This source code is part of the Biotite package and is distributed
|
|
2
|
+
# under the 3-Clause BSD License. Please see 'LICENSE.rst' for further
|
|
3
|
+
# information.
|
|
4
|
+
|
|
5
|
+
"""
|
|
6
|
+
A subpackage for downloading files from the UniProt.
|
|
7
|
+
"""
|
|
8
|
+
|
|
9
|
+
__name__ = "biotite.database.uniprot"
|
|
10
|
+
__author__ = "Maximilian Greil"
|
|
11
|
+
|
|
12
|
+
from .download import *
|
|
13
|
+
from .query import *
|
|
@@ -0,0 +1,32 @@
|
|
|
1
|
+
# This source code is part of the Biotite package and is distributed
|
|
2
|
+
# under the 3-Clause BSD License. Please see 'LICENSE.rst' for further
|
|
3
|
+
# information.
|
|
4
|
+
|
|
5
|
+
__name__ = "biotite.database.uniprot"
|
|
6
|
+
__author__ = "Maximilian Greil"
|
|
7
|
+
__all__ = ["assert_valid_response"]
|
|
8
|
+
|
|
9
|
+
from ..error import RequestError
|
|
10
|
+
|
|
11
|
+
|
|
12
|
+
# Taken from https://www.uniprot.org/help/api_retrieve_entries
|
|
13
|
+
def assert_valid_response(response_status_code):
|
|
14
|
+
"""
|
|
15
|
+
Checks whether the response is valid.
|
|
16
|
+
|
|
17
|
+
Parameters
|
|
18
|
+
----------
|
|
19
|
+
response_status_code: int
|
|
20
|
+
Status code of request.get.
|
|
21
|
+
"""
|
|
22
|
+
if response_status_code == 400:
|
|
23
|
+
raise RequestError("Bad request. There is a problem with your input.")
|
|
24
|
+
elif response_status_code == 404:
|
|
25
|
+
raise RequestError("Not found. The resource you requested doesn't exist.")
|
|
26
|
+
elif response_status_code == 410:
|
|
27
|
+
raise RequestError("Gone. The resource you requested was removed.")
|
|
28
|
+
elif response_status_code == 500:
|
|
29
|
+
raise RequestError(
|
|
30
|
+
"Internal server error. Most likely a temporary problem, but if the problem persists please contact UniProt team.")
|
|
31
|
+
elif response_status_code == 503:
|
|
32
|
+
raise RequestError("Service not available. The server is being updated, try again later.")
|
|
@@ -0,0 +1,134 @@
|
|
|
1
|
+
# This source code is part of the Biotite package and is distributed
|
|
2
|
+
# under the 3-Clause BSD License. Please see 'LICENSE.rst' for further
|
|
3
|
+
# information.
|
|
4
|
+
|
|
5
|
+
__name__ = "biotite.database.uniprot"
|
|
6
|
+
__author__ = "Maximilian Greil"
|
|
7
|
+
__all__ = ["fetch"]
|
|
8
|
+
|
|
9
|
+
from os.path import isdir, isfile, join, getsize
|
|
10
|
+
import os
|
|
11
|
+
import io
|
|
12
|
+
import requests
|
|
13
|
+
from .check import assert_valid_response
|
|
14
|
+
|
|
15
|
+
_fetch_url = "https://rest.uniprot.org/"
|
|
16
|
+
|
|
17
|
+
|
|
18
|
+
def _get_database_name(id):
|
|
19
|
+
"""
|
|
20
|
+
Get the correct UniProt database from the ID of the file to be downloaded.
|
|
21
|
+
|
|
22
|
+
Parameters
|
|
23
|
+
----------
|
|
24
|
+
id: str
|
|
25
|
+
ID of the file to be downloaded.
|
|
26
|
+
|
|
27
|
+
Returns
|
|
28
|
+
-------
|
|
29
|
+
name : str
|
|
30
|
+
E-utility UniProt database name.
|
|
31
|
+
"""
|
|
32
|
+
if id[:3] == "UPI":
|
|
33
|
+
return "uniparc"
|
|
34
|
+
elif id[:6] == "UniRef":
|
|
35
|
+
return "uniref"
|
|
36
|
+
return "uniprotkb"
|
|
37
|
+
|
|
38
|
+
|
|
39
|
+
def fetch(ids, format, target_path=None,
|
|
40
|
+
overwrite=False, verbose=False):
|
|
41
|
+
"""
|
|
42
|
+
Download files from the UniProt in various formats.
|
|
43
|
+
|
|
44
|
+
Available databases are UniProtKB, UniRef and UniParc.
|
|
45
|
+
|
|
46
|
+
This function requires an internet connection.
|
|
47
|
+
|
|
48
|
+
Parameters
|
|
49
|
+
----------
|
|
50
|
+
ids : str or iterable object of str
|
|
51
|
+
A single ID or a list of IDs of the file(s)
|
|
52
|
+
to be downloaded.
|
|
53
|
+
format : {'fasta', 'gff', 'txt', 'xml', 'rdf', 'tab'}
|
|
54
|
+
The format of the files to be downloaded.
|
|
55
|
+
target_path : str, optional
|
|
56
|
+
The target directory of the downloaded files.
|
|
57
|
+
By default, the file content is stored in a file-like object
|
|
58
|
+
(`StringIO` or `BytesIO`, respectively).
|
|
59
|
+
overwrite : bool, optional
|
|
60
|
+
If true, existing files will be overwritten. Otherwise the
|
|
61
|
+
respective file will only be downloaded if the file does not
|
|
62
|
+
exist yet in the specified target directory or if the file is
|
|
63
|
+
empty. (Default: False)
|
|
64
|
+
verbose: bool, optional
|
|
65
|
+
If true, the function will output the download progress.
|
|
66
|
+
(Default: False)
|
|
67
|
+
|
|
68
|
+
Returns
|
|
69
|
+
-------
|
|
70
|
+
files : str or StringIO or BytesIO or list of (str or StringIO or BytesIO)
|
|
71
|
+
The file path(s) to the downloaded files.
|
|
72
|
+
If a single string (a single ID) was given in `ids`,
|
|
73
|
+
a single string is returned. If a list (or other iterable
|
|
74
|
+
object) was given, a list of strings is returned.
|
|
75
|
+
If no `target_path` was given, the file contents are stored in
|
|
76
|
+
either `StringIO` or `BytesIO` objects.
|
|
77
|
+
|
|
78
|
+
Examples
|
|
79
|
+
--------
|
|
80
|
+
|
|
81
|
+
>>> import os.path
|
|
82
|
+
>>> file = fetch("P12345", "fasta", path_to_directory)
|
|
83
|
+
>>> print(os.path.basename(file))
|
|
84
|
+
P12345.fasta
|
|
85
|
+
>>> files = fetch(["P12345", "Q8K9I1"], "fasta", path_to_directory)
|
|
86
|
+
>>> print([os.path.basename(file) for file in files])
|
|
87
|
+
['P12345.fasta', 'Q8K9I1.fasta']
|
|
88
|
+
"""
|
|
89
|
+
# If only a single ID is present,
|
|
90
|
+
# put it into a single element list
|
|
91
|
+
if isinstance(ids, str):
|
|
92
|
+
ids = [ids]
|
|
93
|
+
single_element = True
|
|
94
|
+
else:
|
|
95
|
+
single_element = False
|
|
96
|
+
# Create the target folder, if not existing
|
|
97
|
+
if target_path is not None and not isdir(target_path):
|
|
98
|
+
os.makedirs(target_path)
|
|
99
|
+
files = []
|
|
100
|
+
for i, id in enumerate(ids):
|
|
101
|
+
db_name = _get_database_name(id)
|
|
102
|
+
# Verbose output
|
|
103
|
+
if verbose:
|
|
104
|
+
print(f"Fetching file {i + 1:d} / {len(ids):d} ({id})...",
|
|
105
|
+
end="\r")
|
|
106
|
+
# Fetch file from database
|
|
107
|
+
if target_path is not None:
|
|
108
|
+
file = join(target_path, id + "." + format)
|
|
109
|
+
else:
|
|
110
|
+
# 'file = None' -> store content in a file-like object
|
|
111
|
+
file = None
|
|
112
|
+
if file is None \
|
|
113
|
+
or not isfile(file) \
|
|
114
|
+
or getsize(file) == 0 \
|
|
115
|
+
or overwrite:
|
|
116
|
+
if format in ["fasta", "gff", "txt", "xml", "rdf", "tab"]:
|
|
117
|
+
r = requests.get(_fetch_url + db_name + "/" + id + "." + format)
|
|
118
|
+
content = r.text
|
|
119
|
+
assert_valid_response(r.status_code)
|
|
120
|
+
else:
|
|
121
|
+
raise ValueError(f"Format '{format}' is not supported")
|
|
122
|
+
if file is None:
|
|
123
|
+
file = io.StringIO(content)
|
|
124
|
+
else:
|
|
125
|
+
with open(file, "w+") as f:
|
|
126
|
+
f.write(content)
|
|
127
|
+
files.append(file)
|
|
128
|
+
if verbose:
|
|
129
|
+
print("\nDone")
|
|
130
|
+
# If input was a single ID, return only a single path
|
|
131
|
+
if single_element:
|
|
132
|
+
return files[0]
|
|
133
|
+
else:
|
|
134
|
+
return files
|
|
@@ -0,0 +1,209 @@
|
|
|
1
|
+
# This source code is part of the Biotite package and is distributed
|
|
2
|
+
# under the 3-Clause BSD License. Please see 'LICENSE.rst' for further
|
|
3
|
+
# information.
|
|
4
|
+
|
|
5
|
+
__name__ = "biotite.database.uniprot"
|
|
6
|
+
__author__ = "Maximilian Greil"
|
|
7
|
+
__all__ = ["Query", "SimpleQuery", "CompositeQuery", "search"]
|
|
8
|
+
|
|
9
|
+
import requests
|
|
10
|
+
import abc
|
|
11
|
+
from .check import assert_valid_response
|
|
12
|
+
|
|
13
|
+
|
|
14
|
+
_base_url = "https://rest.uniprot.org/uniprotkb/search/"
|
|
15
|
+
|
|
16
|
+
|
|
17
|
+
class Query(metaclass=abc.ABCMeta):
|
|
18
|
+
"""
|
|
19
|
+
Base class for a wrapper around a search term
|
|
20
|
+
for the UniProt search service.
|
|
21
|
+
"""
|
|
22
|
+
|
|
23
|
+
def __init__(self):
|
|
24
|
+
pass
|
|
25
|
+
|
|
26
|
+
@abc.abstractmethod
|
|
27
|
+
def __str__(self):
|
|
28
|
+
pass
|
|
29
|
+
|
|
30
|
+
def __or__(self, operand):
|
|
31
|
+
return CompositeQuery("OR", self, operand)
|
|
32
|
+
|
|
33
|
+
def __and__(self, operand):
|
|
34
|
+
return CompositeQuery("AND", self, operand)
|
|
35
|
+
|
|
36
|
+
def __xor__(self, operand):
|
|
37
|
+
return CompositeQuery("NOT", self, operand)
|
|
38
|
+
|
|
39
|
+
|
|
40
|
+
class CompositeQuery(Query):
|
|
41
|
+
"""
|
|
42
|
+
A representation of an composite query
|
|
43
|
+
for the UniProt search service.
|
|
44
|
+
|
|
45
|
+
A composite query is a combination of two other queries,
|
|
46
|
+
combined either with an 'AND', 'OR' or 'NOT' operator.
|
|
47
|
+
|
|
48
|
+
Usually the user does not create instances of this class directly,
|
|
49
|
+
but :class:`Query` instances are combined with
|
|
50
|
+
``|`` (OR), ``&`` (AND) or ``^`` (NOT).
|
|
51
|
+
|
|
52
|
+
Parameters
|
|
53
|
+
----------
|
|
54
|
+
operator: str, {"AND", "OR", "NOT"}
|
|
55
|
+
The combination operator.
|
|
56
|
+
queries : iterable object of SimpleQuery
|
|
57
|
+
The queries to be combined.
|
|
58
|
+
"""
|
|
59
|
+
|
|
60
|
+
def __init__(self, operator, query1, query2):
|
|
61
|
+
super().__init__()
|
|
62
|
+
self._op = operator
|
|
63
|
+
self._q1 = query1
|
|
64
|
+
self._q2 = query2
|
|
65
|
+
|
|
66
|
+
def __str__(self):
|
|
67
|
+
return "{:} {:} {:}".format(str(self._q1), self._op, str(self._q2))
|
|
68
|
+
|
|
69
|
+
|
|
70
|
+
def _check_brackets(term):
|
|
71
|
+
"""
|
|
72
|
+
Check if term contains correct number of round brackets and square brackets.
|
|
73
|
+
|
|
74
|
+
Parameters
|
|
75
|
+
----------
|
|
76
|
+
term: str
|
|
77
|
+
The search term.
|
|
78
|
+
|
|
79
|
+
Returns
|
|
80
|
+
-------
|
|
81
|
+
bool
|
|
82
|
+
True if term contains correct number of round brackets and square brackets, otherwise False.
|
|
83
|
+
"""
|
|
84
|
+
square_count = 0
|
|
85
|
+
round_count = 0
|
|
86
|
+
for i in term:
|
|
87
|
+
if i == "[":
|
|
88
|
+
square_count += 1
|
|
89
|
+
elif i == "]":
|
|
90
|
+
square_count -= 1
|
|
91
|
+
if i == "(":
|
|
92
|
+
round_count += 1
|
|
93
|
+
elif i == ")":
|
|
94
|
+
round_count -= 1
|
|
95
|
+
if square_count < 0:
|
|
96
|
+
return False
|
|
97
|
+
if round_count < 0:
|
|
98
|
+
return False
|
|
99
|
+
return square_count == 0 and round_count == 0
|
|
100
|
+
|
|
101
|
+
|
|
102
|
+
class SimpleQuery(Query):
|
|
103
|
+
"""
|
|
104
|
+
A simple query for the UniProt search service without
|
|
105
|
+
combination via 'AND', 'OR' or 'NOT'. A query consists of a search
|
|
106
|
+
term and an optional field.
|
|
107
|
+
|
|
108
|
+
A list of available search fields with description can be found
|
|
109
|
+
`here <https://www.uniprot.org/help/query-fields>`_.
|
|
110
|
+
|
|
111
|
+
Parameters
|
|
112
|
+
----------
|
|
113
|
+
field : str
|
|
114
|
+
The field to search the term in.
|
|
115
|
+
The list of possible fields and the required search term
|
|
116
|
+
formatting can be found
|
|
117
|
+
`here <https://www.uniprot.org/help/query-fields>`_.
|
|
118
|
+
term: str
|
|
119
|
+
The search term.
|
|
120
|
+
"""
|
|
121
|
+
|
|
122
|
+
# Field identifiers are taken from
|
|
123
|
+
# https://www.uniprot.org/help/query-fields
|
|
124
|
+
_fields = [
|
|
125
|
+
"accession", "active", "ft_init_met", "ft_signal", "ft_transit", "ft_propep", "ft_chain", "ft_peptide",
|
|
126
|
+
"ft_topo_dom", "ft_transmem", "ft_intramem", "ft_domain", "ft_repeat", "ft_zn_fing", "ft_dna_bind",
|
|
127
|
+
"ft_region", "ft_coiled", "ft_motif", "ft_compbias", "ft_act_site", "ft_binding", "ft_site", "ft_non_std",
|
|
128
|
+
"ft_mod_res", "ft_lipid", "ft_carbohyd", "ft_disulfid", "ft_crosslnk", "ft_var_seq", "ft_variant",
|
|
129
|
+
"ft_mutagen", "ft_unsure", "ft_conflict", "ft_non_cons", "ft_non_ter", "ft_helix", "ft_turn", "ft_strand",
|
|
130
|
+
"lit_author", "protein_name", "chebi", "citation", "uniref_cluster_90", "xrefcount_pdb", "date_created",
|
|
131
|
+
"database", "xref", "ec", "cc_function", "cc_catalytic_activity", "cc_cofactor", "cc_activity_regulation",
|
|
132
|
+
"cc_biophysicochemical_properties", "cc_subunit", "cc_pathway", "cc_scl_term", "cc_tissue_specificity",
|
|
133
|
+
"cc_developmental_stage", "cc_induction", "cc_domain", "cc_ptm cc_rna_editing", "cc_mass_spectrometry",
|
|
134
|
+
"cc_polymorphism", "cc_disease", "cc_disruption_phenotype", "cc_allergen", "cc_toxic_dose", "cc_biotechnology",
|
|
135
|
+
"cc_pharmaceutical", "cc_miscellaneous", "cc_similarity", "cc_caution", "cc_sequence_caution",
|
|
136
|
+
"existence", "family", "fragment", "gene", "gene_exact", "go", "virus_host_name", "virus_host_id",
|
|
137
|
+
"accession_id", "inchikey", "protein_name", "interactor", "keyword", "length", "lineage", "mass",
|
|
138
|
+
"cc_mass_spectrometry", "date_modified", "protein_name", "organelle", "organism_name", "organism_id",
|
|
139
|
+
"plasmid", "proteome", "proteomecomponent", "sec_acc", "reviewed", "scope", "sequence",
|
|
140
|
+
"date_sequence_modified", "strain", "taxonomy_name", "taxonomy_id", "tissue", "cc_webresource"
|
|
141
|
+
]
|
|
142
|
+
|
|
143
|
+
def __init__(self, field, term):
|
|
144
|
+
super().__init__()
|
|
145
|
+
if field not in SimpleQuery._fields:
|
|
146
|
+
raise ValueError(f"Unknown field identifier '{field}'")
|
|
147
|
+
if not _check_brackets(term):
|
|
148
|
+
raise ValueError(
|
|
149
|
+
f"Query term contains illegal number of round brackets ( ) and/or square brackets [ ]"
|
|
150
|
+
)
|
|
151
|
+
for invalid_string in \
|
|
152
|
+
['"', "AND", "OR", "NOT", "\t", "\n"]:
|
|
153
|
+
if invalid_string in term:
|
|
154
|
+
raise ValueError(
|
|
155
|
+
f"Query contains illegal term {invalid_string}"
|
|
156
|
+
)
|
|
157
|
+
if " " in term:
|
|
158
|
+
term = f'"{term}"'
|
|
159
|
+
self._field = field
|
|
160
|
+
self._term = term
|
|
161
|
+
|
|
162
|
+
def __str__(self):
|
|
163
|
+
return f"{self._field}:{self._term}"
|
|
164
|
+
|
|
165
|
+
|
|
166
|
+
def search(query, number=500):
|
|
167
|
+
"""
|
|
168
|
+
Get all UniProt IDs that meet the given query requirements,
|
|
169
|
+
via the UniProt search service.
|
|
170
|
+
|
|
171
|
+
This function requires an internet connection.
|
|
172
|
+
|
|
173
|
+
Parameters
|
|
174
|
+
----------
|
|
175
|
+
query : Query
|
|
176
|
+
The search query.
|
|
177
|
+
number : int
|
|
178
|
+
The maximum number of IDs that are obtained.
|
|
179
|
+
(Default: 500)
|
|
180
|
+
|
|
181
|
+
Returns
|
|
182
|
+
-------
|
|
183
|
+
ids : list of str
|
|
184
|
+
A list of strings containing all UniProt IDs
|
|
185
|
+
that meet the query requirements.
|
|
186
|
+
|
|
187
|
+
Notes
|
|
188
|
+
-----
|
|
189
|
+
A list of available search fields with description can be found
|
|
190
|
+
`here <https://www.uniprot.org/help/query-fields>`_.
|
|
191
|
+
|
|
192
|
+
Examples
|
|
193
|
+
--------
|
|
194
|
+
>>> query = SimpleQuery("accession", "P12345") & \
|
|
195
|
+
SimpleQuery("reviewed", "true")
|
|
196
|
+
>>> ids = search(query)
|
|
197
|
+
>>> print(sorted(ids))
|
|
198
|
+
['P12345']
|
|
199
|
+
"""
|
|
200
|
+
|
|
201
|
+
params = {
|
|
202
|
+
'query': str(query),
|
|
203
|
+
'format': 'list',
|
|
204
|
+
'size': str(number)
|
|
205
|
+
}
|
|
206
|
+
r = requests.get(_base_url, params=params)
|
|
207
|
+
content = r.text
|
|
208
|
+
assert_valid_response(r.status_code)
|
|
209
|
+
return content.split('\n')[:-1]
|
biotite/file.py
ADDED
|
@@ -0,0 +1,251 @@
|
|
|
1
|
+
# This source code is part of the Biotite package and is distributed
|
|
2
|
+
# under the 3-Clause BSD License. Please see 'LICENSE.rst' for further
|
|
3
|
+
# information.
|
|
4
|
+
|
|
5
|
+
__name__ = "biotite"
|
|
6
|
+
__author__ = "Patrick Kunzmann"
|
|
7
|
+
__all__ = ["File", "TextFile", "InvalidFileError",
|
|
8
|
+
"SerializationError", "DeserializationError"]
|
|
9
|
+
|
|
10
|
+
import abc
|
|
11
|
+
import io
|
|
12
|
+
import warnings
|
|
13
|
+
from os import PathLike
|
|
14
|
+
|
|
15
|
+
from .copyable import Copyable
|
|
16
|
+
import copy
|
|
17
|
+
|
|
18
|
+
|
|
19
|
+
class File(Copyable, metaclass=abc.ABCMeta):
|
|
20
|
+
"""
|
|
21
|
+
Base class for all file classes.
|
|
22
|
+
The constructor creates an empty file, that can be filled with data
|
|
23
|
+
using the class specific setter methods.
|
|
24
|
+
Conversely, the class method :func:`read()` reads a file from disk
|
|
25
|
+
(or a file-like object from other sources).
|
|
26
|
+
In order to write the instance content into a file the
|
|
27
|
+
:func:`write()` method is used.
|
|
28
|
+
"""
|
|
29
|
+
|
|
30
|
+
def __init__(self):
|
|
31
|
+
# Support for deprecated instance method 'read()':
|
|
32
|
+
# When creating an instance, the 'read()' class method is
|
|
33
|
+
# replaced by the instance method, so that subsequent
|
|
34
|
+
# 'read()' calls are delegated to the instance method
|
|
35
|
+
self.read = self._deprecated_read
|
|
36
|
+
|
|
37
|
+
@classmethod
|
|
38
|
+
@abc.abstractmethod
|
|
39
|
+
def read(cls, file):
|
|
40
|
+
"""
|
|
41
|
+
Parse a file (or file-like object).
|
|
42
|
+
|
|
43
|
+
Parameters
|
|
44
|
+
----------
|
|
45
|
+
file : file-like object or str
|
|
46
|
+
The file to be read.
|
|
47
|
+
Alternatively a file path can be supplied.
|
|
48
|
+
|
|
49
|
+
Returns
|
|
50
|
+
-------
|
|
51
|
+
file_object : File
|
|
52
|
+
An instance from the respective :class:`File` subclass
|
|
53
|
+
representing the parsed file.
|
|
54
|
+
"""
|
|
55
|
+
pass
|
|
56
|
+
|
|
57
|
+
def _deprecated_read(self, file, *args, **kwargs):
|
|
58
|
+
"""
|
|
59
|
+
Support for deprecated instance method :func:`read()`.
|
|
60
|
+
|
|
61
|
+
Internally this calls the :func:`read()` class method and
|
|
62
|
+
replaces the data in `self` with the data from the newly created
|
|
63
|
+
:class:`File` object
|
|
64
|
+
"""
|
|
65
|
+
warnings.warn(
|
|
66
|
+
"Instance method 'read()' is deprecated, "
|
|
67
|
+
"use class method instead",
|
|
68
|
+
DeprecationWarning
|
|
69
|
+
)
|
|
70
|
+
cls = type(self)
|
|
71
|
+
new_file = cls.read(file, *args, **kwargs)
|
|
72
|
+
self.__dict__.update(new_file.__dict__)
|
|
73
|
+
|
|
74
|
+
@abc.abstractmethod
|
|
75
|
+
def write(self, file):
|
|
76
|
+
"""
|
|
77
|
+
Write the contents of this :class:`File` object into a file.
|
|
78
|
+
|
|
79
|
+
Parameters
|
|
80
|
+
----------
|
|
81
|
+
file_name : file-like object or str
|
|
82
|
+
The file to be written to.
|
|
83
|
+
Alternatively a file path can be supplied.
|
|
84
|
+
"""
|
|
85
|
+
pass
|
|
86
|
+
|
|
87
|
+
|
|
88
|
+
class TextFile(File, metaclass=abc.ABCMeta):
|
|
89
|
+
"""
|
|
90
|
+
Base class for all line based text files.
|
|
91
|
+
When reading a file, the text content is saved as list of strings,
|
|
92
|
+
one for each line.
|
|
93
|
+
When writing a file, this list is written into the file.
|
|
94
|
+
|
|
95
|
+
Attributes
|
|
96
|
+
----------
|
|
97
|
+
lines : list
|
|
98
|
+
List of string representing the lines in the text file.
|
|
99
|
+
PROTECTED: Do not modify from outside.
|
|
100
|
+
"""
|
|
101
|
+
|
|
102
|
+
def __init__(self):
|
|
103
|
+
super().__init__()
|
|
104
|
+
self.lines = []
|
|
105
|
+
|
|
106
|
+
@classmethod
|
|
107
|
+
def read(cls, file, *args, **kwargs):
|
|
108
|
+
# File name
|
|
109
|
+
if is_open_compatible(file):
|
|
110
|
+
with open(file, "r") as f:
|
|
111
|
+
lines = f.read().splitlines()
|
|
112
|
+
# File object
|
|
113
|
+
else:
|
|
114
|
+
if not is_text(file):
|
|
115
|
+
raise TypeError("A file opened in 'text' mode is required")
|
|
116
|
+
lines = file.read().splitlines()
|
|
117
|
+
file_object = cls(*args, **kwargs)
|
|
118
|
+
file_object.lines = lines
|
|
119
|
+
return file_object
|
|
120
|
+
|
|
121
|
+
@staticmethod
|
|
122
|
+
def read_iter(file):
|
|
123
|
+
"""
|
|
124
|
+
Create an iterator over each line of the given text file.
|
|
125
|
+
|
|
126
|
+
Parameters
|
|
127
|
+
----------
|
|
128
|
+
file : file-like object or str
|
|
129
|
+
The file to be read.
|
|
130
|
+
Alternatively a file path can be supplied.
|
|
131
|
+
|
|
132
|
+
Yields
|
|
133
|
+
------
|
|
134
|
+
line : str
|
|
135
|
+
The current line in the file.
|
|
136
|
+
"""
|
|
137
|
+
# File name
|
|
138
|
+
if is_open_compatible(file):
|
|
139
|
+
with open(file, "r") as f:
|
|
140
|
+
yield from f
|
|
141
|
+
# File object
|
|
142
|
+
else:
|
|
143
|
+
if not is_text(file):
|
|
144
|
+
raise TypeError("A file opened in 'text' mode is required")
|
|
145
|
+
yield from file
|
|
146
|
+
|
|
147
|
+
def write(self, file):
|
|
148
|
+
"""
|
|
149
|
+
Write the contents of this object into a file
|
|
150
|
+
(or file-like object).
|
|
151
|
+
|
|
152
|
+
Parameters
|
|
153
|
+
----------
|
|
154
|
+
file : file-like object or str
|
|
155
|
+
The file to be written to.
|
|
156
|
+
Alternatively a file path can be supplied.
|
|
157
|
+
"""
|
|
158
|
+
if is_open_compatible(file):
|
|
159
|
+
with open(file, "w") as f:
|
|
160
|
+
f.write("\n".join(self.lines) + "\n")
|
|
161
|
+
else:
|
|
162
|
+
if not is_text(file):
|
|
163
|
+
raise TypeError("A file opened in 'text' mode is required")
|
|
164
|
+
file.write("\n".join(self.lines) + "\n")
|
|
165
|
+
|
|
166
|
+
@staticmethod
|
|
167
|
+
def write_iter(file, lines):
|
|
168
|
+
"""
|
|
169
|
+
Iterate over the given `lines` of text and write each line into
|
|
170
|
+
the specified `file`.
|
|
171
|
+
|
|
172
|
+
In contrast to :meth:`write()`, each line of text is not stored
|
|
173
|
+
in an intermediate :class:`TextFile`, but is directly written
|
|
174
|
+
to the file.
|
|
175
|
+
Hence, this static method may save a large amount of memory if
|
|
176
|
+
a large file should be written, especially if the `lines`
|
|
177
|
+
are provided as generator.
|
|
178
|
+
|
|
179
|
+
Parameters
|
|
180
|
+
----------
|
|
181
|
+
file : file-like object or str
|
|
182
|
+
The file to be written to.
|
|
183
|
+
Alternatively a file path can be supplied.
|
|
184
|
+
lines : generator or array-like of str
|
|
185
|
+
The lines of text to be written.
|
|
186
|
+
Must not include line break characters.
|
|
187
|
+
"""
|
|
188
|
+
if is_open_compatible(file):
|
|
189
|
+
with open(file, "w") as f:
|
|
190
|
+
for line in lines:
|
|
191
|
+
f.write(line + "\n")
|
|
192
|
+
else:
|
|
193
|
+
if not is_text(file):
|
|
194
|
+
raise TypeError("A file opened in 'text' mode is required")
|
|
195
|
+
for line in lines:
|
|
196
|
+
file.write(line + "\n")
|
|
197
|
+
|
|
198
|
+
def __copy_fill__(self, clone):
|
|
199
|
+
super().__copy_fill__(clone)
|
|
200
|
+
clone.lines = copy.copy(self.lines)
|
|
201
|
+
|
|
202
|
+
def __str__(self):
|
|
203
|
+
return "\n".join(self.lines)
|
|
204
|
+
|
|
205
|
+
|
|
206
|
+
class InvalidFileError(Exception):
|
|
207
|
+
"""
|
|
208
|
+
Indicates that the file is not suitable for the requested action,
|
|
209
|
+
either because the file does not contain the required data or
|
|
210
|
+
because the file is malformed.
|
|
211
|
+
"""
|
|
212
|
+
pass
|
|
213
|
+
|
|
214
|
+
|
|
215
|
+
class SerializationError(Exception):
|
|
216
|
+
pass
|
|
217
|
+
|
|
218
|
+
class DeserializationError(Exception):
|
|
219
|
+
pass
|
|
220
|
+
|
|
221
|
+
|
|
222
|
+
def wrap_string(text, width):
|
|
223
|
+
"""
|
|
224
|
+
A much simpler and hence much more efficient version of
|
|
225
|
+
`textwrap.wrap()`.
|
|
226
|
+
|
|
227
|
+
This function simply wraps the given `text` after `width`
|
|
228
|
+
characters, ignoring sentences, whitespaces, etc.
|
|
229
|
+
"""
|
|
230
|
+
lines = []
|
|
231
|
+
for i in range(0, len(text), width):
|
|
232
|
+
lines.append(text[i : i+width])
|
|
233
|
+
return lines
|
|
234
|
+
|
|
235
|
+
|
|
236
|
+
def is_binary(file):
|
|
237
|
+
if isinstance(file, io.BufferedIOBase):
|
|
238
|
+
return True
|
|
239
|
+
# for file wrappers, e.g. 'TemporaryFile'
|
|
240
|
+
return hasattr(file, "file") and isinstance(file.file, io.BufferedIOBase)
|
|
241
|
+
|
|
242
|
+
|
|
243
|
+
def is_text(file):
|
|
244
|
+
if isinstance(file, io.TextIOBase):
|
|
245
|
+
return True
|
|
246
|
+
# for file wrappers, e.g. 'TemporaryFile'
|
|
247
|
+
return hasattr(file, "file") and isinstance(file.file, io.TextIOBase)
|
|
248
|
+
|
|
249
|
+
|
|
250
|
+
def is_open_compatible(file):
|
|
251
|
+
return isinstance(file, (str, bytes, PathLike))
|