Skip to content
GitLab
Explore
Sign in
Primary navigation
Search or go to…
Project
P
panacota
Manage
Activity
Members
Labels
Plan
Issues
Issue boards
Milestones
Code
Merge requests
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Snippets
Build
Pipelines
Jobs
Pipeline schedules
Artifacts
Deploy
Releases
Container registry
Model registry
Operate
Environments
Monitor
Incidents
Analyze
Value stream analytics
Contributor analytics
CI/CD analytics
Repository analytics
Model experiments
Help
Help
Support
GitLab documentation
Compare GitLab plans
Community forum
Contribute to GitLab
Provide feedback
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
Amandine PERRIN
panacota
Commits
d3b5c62b
Commit
d3b5c62b
authored
8 years ago
by
Amandine PERRIN
Browse files
Options
Downloads
Patches
Plain Diff
Add grep and count functions to utils
parent
68c2fe6b
No related branches found
No related tags found
No related merge requests found
Changes
1
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
genomeAPCAT/utils.py
+38
-0
38 additions, 0 deletions
genomeAPCAT/utils.py
with
38 additions
and
0 deletions
genomeAPCAT/utils.py
+
38
−
0
View file @
d3b5c62b
...
...
@@ -11,6 +11,7 @@ April 2017
import
os
import
sys
import
re
import
glob
import
logging
from
logging.handlers
import
RotatingFileHandler
...
...
@@ -487,6 +488,43 @@ def cat(list_files, output, title = None):
bar
.
finish
()
def
grep
(
filein
,
pattern
,
count
=
False
):
"""
By default, returns all the lines containing the given pattern.
If count = True, returns the number of lines containing the pattern.
"""
num
=
0
lines
=
[]
with
open
(
filein
,
"
r
"
)
as
inf
:
for
line
in
inf
:
if
re
.
search
(
pattern
,
line
):
lines
.
append
(
line
.
strip
())
num
+=
1
if
count
:
return
num
else
:
return
lines
def
count
(
filein
,
get
=
"
lines
"
):
"""
Count the number of what is given in
'
get
'
. It can be:
- lines (default)
- words
"""
gets
=
[
"
lines
"
,
"
words
"
]
if
get
not
in
gets
:
logger
.
error
(
"
Choose what you want to count among {}.
"
.
format
(
gets
))
num
=
0
with
open
(
filein
,
"
r
"
)
as
inf
:
for
line
in
inf
:
if
get
==
"
lines
"
:
num
+=
1
elif
get
==
"
words
"
:
num
+=
len
(
line
.
strip
())
return
num
def
check_format
(
info
):
"""
Check that the given information (can be the genomes name or the date) is in the right
...
...
This diff is collapsed.
Click to expand it.
Preview
0%
Loading
Try again
or
attach a new file
.
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Save comment
Cancel
Please
register
or
sign in
to comment