# Path: abtools/pipeline.py
# def list_files(d, extension=None):
#     '''
#     Lists files in a given directory.
# 
#     Args:
# 
#         d (str): Path to a directory.
# 
#         extension (str): If supplied, only files that contain the
#             specificied extension will be returned. Default is ``False``,
#             which returns all files in ``d``.
# 
#     Returns:
# 
#         list: A sorted list of file paths.
#     '''
#     if os.path.isdir(d):
#         expanded_dir = os.path.expanduser(d)
#         files = sorted(glob.glob(expanded_dir + '/*'))
#     else:
#         files = [d, ]
#     if extension is not None:
#         if type(extension) in [str, unicode]:
#             extension = [extension, ]
#         files = [f for f in files if any([f.split('.')[-1] in extension,
#                                           f.split('.')[-1].upper() in extension,
#                                           f.split('.')[-1].lower() in extension])]
#     return files
# 
# def make_dir(d):
#     '''
#     Makes a directory, if it doesn't already exist.
# 
#     Args:
# 
#         d (str): Path to a directory.
#     '''
#     if not os.path.exists(d):
#         os.makedirs(d)


from itertools import chain
from zipfile import ZipFile
from Bio import SeqIO
from abtools.pipeline import list_files, make_dir
import os
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING
# BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
# NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,
# DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
#






def abi_to_fasta(input, output):
    '''
    Converts ABI or AB1 files to FASTA format.

    Args:

         input (str): Path to a file or directory containing abi/ab1 files or
            zip archives of abi/ab1 files

        output (str): Path to a directory for the output FASTA files
    '''
    direcs = [input, ]
    # unzip any zip archives
    zip_files = list_files(input, ['zip'])
    if zip_files:
        direcs.extend(_process_zip_files(zip_files))
    # convert files
    for d in direcs:

# Path: abtools/pipeline.py
# def list_files(d, extension=None):
#     '''
#     Lists files in a given directory.
# 
#     Args:
# 
#         d (str): Path to a directory.
# 
#         extension (str): If supplied, only files that contain the
#             specificied extension will be returned. Default is ``False``,
#             which returns all files in ``d``.
# 
#     Returns:
# 
#         list: A sorted list of file paths.
#     '''
#     if os.path.isdir(d):
#         expanded_dir = os.path.expanduser(d)
#         files = sorted(glob.glob(expanded_dir + '/*'))
#     else:
#         files = [d, ]
#     if extension is not None:
#         if type(extension) in [str, unicode]:
#             extension = [extension, ]
#         files = [f for f in files if any([f.split('.')[-1] in extension,
#                                           f.split('.')[-1].upper() in extension,
#                                           f.split('.')[-1].lower() in extension])]
#     return files
# 
# def make_dir(d):
#     '''
#     Makes a directory, if it doesn't already exist.
# 
#     Args:
# 
#         d (str): Path to a directory.
#     '''
#     if not os.path.exists(d):
#         os.makedirs(d)


# Path: abtools/utils/convert.py
from itertools import chain
from zipfile import ZipFile
from Bio import SeqIO
from abtools.pipeline import list_files, make_dir
import os

# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING
# BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
# NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,
# DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
#






def abi_to_fasta(input, output):
    '''
    Converts ABI or AB1 files to FASTA format.

    Args:

         input (str): Path to a file or directory containing abi/ab1 files or
            zip archives of abi/ab1 files

        output (str): Path to a directory for the output FASTA files
    '''
    direcs = [input, ]
    # unzip any zip archives
    zip_files = list_files(input, ['zip'])
    if zip_files:
        direcs.extend(_process_zip_files(zip_files))
    # convert files
    for d in direcs:

        files = list_files(d, ['ab1', 'abi'])