Python Forum
Using regex for type validation
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Using regex for type validation
#4
You would get a better structured code with a function to handle each line
def is_vcf_line(line):
    columns = line.split('\t')
    return bool(
        re.match(r"^chr(?:[1-9][0-9]?|[XYM])$", columns[0]) 
        and re.match(r"^[1-9][0-9]*$", columns[1])
        and re.match(r"^[ATGC]$", columns[3]) 
        and re.match(r"^[ATGC]$", columns[4]))

def is_vcf(filename):
    with open(filename) as lines:
        return all(is_vcf_line(line) for line in lines)
Reply


Messages In This Thread
Using regex for type validation - by ranbarr - May-21-2021, 04:53 PM
RE: Using regex for type validation - by ranbarr - May-21-2021, 08:16 PM
RE: Using regex for type validation - by Gribouillis - May-22-2021, 07:35 AM
RE: Using regex for type validation - by ranbarr - May-22-2021, 09:39 AM

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020