[Baypiggies] More dramatic material?

Mark Voorhies mvoorhie at yahoo.com
Wed Feb 24 22:09:41 CET 2010


On Wednesday 24 February 2010 7:36 am Aahz wrote:
> Generally speaking, regexes are good when you want to search for two
> things at the same time or to use the result of a search in a replace
> operation.

A nice Python feature for this case is being able to assign names to the 
matched groups, e.g.:

import re

locus_re = re.compile(
  "(^(?P<ref>[^:]+)\:(?P<start>[\d]+)\.\.(?P<stop>[\d]+)$)", re.M)

class Locus:
  def __init__(self, ref, start, stop, strand = "+"):
     self.ref = str(ref)
     self.start = int(start)
     self.stop = int(stop)

open("loci.txt","w").write("\n".join((
    "contig1:1..100",
    "contig2:1..50",
    "contig3:75..600"))+"\n")

loci = [Locus(strand = "-", **(i.groupdict())) for i in  
        locus_re.finditer(open("loci.txt").read())]

--Mark



More information about the Baypiggies mailing list