code: def filter(args):
      
      p = OptionParser(filter.__doc__)
      opts, args = p.parse_args(args)
  
      if len(args) != 2:
          sys.exit(not p.print_help())
  
      frgfile, idsfile = args
      assert frgfile.endswith(".frg")
  
      fp = open(idsfile)
      allowed = set(x.strip() for x in fp)
      logging.debug("A total of {0} allowed ids loaded.".format(len(allowed)))
  
      newfrgfile = frgfile.replace(".frg", ".filtered.frg")
      fp = open(frgfile)
      fw = open(newfrgfile, "w")
  
      nfrags, discarded_frags = 0, 0
      nmates, discarded_mates = 0, 0
      for rec in iter_records(fp):
          if rec.type == "FRG":
              readname = rec.get_field("acc")
              readname = readname.rstrip("ab")
              nfrags += 1
              if readname not in allowed:
                  discarded_frags += 1
                  continue
          if rec.type == "LKG":
              readname = rec.get_field("frg")
              readname = readname.rstrip("ab")
              nmates += 1
              if readname not in allowed:
                  discarded_mates += 1
                  continue
          print(rec, file=fw)
  
      # Print out a summary
      survived_frags = nfrags - discarded_frags
      survived_mates = nmates - discarded_mates
      print("Survived fragments: {0}".\
              format(percentage(survived_frags, nfrags)), file=sys.stderr)
      print("Survived mates: {0}".\
              format(percentage(survived_mates, nmates)), file=sys.stderr)
docs: %prog filter frgfile idsfile
  
      Removes the reads from frgfile that are indicated as duplicates in the
      clstrfile (generated by CD-HIT-454). `idsfile` includes a set of names to
      include in the filtered frgfile. See apps.cdhit.ids().