<commit_before>#! /usr/bin/env python
# -*- coding: utf-8 -*-

"""
Read PDB structures and assign protein blocs (PBs).

2013 - <NAME>, <NAME>
"""


## Use print as a function for python 3 compatibility
from __future__ import print_function

## standard modules
import os
import sys
import glob
import argparse

## local module
import PBlib as PB
import PDBlib as PDB

# MDAnalysis is an optional requirement
try:
    import MDAnalysis
except ImportError:
    IS_MDANALYSIS = False
else:
    IS_MDANALYSIS = True


## Python2/Python3 compatibility
# The range function in python 3 behaves as the range function in python 2
# and returns a generator rather than a list. To produce a list in python 3,
# one should use list(range). Here we change range to behave the same in
# python 2 and in python 3. In both cases, range will return a generator.
try:
    range = xrange
except NameError:
    pass


class _NullContext(object):
    def __enter__(self):
        pass

    def __exit__(self, exc_type, exc_value, traceback):
        pass


def PB_assign(pb_ref, structure, comment,
              fasta_file, flat_file=_NullContext(), phipsi_file=_NullContext()):
    """assign Protein Blocks (PB) from phi and psi angles
    """
    dihedrals = structure.get_phi_psi_angles()
    pb_seq = PB.assign(dihedrals, pb_ref)

    # write PBs in fasta file
    PB.write_fasta_entry(fasta_file, pb_seq, comment)
    # write phi and psi angles
    if not isinstance(phipsi_file, _NullContext):
        PB.write_phipsi_entry(phipsi_file, dihedrals, comment)
    # write PBs in flat file
    if not isinstance(flat_file, _NullContext):
        print(pb_seq, file=flat_file)
 <commit_msg>Make PBassign.PB_assign easier to use<commit_after>#! /usr/bin/env python
# -*- coding: utf-8 -*-

"""
Read PDB structures and assign protein blocs (PBs).

2013 - <NAME>, <NAME>
"""


## Use print as a function for python 3 compatibility
from __future__ import print_function

## standard modules
import os
import sys
import glob
import argparse

## local module
import PBlib as PB
import PDBlib as PDB

# MDAnalysis is an optional requirement
try:
    import MDAnalysis
except ImportError:
    IS_MDANALYSIS = False
else:
    IS_MDANALYSIS = True


## Python2/Python3 compatibility
# The range function in python 3 behaves as the range function in python 2
# and returns a generator rather than a list. To produce a list in python 3,
# one should use list(range). Here we change range to behave the same in
# python 2 and in python 3. In both cases, range will return a generator.
try:
    range = xrange
except NameError:
    pass


class _NullContext(object):
    """
    Dummy context manager that does nothing
    """
    def __enter__(self):
        pass

    def __exit__(self, exc_type, exc_value, traceback):
        pass


def PB_assign(pb_ref, structure, comment,
              fasta_file=None, flat_file=None, phipsi_file=None):
    """
    Assign Protein Blocks (PB) from phi and psi angles

    `fasta_file`, `flat_file`, and `phipsi_file` should be set to an open file
    if an entry should be written is the file or to None or an instance of
    _NullContext if no entry should be written.
    """
    dihedrals = structure.get_phi_psi_angles()
    pb_seq = PB.assign(dihedrals, pb_ref)

    # write PBs in fasta file
    if not (isinstance(fasta_file, _NullContext) or fasta_file is None):
        PB.write_fasta_entry(fasta_file, pb_seq, comment)
    # write phi and psi angles
    if not (isinstance(phipsi_file, _NullContext) or phipsi_file is None):
        PB.write_phipsi_entry(phipsi_file, dihedrals, comment)
    # write PBs in flat file
    if not (isinstance(flat_file, _NullContext) or flat_file is None):
        print(pb_seq, file=flat_file)

{'fraction_non_alphanumeric': 0.07556742323097464, 'fraction_numerical': 0.006942590120160214, 'mean_word_length': 3.513253012048193, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 7, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '5231268', 'n_tokens_mistral': 1230, 'n_tokens_neox': 1166, 'n_words': 442}