code: def extract_pairs(fastqfile, p1fw, p2fw, fragsfw, p, suffix=False):
      
      fp = open(fastqfile)
      currentID = 0
      npairs = nfrags = 0
      for x, lib in izip(p.r1, p.libs):
          while currentID != x:
              fragsfw.writelines(islice(fp, 4))  # Exhaust the iterator
              currentID += 1
              nfrags += 1
          a = list(islice(fp, 4))
          b = list(islice(fp, 4))
          if suffix:
              name = a[0].rstrip()
              a[0] = name + "/1\n"
              b[0] = name + "/2\n"
          else:
              b[0] = a[0]  # Keep same read ID for pairs
  
          p1fw[lib].writelines(a)
          p2fw[lib].writelines(b)
          currentID += 2
          npairs += 2
  
      # Write the remaining single reads
      while True:
          contents = list(islice(fp, 4))
          if not contents:
              break
          fragsfw.writelines(contents)
          nfrags += 1
  
      logging.debug("A total of {0} paired reads written to `{1}`.".\
                    format(npairs, ",".join(x.name for x in p1fw + p2fw)))
      logging.debug("A total of {0} single reads written to `{1}`.".\
                    format(nfrags, fragsfw.name))
  
      # Validate the numbers
      expected_pairs = 2 * p.npairs
      expected_frags = p.nreads - 2 * p.npairs
      assert npairs == expected_pairs, "Expect {0} paired reads, got {1} instead".\
                format(expected_pairs, npairs)
      assert nfrags == expected_frags, "Expect {0} single reads, got {1} instead".\
                format(expected_frags, nfrags)
docs: Take fastqfile and array of pair ID, extract adjacent pairs to outfile.
      Perform check on numbers when done. p1fw, p2fw is a list of file handles,
      each for one end. p is a Pairs instance.