Alignment of seismic traces is a recurring need in seismic processing and interpretation. For global alignment via static shift there are robust tools available, including cross correlation. However, another kind of alignment problem arises in applications as diverse as associating synthetic seismograms to field data, harmonizing P-wave and mode converted data, final multilevel flattening of common image gathers, and so on. These cases require combinations of trace compression, extension, and shift - all of which are time variant. The difficulty is to find a mapping between the traces which is in some sense optimum. This problem is solved here using a modified form of the Needleman-Wunsch algorithm, which was originally developed for amino acid sequence alignment in proteins. Applied to seismic traces, this global optimization algorithm provides a nonlinear mapping of one seismic trace onto another. The method extends to alignment of any number of traces since that problem can be broken down into a cascade of pairwise alignments. The Needleman-Wunch algorithm is discussed, extended to the seismic case, and applied to field data. The results show a promising new tool for nonlinear alignment or flattening of seismic data.