Commits

Bob Harris committed f695c44

separated read_lengths_file into its own module

Comments (0)

Files changed (2)

lib/bx/align/core.py

 
 import random
 import string
-#from bx import align
+from bx.misc.readlengths import read_lengths_file
 
 # DNA reverse complement table
 ## DNA_COMP = "                                             -                  " \
     if contig != None: src += "[%s]" % contig
     return src
 
-# improvement: lengths file should probably be another class
-
-def read_lengths_file( name ):
-    chrom_to_length = {}
-    f = file ( name, "rt" )
-    for line in f:
-        line = line.strip()
-        if line == '' or line[0] == '#': continue
-        try:
-            fields = line.split()
-            if len(fields) != 2: raise
-            chrom = fields[0]
-            length = int( fields[1] )
-        except:
-            raise "bad length file line: %s" % line
-        if chrom in chrom_to_length and length != chrom_to_length[chrom]:
-            raise "%s has more than one length!" % chrom
-        chrom_to_length[chrom] = length
-    f.close()
-    return chrom_to_length
-
 # ---- Read C extension if available ---------------------------------------
 
 try:

lib/bx/misc/readlengths.py

+"""
+Read sequence lengths from a file.  Each line is of the form <name> <length>
+where <anme> is typically a chromsome name (e.g. chr12) and length is the
+number of bases the sequence.
+"""
+
+def read_lengths_file( name ):
+    """
+    Returns a hash from sequence name to length.
+    """
+    
+    chrom_to_length = {}
+    f = file ( name, "rt" )
+    for line in f:
+        line = line.strip()
+        if line == '' or line[0] == '#': continue
+        try:
+            fields = line.split()
+            if len(fields) != 2: raise
+            chrom = fields[0]
+            length = int( fields[1] )
+        except:
+            raise "bad length file line: %s" % line
+        if chrom in chrom_to_length and length != chrom_to_length[chrom]:
+            raise "%s has more than one length!" % chrom
+        chrom_to_length[chrom] = length
+    f.close()
+    return chrom_to_length
+