Skip to content

Commit 0f2090f

Browse files
author
Bill Majoros
committedJan 16, 2017
NgramIterator.py
1 parent cf453e9 commit 0f2090f

File tree

5 files changed

+128
-3
lines changed

5 files changed

+128
-3
lines changed
 

‎BedReader.py

+13-1
Original file line numberDiff line numberDiff line change
@@ -16,8 +16,9 @@
1616
# fh : file handle
1717
# Instance Methods:
1818
# reader=BedReader(filename)
19-
# reader.close()
2019
# record=reader.nextRecord() # Bed3Record or Bed6Record
20+
# reader.close()
21+
# list=BedReader.readAll(filename)
2122
# Class Methods:
2223
#
2324
#=========================================================================
@@ -26,6 +27,17 @@ class BedReader:
2627
def __init__(self,filename):
2728
self.fh=open(filename,"r")
2829

30+
@classmethod
31+
def readAll(cls,filename):
32+
reader=BedReader(filename)
33+
array=[]
34+
while(True):
35+
record=reader.nextRecord()
36+
if(not record): break
37+
array.append(record)
38+
reader.close()
39+
return array
40+
2941
def close(self):
3042
self.fh.close()
3143

‎FastaReader.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@
2525
# size=FastaReader.getSize(filename)
2626
# FastaReader.readAll(filename) # returns hash : id->sequence
2727
# FastaReader.readAllAndKeepDefs(filename) # returns hash : id->[def,seq]
28-
# seq=FastaReader.firstSequence(filename)
28+
# (defline,seq)=FastaReader.firstSequence(filename)
2929
# [id,attribute_hash]=FastaReader.parseDefline(defline)
3030
#=========================================================================
3131
class FastaReader:

‎FastbTrack.py

+18-1
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@
2222
# data : string (for discrete) or array of float (for continuous)
2323
# deflineExtra : extra info for defline
2424
# Methods:
25-
# track=FastbTrack(type,id,data,deflineExtra); # $type="discrete" or "continuous"
25+
# track=FastbTrack(type,id,data,deflineExtra); # type="discrete" or "continuous"
2626
# type=track.getType()
2727
# data=track.getData()
2828
# track.setSequence(string) # discrete
@@ -41,6 +41,7 @@
4141
# "value" attribute added
4242
# newTrack=track.slice(begin,end) # [begin,end) => end not inclusive
4343
# meanValue=track.getMean(interval=None) # only for continuous data
44+
# (maxValue,maxPos)=track.getMax(interval=None) # only for continuous data
4445
######################################################################
4546

4647
class FastbTrack:
@@ -177,6 +178,22 @@ def getMean(self,interval=None):
177178
mean=sum/L
178179
return mean
179180

181+
def getMax(self,interval=None):
182+
data=self.data
183+
L=len(data)
184+
begin=0
185+
end=L
186+
if(interval is not None):
187+
begin=interval.begin
188+
end=interval.end
189+
L=end-begin
190+
max=data[begin]; maxPos=begin
191+
for i in range(begin+1,end):
192+
if(data[i]>max):
193+
max=data[i]
194+
maxPos=i
195+
return (max,maxPos)
196+
180197
def anyZeroValues(self):
181198
data=self.data
182199
for x in data:

‎NgramIterator.py

+90
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,90 @@
1+
#!/usr/bin/env python
2+
#=========================================================================
3+
# This is OPEN SOURCE SOFTWARE governed by the Gnu General Public
4+
# License (GPL) version 3, as described at www.opensource.org.
5+
# Copyright (C)2017 William H. Majoros (martiandna@gmail.com).
6+
#=========================================================================
7+
from __future__ import (absolute_import, division, print_function,
8+
unicode_literals, generators, nested_scopes, with_statement)
9+
from builtins import (bytes, dict, int, list, object, range, str, ascii,
10+
chr, hex, input, next, oct, open, pow, round, super, filter, map, zip)
11+
# The above imports should allow this program to run in both Python 2 and
12+
# Python 3. You might need to update your version of module "future".
13+
14+
######################################################################
15+
#
16+
# NgramIterator.py bmajoros
17+
#
18+
# Attributes:
19+
# array ngram : array of integer indices into alphabet string
20+
# string alphabet
21+
# Methods:
22+
# ngramIterator=NgramIterator("ATCG",N)
23+
# string=ngramIterator.nextString() # returns None if no more
24+
# ngramIterator.reset()
25+
# Private methods:
26+
# self.ngramToString()
27+
######################################################################
28+
29+
class NgramIterator:
30+
#---------------------------------------------------------------------
31+
# PUBLIC METHODS
32+
#---------------------------------------------------------------------
33+
# ngramIterator=NgramIterator("ATCG",N)
34+
def __init__(self,alphabet,N):
35+
ngram=[]
36+
alphaSize=len(alphabet)
37+
for i in range(N-1): ngram.append(0)
38+
if(N>0): ngram.append(-1)
39+
self.alphabet=alphabet
40+
self.ngram=ngram
41+
#---------------------------------------------------------------------
42+
# ngramIterator.reset()
43+
def reset(self):
44+
ngram=self.ngram
45+
L=len(ngram)
46+
for i in range(L-1): ngram[i]=0
47+
ngram[L-1]=-1
48+
#---------------------------------------------------------------------
49+
# string=ngramIterator.nextString() # returns undef if no more
50+
def nextString(self):
51+
alphabet=self.alphabet
52+
ngram=self.ngram
53+
if(ngram is None): return None
54+
L=len(ngram)
55+
if(L==0):
56+
self.ngram=None
57+
return ""
58+
alphaSize=len(alphabet)
59+
for i in range(L-1,-1,-1): # for($i=$len-1 ; $i>=0 ; --$i)
60+
ngram[i]+=1
61+
index=ngram[i]
62+
if(index<alphaSize): return self.ngramToString()
63+
ngram[i]=0
64+
return None
65+
#---------------------------------------------------------------------
66+
#---------------------------------------------------------------------
67+
#---------------------------------------------------------------------
68+
#---------------------------------------------------------------------
69+
#---------------------------------------------------------------------
70+
71+
72+
73+
74+
75+
76+
#---------------------------------------------------------------------
77+
# PRIVATE METHODS
78+
#---------------------------------------------------------------------
79+
# self.ngramToString()
80+
def ngramToString(self):
81+
ngram=self.ngram
82+
alphabet=self.alphabet
83+
length=len(ngram)
84+
string=""
85+
for i in range(length):
86+
index=ngram[i]
87+
string+=alphabet[index:index+1]
88+
return string
89+
90+

‎template.py

+6
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,12 @@
1010
chr, hex, input, next, oct, open, pow, round, super, filter, map, zip)
1111
# The above imports should allow this program to run in both Python 2 and
1212
# Python 3. You might need to update your version of module "future".
13+
import sys
14+
import ProgramName
15+
16+
if(len(sys.argv)!=2):
17+
exit(ProgramName.get()+" <>\n")
18+
()=sys.argv[1:]
1319

1420

1521

0 commit comments

Comments
 (0)
Please sign in to comment.