Compare two files containing formatted basis sets
486794
def compare_basis_sets(basis_name_1,
                       basis_name_2,
                       version_1=None,
                       version_2=None,
                       uncontract_general=False,
                       data_dir_1=None,
                       data_dir_2=None):
    ''''''

    bs1 = get_basis(basis_name_1, version=version_1, data_dir=data_dir_1)
    bs2 = get_basis(basis_name_2, version=version_2, data_dir=data_dir_2)
    return basis_comparison_report(bs1, bs2, uncontract_general)
970980
def read_gbasis(basis_lines, fname):
    '''
    '''

    skipchars = '!#'
    basis_lines = [l for l in basis_lines if l and not l[0] in skipchars]

    bs_data = create_skel('component')

    i = 0
    bs_name = None
    while i < len(basis_lines):
        line = basis_lines[i]
        lsplt = line.split(':')
        elementsym = lsplt[0]

        if bs_name is None:
            bs_name = lsplt[1]
        elif lsplt[1] != bs_name:
            raise RuntimeError("Multiple basis sets in a file")

        element_Z = lut.element_Z_from_sym(elementsym)
        element_Z = str(element_Z)

        if not element_Z in bs_data['elements']:
            bs_data['elements'][element_Z] = {}

        element_data = bs_data['elements'][element_Z]

        if not 'electron_shells' in element_data:
            element_data['electron_shells'] = []

        i += 1

        max_am = int(basis_lines[i].strip())
        i += 1

        for am in range(0, max_am + 1):
            lsplt = basis_lines[i].split()
            shell_am = lut.amchar_to_int(lsplt[0])
            nprim = int(lsplt[1])
            ngen = int(lsplt[2])

            if shell_am[0] != am:
                raise RuntimeError("AM out of order in gbasis?")

            if max(shell_am) <= 1:
                func_type = 'gto'
            else:
                func_type = 'gto_spherical'


            shell = {
                'function_type': func_type,
                'region': '',
                'angular_momentum': shell_am
            }

            exponents = []
            coefficients = []

            i += 1
            for j in range(nprim):
                line = basis_lines[i].replace('D', 'E')
                line = line.replace('d', 'E')
                lsplt = line.split()

                if len(lsplt) != (ngen + 1):
                    raise RuntimeError("Incorrect number of general contractions in gbasis")

                exponents.append(lsplt[0])
                coefficients.append(lsplt[1:])
                i += 1

            shell['exponents'] = exponents

            # We need to transpose the coefficient matrix
            # (we store a matrix with primitives being the column index and
            # general contraction being the row index)
            shell['coefficients'] = list(map(list, zip(*coefficients)))

            element_data['electron_shells'].append(shell)

    return bs_data
779971
def diff_texts(a, b, filename):
    """"""
    a = a.splitlines()
    b = b.splitlines()
    return difflib.unified_diff(a, b, filename, filename,
                                "(original)", "(refactored)",
                                lineterm="")
1152920
def _compare(self, dir1, dir2):
        

        left = set()
        right = set()

        self._numdirs += 1

        excl_patterns = set(self._exclude).union(self._ignore)

        for cwd, dirs, files in os.walk(dir1):
            self._numdirs += len(dirs)
            for f in dirs + files:
                path = os.path.relpath(os.path.join(cwd, f), dir1)
                re_path = path.replace('\\', '/')
                if self._only:
                    for pattern in self._only:
                        if re.match(pattern, re_path):
                            # go to exclude and ignore filtering
                            break
                    else:
                        # next item, this one does not match any pattern
                        # in the _only list
                        continue

                add_path = False
                for pattern in self._include:
                    if re.match(pattern, re_path):
                        add_path = True
                        break
                else:
                    # path was not in includes
                    # test if it is in excludes
                    for pattern in excl_patterns:
                        if re.match(pattern, re_path):
                            # path is in excludes, do not add it
                            break
                    else:
                        # path was not in excludes
                        # it should be added
                        add_path = True

                if add_path:
                    left.add(path)
                    anc_dirs = re_path[:-1].split('/')
                    for i in range(1, len(anc_dirs)):
                        left.add('/'.join(anc_dirs[:i]))

        for cwd, dirs, files in os.walk(dir2):
            for f in dirs + files:
                path = os.path.relpath(os.path.join(cwd, f), dir2)
                re_path = path.replace('\\', '/')
                for pattern in self._ignore:
                    if re.match(pattern, re_path):
                        if f in dirs:
                            dirs.remove(f)
                        break
                else:
                    right.add(path)
                    # no need to add the parent dirs here,
                    # as there is no _only pattern detection
                    if f in dirs and path not in left:
                        self._numdirs += 1

        common = left.intersection(right)
        left.difference_update(common)
        right.difference_update(common)

        return DCMP(left, right, common)
311855
def is_same_file (filename1, filename2):
    
    if filename1 == filename2:
        return True
    if os.name == 'posix':
        return os.path.samefile(filename1, filename2)
    return is_same_filename(filename1, filename2)
660207
def check_parameter_similarity(files_dict):
    
    try:
        parameter_names = files_dict.itervalues().next().keys()  # get the parameter names of the first file, to check if these are the same in the other files
    except AttributeError:  # if there is no parameter at all
        if any(i is not None for i in files_dict.itervalues()):  # check if there is also no parameter for the other files
            return False
        else:
            return True
    if any(parameter_names != i.keys() for i in files_dict.itervalues()):
        return False
    return True
282039
def read_turbomole(basis_lines, fname):
    '''
    '''

    skipchars = '*#$'
    basis_lines = [l for l in basis_lines if l and not l[0] in skipchars]

    bs_data = create_skel('component')

    i = 0
    while i < len(basis_lines):
        line = basis_lines[i]
        elementsym = line.split()[0]

        element_Z = lut.element_Z_from_sym(elementsym)
        element_Z = str(element_Z)

        if not element_Z in bs_data['elements']:
            bs_data['elements'][element_Z] = {}

        element_data = bs_data['elements'][element_Z]

        if "ecp" in line.lower():
            if not 'ecp_potentials' in element_data:
                element_data['ecp_potentials'] = []

            i += 1
            line = basis_lines[i]

            lsplt = line.split('=')
            maxam = int(lsplt[2])
            n_elec = int(lsplt[1].split()[0])

            amlist = [maxam]
            amlist.extend(list(range(0, maxam)))

            i += 1
            for shell_am in amlist:
                shell_am2 = lut.amchar_to_int(basis_lines[i][0])[0]
                if shell_am2 != shell_am:
                    raise RuntimeError("AM not in expected order?")

                i += 1

                ecp_shell = {
                    'ecp_type': 'scalar_ecp',
                    'angular_momentum': [shell_am],
                }
                ecp_exponents = []
                ecp_rexponents = []
                ecp_coefficients = []

                while i < len(basis_lines) and basis_lines[i][0].isalpha() is False:
                    lsplt = basis_lines[i].split()
                    ecp_exponents.append(lsplt[2])
                    ecp_rexponents.append(int(lsplt[1]))
                    ecp_coefficients.append(lsplt[0])
                    i += 1

                ecp_shell['r_exponents'] = ecp_rexponents
                ecp_shell['gaussian_exponents'] = ecp_exponents
                ecp_shell['coefficients'] = [ecp_coefficients]
                element_data['ecp_potentials'].append(ecp_shell)

            element_data['ecp_electrons'] = n_elec

        else:
            if not 'electron_shells' in element_data:
                element_data['electron_shells'] = []

            i += 1
            while i < len(basis_lines) and basis_lines[i][0].isalpha() == False:
                lsplt = basis_lines[i].split()
                shell_am = lut.amchar_to_int(lsplt[1])
                nprim = int(lsplt[0])

                if max(shell_am) <= 1:
                    func_type = 'gto'
                else:
                    func_type = 'gto_spherical'

                shell = {
                    'function_type': func_type,
                    'region': '',
                    'angular_momentum': shell_am
                }

                exponents = []
                coefficients = []

                i += 1
                for j in range(nprim):
                    line = basis_lines[i].replace('D', 'E')
                    line = line.replace('d', 'E')
                    lsplt = line.split()
                    exponents.append(lsplt[0])
                    coefficients.append(lsplt[1:])
                    i += 1

                shell['exponents'] = exponents

                # We need to transpose the coefficient matrix
                # (we store a matrix with primitives being the column index and
                # general contraction being the row index)
                shell['coefficients'] = list(map(list, zip(*coefficients)))

                element_data['electron_shells'].append(shell)

    return bs_data
423708
function compareFilesWithIndices(index1, index2) {
                return entries[index1]._name.toLocaleLowerCase().localeCompare(entries[index2]._name.toLocaleLowerCase());
            }
497217
def compare_notebooks(notebook_expected,
                      notebook_actual,
                      fmt=None,
                      allow_expected_differences=True,
                      raise_on_first_difference=True,
                      compare_outputs=False):
    """"""
    fmt = long_form_one_format(fmt)
    format_name = fmt.get('format_name')

    # Expected differences
    allow_filtered_cell_metadata = allow_expected_differences
    allow_missing_code_cell_metadata = allow_expected_differences and format_name == 'sphinx'
    allow_missing_markdown_cell_metadata = allow_expected_differences and format_name in ['sphinx', 'spin']
    allow_removed_final_blank_line = allow_expected_differences

    cell_metadata_filter = notebook_actual.get('jupytext', {}).get('cell_metadata_filter')

    if format_name == 'sphinx' and notebook_actual.cells and notebook_actual.cells[0].source == '%matplotlib inline':
        notebook_actual.cells = notebook_actual.cells[1:]

    # Compare cells type and content
    test_cell_iter = iter(notebook_actual.cells)
    modified_cells = set()
    modified_cell_metadata = set()
    for i, ref_cell in enumerate(notebook_expected.cells, 1):
        try:
            test_cell = next(test_cell_iter)
        except StopIteration:
            if raise_on_first_difference:
                raise NotebookDifference('No cell corresponding to {} cell #{}:\n{}'
                                         .format(ref_cell.cell_type, i, ref_cell.source))
            modified_cells.update(range(i, len(notebook_expected.cells) + 1))
            break

        ref_lines = [line for line in ref_cell.source.splitlines() if not _BLANK_LINE.match(line)]
        test_lines = []

        # 1. test cell type
        if ref_cell.cell_type != test_cell.cell_type:
            if raise_on_first_difference:
                raise NotebookDifference("Unexpected cell type '{}' for {} cell #{}:\n{}"
                                         .format(test_cell.cell_type, ref_cell.cell_type, i, ref_cell.source))
            modified_cells.add(i)

        # 2. test cell metadata
        if (ref_cell.cell_type == 'code' and not allow_missing_code_cell_metadata) or \
                (ref_cell.cell_type != 'code' and not allow_missing_markdown_cell_metadata):

            if allow_filtered_cell_metadata:
                ref_cell.metadata = {key: ref_cell.metadata[key] for key in ref_cell.metadata
                                     if key not in _IGNORE_CELL_METADATA}
                test_cell.metadata = {key: test_cell.metadata[key] for key in test_cell.metadata
                                      if key not in _IGNORE_CELL_METADATA}

            if ref_cell.metadata != test_cell.metadata:
                if raise_on_first_difference:
                    try:
                        compare(ref_cell.metadata, test_cell.metadata)
                    except AssertionError as error:
                        raise NotebookDifference("Metadata differ on {} cell #{}: {}\nCell content:\n{}"
                                                 .format(test_cell.cell_type, i, str(error), ref_cell.source))
                else:
                    modified_cell_metadata.update(set(test_cell.metadata).difference(ref_cell.metadata))
                    modified_cell_metadata.update(set(ref_cell.metadata).difference(test_cell.metadata))
                    for key in set(ref_cell.metadata).intersection(test_cell.metadata):
                        if ref_cell.metadata[key] != test_cell.metadata[key]:
                            modified_cell_metadata.add(key)

        test_lines.extend([line for line in test_cell.source.splitlines() if not _BLANK_LINE.match(line)])

        # 3. test cell content
        if ref_lines != test_lines:
            if raise_on_first_difference:
                try:
                    compare('\n'.join(ref_lines), '\n'.join(test_lines))
                except AssertionError as error:
                    raise NotebookDifference("Cell content differ on {} cell #{}: {}"
                                             .format(test_cell.cell_type, i, str(error)))
            else:
                modified_cells.add(i)

        # 3. bis test entire cell content
        if not same_content(ref_cell.source, test_cell.source, allow_removed_final_blank_line):
            try:
                compare(ref_cell.source, test_cell.source)
            except AssertionError as error:
                if raise_on_first_difference:
                    raise NotebookDifference("Cell content differ on {} cell #{}: {}"
                                             .format(test_cell.cell_type, i, str(error)))
                modified_cells.add(i)

        if not compare_outputs:
            continue

        if ref_cell.cell_type != 'code':
            continue

        ref_cell = filtered_cell(ref_cell,
                                 preserve_outputs=compare_outputs,
                                 cell_metadata_filter=cell_metadata_filter)
        test_cell = filtered_cell(test_cell,
                                  preserve_outputs=compare_outputs,
                                  cell_metadata_filter=cell_metadata_filter)

        try:
            compare(ref_cell, test_cell)
        except AssertionError as error:
            if raise_on_first_difference:
                raise NotebookDifference("Cell outputs differ on {} cell #{}: {}"
                                         .format(test_cell['cell_type'], i, str(error)))
            modified_cells.add(i)

    # More cells in the actual notebook?
    remaining_cell_count = 0
    while True:
        try:
            test_cell = next(test_cell_iter)
            if raise_on_first_difference:
                raise NotebookDifference('Additional {} cell: {}'.format(test_cell.cell_type, test_cell.source))
            remaining_cell_count += 1
        except StopIteration:
            break

    if remaining_cell_count and not raise_on_first_difference:
        modified_cells.update(
            range(len(notebook_expected.cells) + 1, len(notebook_expected.cells) + 1 + remaining_cell_count))

    # Compare notebook metadata
    modified_metadata = False
    try:
        compare(filtered_notebook_metadata(notebook_expected),
                filtered_notebook_metadata(notebook_actual))
    except AssertionError as error:
        if raise_on_first_difference:
            raise NotebookDifference("Notebook metadata differ: {}".format(str(error)))
        modified_metadata = True

    error = []
    if modified_cells:
        error.append('Cells {} differ ({}/{})'.format(','.join([str(i) for i in modified_cells]),
                                                      len(modified_cells), len(notebook_expected.cells)))
    if modified_cell_metadata:
        error.append("Cell metadata '{}' differ".format("', '".join([str(i) for i in modified_cell_metadata])))
    if modified_metadata:
        error.append('Notebook metadata differ')

    if error:
        raise NotebookDifference(' | '.join(error))
793459
def isetdiff_flags(list1, list2):
    
    set2 = set(list2)
    return (item not in set2 for item in list1)
876423
def MemberDiff(data1, set1_name, data2, set2_name):
    """"""
    set1 = set(data1)
    set2 = set(data2)
    diffs = []
    msg = "Present in %s, missing in %s: %s"
    if set1 != set2:
      in_set1 = set1 - set2
      in_set2 = set2 - set1
      if in_set1:
        diffs.append(msg % (set1_name, set2_name, ",".join(in_set1)))
      if in_set2:
        diffs.append(msg % (set2_name, set1_name, ",".join(in_set2)))
    return diffs
722373
def match_filenames(filenames, pattern):
    """"""
    matching = []

    for filename in filenames:
        if fnmatch(get_filebase(filename, pattern), globify(pattern)):
            matching.append(filename)

    return matching
567566
def fasta_files_equal(seq_file1, seq_file2):
    

    # Load already set representative sequence
    seq1 = SeqIO.read(open(seq_file1), 'fasta')

    # Load kegg sequence
    seq2 = SeqIO.read(open(seq_file2), 'fasta')

    # Test equality
    if str(seq1.seq) == str(seq2.seq):
        return True
    else:
        return False
561221
def _bsecurate_cli_compare_basis_files(args):
    ''''''
    ret = curate.compare_basis_files(args.file1, args.file2, args.readfmt1, args.readfmt2, args.uncontract_general)

    if ret:
        return "No difference found"
    else:
        return "DIFFERENCES FOUND. SEE ABOVE"
348450
def assert_files_same(path1, path2):
    # type: (str, str) -> None
    
    difflines = compare_files(path1, path2)
    assert len(difflines) == 0, ''.join(['\n'] + difflines)
797550
def parse_udiff(diff, patterns=None, parent='.'):
    """"""
    # For each file of the diff, the entry key is the filename,
    # and the value is a set of row numbers to consider.
    rv = {}
    path = nrows = None
    for line in diff.splitlines():
        if nrows:
            if line[:1] != '-':
                nrows -= 1
            continue
        if line[:3] == '@@ ':
            hunk_match = HUNK_REGEX.match(line)
            (row, nrows) = [int(g or '1') for g in hunk_match.groups()]
            rv[path].update(range(row, row + nrows))
        elif line[:3] == '+++':
            path = line[4:].split('\t', 1)[0]
            if path[:2] == 'b/':
                path = path[2:]
            rv[path] = set()
    return dict([(os.path.join(parent, path), rows)
                 for (path, rows) in rv.items()
                 if rows and filename_match(path, patterns)])
748438
def file_matches_regexps(filename, patterns):
    """"""
    return any(re.match(pat, filename) for pat in patterns)