def MergePdbFiles( filenames,
                   rotations = None,
                   target = None,
                   chains = None,
                   do_gunzip = None):
    """Merge several pdb files.

    Rotates them if so desired.
    """
    
    current_chain = 0

    map_old2new = {}

    current_id = ord("A")
    index = 0
    coord = []

    for index in range(len(filenames)):
        
        coord.append("MODEL            %i\n" % (index))

        if not os.path.exists( filenames[index] ):
            raise "pdb file for %s not found" % pdb_id
        
        if do_gunzip:
            statement = "gunzip < %s" % filenames[index]
        else:
            statement = "cat %s" % filenames[index]

        if rotations:
            if rotations[index]:
                t = rotations[index][0]
                u = numpy.transpose(rotations[index][1])
                statement += " | %s %s %s" % (CMD_ROTATE,
                                           string.join(map(str, t), " "),
                                           string.join(map(str, numpy.reshape(u, (9,))), " "))

        lines = os.popen(statement).readlines()

        for line in lines:
            if line[:4] == "ATOM":
                chain = string.upper(line[21])
                if chains and chain in chains[index]: continue

                key = str(index) + "-" + chain
                
                if not map_old2new.has_key(key):
                    map_old2new[key] = chr(current_id)
                    current_id += 1

                new_chain = map_old2new[key]
                
                coord.append(line[:21] + new_chain + line[22:])

        coord.append("ENDMDL\n")

    result=["HEADER    PROTEIN STRUCTURE ALIGNMENT\n"]
        
    for key in map_old2new.keys():
        result.append("COMPND    (%s) %s\n" % (map_old2new[key], key))
        
    result += coord 

    if target:
        file = open(target,"w")
    else:
        file = sys.stdout

    file.write(string.join(result, "") + "\n")

    if target:
        file.close()
dataset/ETHPy150Open CGATOxford/cgat/legacy/PdbTools.py/MergePdbFiles/original