annotate uniqprimer-0.5.0/primertools/nucmerparser.py @ 6:8218425fda1d draft default tip

Uploaded
author dereeper
date Tue, 04 Jan 2022 16:05:55 +0000
parents 3249d78ecfc2
children
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
3
3249d78ecfc2 Uploaded
dereeper
parents:
diff changeset
1 '''
3249d78ecfc2 Uploaded
dereeper
parents:
diff changeset
2 Created on Jan 1, 2011
3249d78ecfc2 Uploaded
dereeper
parents:
diff changeset
3
3249d78ecfc2 Uploaded
dereeper
parents:
diff changeset
4 @author: John L. Herndon
3249d78ecfc2 Uploaded
dereeper
parents:
diff changeset
5 @contact: herndon@cs.colostate.edu
3249d78ecfc2 Uploaded
dereeper
parents:
diff changeset
6 @organization: Colorado State University
3249d78ecfc2 Uploaded
dereeper
parents:
diff changeset
7 @group: Computer Science Department, Asa Ben-Hur's laboratory
3249d78ecfc2 Uploaded
dereeper
parents:
diff changeset
8 '''
3249d78ecfc2 Uploaded
dereeper
parents:
diff changeset
9
3249d78ecfc2 Uploaded
dereeper
parents:
diff changeset
10 import utils
3249d78ecfc2 Uploaded
dereeper
parents:
diff changeset
11 import os
3249d78ecfc2 Uploaded
dereeper
parents:
diff changeset
12 import re
3249d78ecfc2 Uploaded
dereeper
parents:
diff changeset
13
3249d78ecfc2 Uploaded
dereeper
parents:
diff changeset
14 def parseCoordMatchLine( match ):
3249d78ecfc2 Uploaded
dereeper
parents:
diff changeset
15
3249d78ecfc2 Uploaded
dereeper
parents:
diff changeset
16 match = match.replace( '\t', ' ' )
3249d78ecfc2 Uploaded
dereeper
parents:
diff changeset
17 sections = match.split( '|', 4 )
3249d78ecfc2 Uploaded
dereeper
parents:
diff changeset
18
3249d78ecfc2 Uploaded
dereeper
parents:
diff changeset
19 #parse the first section, containing the start and end
3249d78ecfc2 Uploaded
dereeper
parents:
diff changeset
20 #locations of the match
3249d78ecfc2 Uploaded
dereeper
parents:
diff changeset
21 firstsection = sections[ 0 ].strip( )
3249d78ecfc2 Uploaded
dereeper
parents:
diff changeset
22 firstsectiontokens = re.split( ' +', firstsection )
3249d78ecfc2 Uploaded
dereeper
parents:
diff changeset
23 start = int( firstsectiontokens[ 0 ].strip( ) )
3249d78ecfc2 Uploaded
dereeper
parents:
diff changeset
24 end = int( firstsectiontokens[ 1 ].strip( ) )
3249d78ecfc2 Uploaded
dereeper
parents:
diff changeset
25
3249d78ecfc2 Uploaded
dereeper
parents:
diff changeset
26 #parse the last section, containing the sequenceID
3249d78ecfc2 Uploaded
dereeper
parents:
diff changeset
27 lastsection = sections[ -1 ].strip( )
3249d78ecfc2 Uploaded
dereeper
parents:
diff changeset
28 lastsectiontokens = re.split( " +", lastsection )
3249d78ecfc2 Uploaded
dereeper
parents:
diff changeset
29
3249d78ecfc2 Uploaded
dereeper
parents:
diff changeset
30 seqid = lastsectiontokens[ 0 ].strip( )
3249d78ecfc2 Uploaded
dereeper
parents:
diff changeset
31
3249d78ecfc2 Uploaded
dereeper
parents:
diff changeset
32 return utils.Match( start, end, seqid )
3249d78ecfc2 Uploaded
dereeper
parents:
diff changeset
33
3249d78ecfc2 Uploaded
dereeper
parents:
diff changeset
34 def parseCoordMatchFile( coordFileName ):
3249d78ecfc2 Uploaded
dereeper
parents:
diff changeset
35 '''
3249d78ecfc2 Uploaded
dereeper
parents:
diff changeset
36 A method to parse the coord file.
3249d78ecfc2 Uploaded
dereeper
parents:
diff changeset
37 returns a list of utils.match objects
3249d78ecfc2 Uploaded
dereeper
parents:
diff changeset
38 '''
3249d78ecfc2 Uploaded
dereeper
parents:
diff changeset
39 returnValue = [ ]
3249d78ecfc2 Uploaded
dereeper
parents:
diff changeset
40
3249d78ecfc2 Uploaded
dereeper
parents:
diff changeset
41 #throw if the file doesn't exist
3249d78ecfc2 Uploaded
dereeper
parents:
diff changeset
42 if os.path.exists( coordFileName ) == False:
3249d78ecfc2 Uploaded
dereeper
parents:
diff changeset
43 raise utils.NoFileFoundException( coordFileName )
3249d78ecfc2 Uploaded
dereeper
parents:
diff changeset
44
3249d78ecfc2 Uploaded
dereeper
parents:
diff changeset
45
3249d78ecfc2 Uploaded
dereeper
parents:
diff changeset
46 #read the nucmer file into memory
3249d78ecfc2 Uploaded
dereeper
parents:
diff changeset
47 lines = open( coordFileName ).readlines( )
3249d78ecfc2 Uploaded
dereeper
parents:
diff changeset
48
3249d78ecfc2 Uploaded
dereeper
parents:
diff changeset
49 #skip forward to the start of the matches.
3249d78ecfc2 Uploaded
dereeper
parents:
diff changeset
50 i = 0
3249d78ecfc2 Uploaded
dereeper
parents:
diff changeset
51 while lines[ i ] [ 0] != '=':
3249d78ecfc2 Uploaded
dereeper
parents:
diff changeset
52 i += 1
3249d78ecfc2 Uploaded
dereeper
parents:
diff changeset
53 matchLines = lines[ i+1 : ]
3249d78ecfc2 Uploaded
dereeper
parents:
diff changeset
54
3249d78ecfc2 Uploaded
dereeper
parents:
diff changeset
55 #parse each line for match start, end and sequenceID
3249d78ecfc2 Uploaded
dereeper
parents:
diff changeset
56 for matchLine in matchLines:
3249d78ecfc2 Uploaded
dereeper
parents:
diff changeset
57 returnValue.append( parseCoordMatchLine( matchLine ) )
3249d78ecfc2 Uploaded
dereeper
parents:
diff changeset
58
3249d78ecfc2 Uploaded
dereeper
parents:
diff changeset
59 utils.logMessage( "NucmerParser::parseCoordMatchFile( )", "Parse {0}, finding {1} matches".format( coordFileName, len( returnValue ) ) )
3249d78ecfc2 Uploaded
dereeper
parents:
diff changeset
60
3249d78ecfc2 Uploaded
dereeper
parents:
diff changeset
61 return returnValue
3249d78ecfc2 Uploaded
dereeper
parents:
diff changeset
62
3249d78ecfc2 Uploaded
dereeper
parents:
diff changeset
63
3249d78ecfc2 Uploaded
dereeper
parents:
diff changeset
64
3249d78ecfc2 Uploaded
dereeper
parents:
diff changeset
65
3249d78ecfc2 Uploaded
dereeper
parents:
diff changeset
66
3249d78ecfc2 Uploaded
dereeper
parents:
diff changeset
67
3249d78ecfc2 Uploaded
dereeper
parents:
diff changeset
68