Hot-keys on this page

r m x p   toggle line displays

j k   next/prev highlighted chunk

0   (zero) top of page

1   (one) first highlighted chunk

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

34

35

36

37

38

39

40

41

42

43

44

45

46

47

48

49

50

51

52

53

54

55

56

57

58

59

60

61

62

63

64

65

66

67

68

69

70

71

72

73

74

75

76

77

78

79

80

81

82

83

84

85

86

87

88

89

90

91

92

93

94

95

96

97

98

99

100

101

102

103

104

105

106

107

108

109

110

111

112

113

114

115

116

117

118

119

120

121

122

123

124

125

126

127

128

129

130

131

132

133

134

135

136

137

138

139

140

141

142

143

144

145

146

147

148

149

150

151

152

153

154

155

156

157

158

159

160

161

162

163

164

165

166

167

168

169

170

171

172

173

174

175

176

177

178

179

180

181

182

183

184

185

186

187

188

189

190

191

192

193

194

195

196

197

198

199

200

201

202

203

204

205

206

207

208

209

210

211

212

213

214

215

216

217

218

219

220

221

222

223

224

225

226

227

228

229

230

231

232

233

234

235

236

237

238

239

240

241

242

243

244

245

246

247

248

249

250

251

252

253

254

255

256

257

258

259

260

261

262

263

264

265

266

267

268

269

270

271

272

273

274

275

276

277

278

279

280

# Copyright 2006-2009 by Peter Cock.  All rights reserved. 

# This code is part of the Biopython distribution and governed by its 

# license.  Please see the LICENSE file that should have been included 

# as part of this package. 

# 

# This module is for reading and writing FASTA format files as SeqRecord 

# objects.  The code is partly inspired  by earlier Biopython modules, 

# Bio.Fasta.* and the now deprecated Bio.SeqIO.FASTA 

 

"""Bio.SeqIO support for the "fasta" (aka FastA or Pearson) file format. 

 

You are expected to use this module via the Bio.SeqIO functions.""" 

 

from __future__ import print_function 

 

from Bio.Alphabet import single_letter_alphabet 

from Bio.Seq import Seq 

from Bio.SeqRecord import SeqRecord 

from Bio.SeqIO.Interfaces import SequentialSequenceWriter 

 

 

def SimpleFastaParser(handle): 

    """Generator function to iterate over Fasta records (as string tuples). 

 

    For each record a tuple of two strings is returned, the FASTA title 

    line (without the leading '>' character), and the sequence (with any 

    whitespace removed). The title line is not divided up into an 

    identifier (the first word) and comment or description. 

 

    >>> with open("Fasta/dups.fasta") as handle: 

    ...     for values in SimpleFastaParser(handle): 

    ...         print(values) 

    ... 

    ('alpha', 'ACGTA') 

    ('beta', 'CGTC') 

    ('gamma', 'CCGCC') 

    ('alpha (again - this is a duplicate entry to test the indexing code)', 'ACGTA') 

    ('delta', 'CGCGC') 

 

    """ 

    #Skip any text before the first record (e.g. blank lines, comments) 

    while True: 

        line = handle.readline() 

        if line == "": 

            return  # Premature end of file, or just empty? 

        if line[0] == ">": 

            break 

 

    while True: 

        if line[0] != ">": 

            raise ValueError( 

                "Records in Fasta files should start with '>' character") 

        title = line[1:].rstrip() 

        lines = [] 

        line = handle.readline() 

        while True: 

            if not line: 

                break 

            if line[0] == ">": 

                break 

            lines.append(line.rstrip()) 

            line = handle.readline() 

 

        #Remove trailing whitespace, and any internal spaces 

        #(and any embedded \r which are possible in mangled files 

        #when not opened in universal read lines mode) 

        yield title, "".join(lines).replace(" ", "").replace("\r", "") 

 

        if not line: 

            return  # StopIteration 

 

    assert False, "Should not reach this line" 

 

 

def FastaIterator(handle, alphabet=single_letter_alphabet, title2ids=None): 

    """Generator function to iterate over Fasta records (as SeqRecord objects). 

 

    handle - input file 

    alphabet - optional alphabet 

    title2ids - A function that, when given the title of the FASTA 

    file (without the beginning >), will return the id, name and 

    description (in that order) for the record as a tuple of strings. 

 

    If this is not given, then the entire title line will be used 

    as the description, and the first word as the id and name. 

 

    By default this will act like calling Bio.SeqIO.parse(handle, "fasta") 

    with no custom handling of the title lines: 

 

    >>> with open("Fasta/dups.fasta") as handle: 

    ...     for record in FastaIterator(handle): 

    ...         print(record.id) 

    ... 

    alpha 

    beta 

    gamma 

    alpha 

    delta 

 

    However, you can supply a title2ids function to alter this: 

 

    >>> def take_upper(title): 

    ...     return title.split(None, 1)[0].upper(), "", title 

    >>> with open("Fasta/dups.fasta") as handle: 

    ...     for record in FastaIterator(handle, title2ids=take_upper): 

    ...         print(record.id) 

    ... 

    ALPHA 

    BETA 

    GAMMA 

    ALPHA 

    DELTA 

 

    """ 

    if title2ids: 

        for title, sequence in SimpleFastaParser(handle): 

            id, name, descr = title2ids(title) 

            yield SeqRecord(Seq(sequence, alphabet), 

                            id=id, name=name, description=descr) 

    else: 

        for title, sequence in SimpleFastaParser(handle): 

            try: 

                first_word = title.split(None, 1)[0] 

            except IndexError: 

                assert not title, repr(title) 

                #Should we use SeqRecord default for no ID? 

                first_word = "" 

            yield SeqRecord(Seq(sequence, alphabet), 

                            id=first_word, name=first_word, description=title) 

 

 

class FastaWriter(SequentialSequenceWriter): 

    """Class to write Fasta format files.""" 

    def __init__(self, handle, wrap=60, record2title=None): 

        """Create a Fasta writer. 

 

        handle - Handle to an output file, e.g. as returned 

                 by open(filename, "w") 

        wrap -   Optional line length used to wrap sequence lines. 

                 Defaults to wrapping the sequence at 60 characters 

                 Use zero (or None) for no wrapping, giving a single 

                 long line for the sequence. 

        record2title - Optional function to return the text to be 

                 used for the title line of each record.  By default 

                 a combination of the record.id and record.description 

                 is used.  If the record.description starts with the 

                 record.id, then just the record.description is used. 

 

        You can either use:: 

 

            handle = open(filename, "w") 

            myWriter = FastaWriter(handle) 

            writer.write_file(myRecords) 

            handle.close() 

 

        Or, follow the sequential file writer system, for example: 

 

            handle = open(filename, "w") 

            myWriter = FastaWriter(handle) 

            writer.write_header() # does nothing for Fasta files 

            ... 

            Multiple calls to writer.write_record() and/or writer.write_records() 

            ... 

            writer.write_footer() # does nothing for Fasta files 

            handle.close() 

 

        """ 

        SequentialSequenceWriter.__init__(self, handle) 

        #self.handle = handle 

        self.wrap = None 

        if wrap: 

            if wrap < 1: 

                raise ValueError 

        self.wrap = wrap 

        self.record2title = record2title 

 

    def write_record(self, record): 

        """Write a single Fasta record to the file.""" 

        assert self._header_written 

        assert not self._footer_written 

        self._record_written = True 

 

        if self.record2title: 

            title = self.clean(self.record2title(record)) 

        else: 

            id = self.clean(record.id) 

            description = self.clean(record.description) 

 

            #if description[:len(id)]==id: 

            if description and description.split(None, 1)[0] == id: 

                #The description includes the id at the start 

                title = description 

            elif description: 

                title = "%s %s" % (id, description) 

            else: 

                title = id 

 

        assert "\n" not in title 

        assert "\r" not in title 

        self.handle.write(">%s\n" % title) 

 

        data = self._get_seq_string(record)  # Catches sequence being None 

 

        assert "\n" not in data 

        assert "\r" not in data 

 

        if self.wrap: 

            for i in range(0, len(data), self.wrap): 

                self.handle.write(data[i:i + self.wrap] + "\n") 

        else: 

            self.handle.write(data + "\n") 

 

if __name__ == "__main__": 

    print("Running quick self test") 

 

    import os 

    from Bio.Alphabet import generic_protein, generic_nucleotide 

 

    #Download the files from here: 

    #ftp://ftp.ncbi.nlm.nih.gov/genomes/Bacteria/Nanoarchaeum_equitans 

    fna_filename = "NC_005213.fna" 

    faa_filename = "NC_005213.faa" 

 

    def genbank_name_function(text): 

        text, descr = text.split(None, 1) 

        id = text.split("|")[3] 

        name = id.split(".", 1)[0] 

        return id, name, descr 

 

    def print_record(record): 

        #See also bug 2057 

        #http://bugzilla.open-bio.org/show_bug.cgi?id=2057 

        print("ID:" + record.id) 

        print("Name:" + record.name) 

        print("Descr:" + record.description) 

        print(record.seq) 

        for feature in record.annotations: 

            print('/%s=%s' % (feature, record.annotations[feature])) 

        if record.dbxrefs: 

            print("Database cross references:") 

            for x in record.dbxrefs: 

                print(" - %s" % x) 

 

    if os.path.isfile(fna_filename): 

        print("--------") 

        print("FastaIterator (single sequence)") 

        with open(fna_filename, "r") as h: 

            iterator = FastaIterator(h, alphabet=generic_nucleotide, title2ids=genbank_name_function) 

        count = 0 

        for record in iterator: 

            count += 1 

            print_record(record) 

        assert count == 1 

        print(str(record.__class__)) 

 

    if os.path.isfile(faa_filename): 

        print("--------") 

        print("FastaIterator (multiple sequences)") 

        with open(faa_filename, "r") as h: 

            iterator = FastaIterator(h, alphabet=generic_protein, title2ids=genbank_name_function) 

        count = 0 

        for record in iterator: 

            count += 1 

            print_record(record) 

            break 

        assert count > 0 

        print(str(record.__class__)) 

 

    from Bio._py3k import StringIO 

    print("--------") 

    print("FastaIterator (empty input file)") 

    #Just to make sure no errors happen 

    iterator = FastaIterator(StringIO("")) 

    count = 0 

    for record in iterator: 

        count += 1 

    assert count == 0 

 

    print("Done")