Exemplo n.º 1
0
 def records_in(handle):
     """Generator function to iterate over fasta records as Sequence
     objects.
     """
     with handle:
         header = None
         for (is_header, lines) in groupby(handle, \
                 lambda line : line[0] == '>'):
             if is_header:
                 header = lines.next().rstrip()[1:]
             else:
                 if header:
                     seq = "".join([line.rstrip() \
                             for line in lines]).upper()
                     yield Sequence(name=header, seq=seq)
                 else:  # skip comment section
                     continue
Exemplo n.º 2
0
 def __init__(self, fn=None):
     self.seqs = []
     if fn is None:
         self.seqs = []
     elif fn != None and os.path.exists(fn):
         fi = open(fn, 'r')
         rec = []
         b = False
         for ln in fi:
             fl = ln.rstrip()
             if fl.startswith(">") and b:
                 #sq = self.createSeq(rec)
                 sn, ss = "", ""
                 for i in rec:
                     if i.startswith(">"):
                         sn = i[1:]
                     else:
                         ss = ss + i.replace(" ", "")
                 sq = Sequence(sn, ss)
                 del rec[:]
                 self.seqs.append(sq)
                 rec.append(fl)
             elif fl.startswith(">") and not b and not rec:
                 b = True
                 rec.append(fl)
             elif not fl.startswith(">"):
                 rec.append(fl)
         sn, ss = "", ""
         for i in rec:
             if i.startswith(">"):
                 sn = i[1:]
             else:
                 ss = ss + i.replace(" ", "")
             asq = AlignedSequence(sn, ss)
             del rec[:]
             self.aseqs.append(asq)
     else:
         print("File", fn, "does not exist!!")
         sys.exit()