Example #1
0
def fasta_read(input):
    """
        fasta_read(input):

        @param input can be either a file or the name of a file.

        returns a list of fasta_sequence objects with all the sequences in the file.
        comments (lines starting with ';') are ignored.
    """
    if type(input) == str:
        if input.endswith('.gz'):
            import gzip
            input=gzip.gzipfile(input)
        else:
            input=file(input)
    results = []
    header = ''
    seq_items = []
    first = true
    for line in input:
        if line[0] == ';':
            continue # comment
        elif line[0] == '>':
            if not first:
                seq= "".join(seq_items)
                results.append(fasta_sequence(header,seq))
                seq_items = []
            header = line[1:-1] # eat '>' and '\n'
            first = false
        else:
            seq_items.append(line[:-1])
    if len(seq_items) > 0:
        seq = "".join(seq_items)
        results.append(fasta_sequence(header,seq))
    return results
Example #2
0
def fasta_write(output,s):
    """
        fasta_write(output, sequence[s])

        @param output either a file (opened for writing) or a filename
        @param sequence it can be either a fasta_sequence or a list of fasta_sequence objects

        writes the sequence(s) into the file in fasta format
    """
    line_width=70
    if type(output) == str:
        if output.endswith('.gz'):
            import gzip
            output=gzip.gzipfile(output,'w')
        else:
            output=file(output,'w')
    if type(s) == list:
        for ss in s:
            fasta_write(output,ss)
    else:
        output.write("> %s\n" % s.header)
        for i in xrange(0,len(s.seq),line_width):
            output.write("%s\n" % s.seq[i:i+line_width])