-
Notifications
You must be signed in to change notification settings - Fork 14
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Use pandas in the post treatment #59
Closed
Closed
Changes from 3 commits
Commits
Show all changes
17 commits
Select commit
Hold shift + click to select a range
d3b8f2c
Allow to specify a path to an index dir for msf
9102c17
fix typo
0681c86
Add pandas in all post treament files best_solution.py, df_genes.py, …
FlorianTesson 6576132
change +'/'+ by oS.path.join, change system table creation and fix Ca…
FlorianTesson 16af264
fix df_hmmer.py and df_systems.py (add sort by position)
FlorianTesson 3a68121
change tests files
FlorianTesson 5f3230f
fix index-dir missing dir
9a88433
add defensefinder version
e1adfd9
Add pandas in requirement
FlorianTesson 096cd0e
Merge branch 'posttreat_pandas' of github.com:mdmparis/defense-finder…
FlorianTesson e5f4483
correct to pandas any version
FlorianTesson fdfbacd
Moove to defense-finder-models
FlorianTesson b45a5e9
change readme
FlorianTesson 96f1439
faster pandas version
d0ec072
Merge branch 'posttreat_pandas' of github.com:mdmparis/defense-finder…
FlorianTesson 9f9dac7
add possibility to run as script, for profiling purposes
f559d44
Merge branch 'posttreat_pandas' of github.com:mdmparis/defense-finder…
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,23 +1,11 @@ | ||
import os | ||
import csv | ||
from defense_finder_posttreat import best_solution | ||
|
||
def export_defense_finder_genes(defense_finder_genes, outdir, filename): | ||
defense_finder_genes_list = defense_finder_genes_to_list(defense_finder_genes) | ||
write_defense_finder_genes(defense_finder_genes_list, outdir, filename) | ||
defense_finder_genes.to_csv(outdir+'/'+filename+'_defense_finder_genes.tsv',sep='\t',index=False) | ||
|
||
def defense_finder_genes_to_list(defense_finder_genes): | ||
header = best_solution.get_best_solution_keys() | ||
out = [header] | ||
for g in defense_finder_genes: | ||
l = [] | ||
for key in header: | ||
l.append(g[key]) | ||
out.append(l) | ||
return out | ||
|
||
def write_defense_finder_genes(defense_finder_genes_list, outdir, filename): | ||
filepath = os.path.join(outdir, f'{filename}_defense_finder_genes.tsv') | ||
with open(filepath, 'w') as defense_finder_genes_file: | ||
write = csv.writer(defense_finder_genes_file, delimiter='\t') | ||
write.writerows(defense_finder_genes_list) | ||
defense_finder_genes_list.to_csv('filepath',sep='\t',index=False) | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,49 +1,27 @@ | ||
import os | ||
import csv | ||
from itertools import groupby | ||
from functools import reduce | ||
import pandas as pd | ||
|
||
def export_defense_finder_systems(defense_finder_genes, outdir, filename): | ||
systems = build_defense_finder_systems(defense_finder_genes) | ||
systems_list = systems_to_list(systems) | ||
write_defense_finder_systems(systems_list, outdir, filename) | ||
systems.to_csv(outdir+'/'+filename+'_defense_finder_systems.tsv',sep='\t',index=False) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. use os.path.join |
||
|
||
def systems_to_list(systems): | ||
header = get_system_keys() | ||
out = [header] | ||
for s in systems: | ||
l = [] | ||
for key in header: | ||
l.append(s[key]) | ||
out.append(l) | ||
return out | ||
|
||
def write_defense_finder_systems(systems_list, outdir, filename): | ||
filepath = os.path.join(outdir, f'{filename}_defense_finder_systems.tsv') | ||
with open(filepath, 'w') as defense_finder_systems_file: | ||
write = csv.writer(defense_finder_systems_file, delimiter='\t') | ||
write.writerows(systems_list) | ||
def build_defense_finder_systems(defense_finder_genes): | ||
sys=defense_finder_genes.drop_duplicates('sys_id')[['sys_id' , 'type' , 'subtype']] | ||
|
||
def get_system_keys(): | ||
return [ 'sys_id', 'type', 'subtype', 'sys_beg', 'sys_end', 'protein_in_syst', 'genes_count', 'name_of_profiles_in_sys' ] | ||
sys_beg=defense_finder_genes.sort_values('hit_pos').drop_duplicates('sys_id').rename({'hit_id' : 'sys_beg'},axis=1)[['sys_id','sys_beg']] | ||
sys_end=defense_finder_genes.sort_values('hit_pos' , ascending=False).drop_duplicates('sys_id').rename({'hit_id' : 'sys_end'},axis=1)[['sys_id','sys_end']] | ||
protein_in_syst=defense_finder_genes.groupby('sys_id').hit_id.apply(lambda x: ",".join(x.sort_values())).reset_index().rename({'hit_id':'protein_in_syst'},axis=1) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. simplify to get sys_beg and sys_end from protein in syst |
||
name_of_profiles_in_sys=defense_finder_genes.groupby('sys_id').gene_name.apply(lambda x : ",".join(x.sort_values())).reset_index().rename({'hit_id' : 'protein_in_syst'},axis = 1) | ||
genes_count=defense_finder_genes.sys_id.value_counts().reset_index() | ||
genes_count.columns=['sys_id','genes_count'] | ||
|
||
def projection(val): | ||
return val['sys_id'] | ||
|
||
def build_defense_finder_systems(defense_finder_genes): | ||
system_goups = [list(it) for k, it in groupby(defense_finder_genes, projection)] | ||
out = [] | ||
for system_group in system_goups: | ||
item = {} | ||
first_item = system_group[0] | ||
last_item = system_group[-1] | ||
item['sys_id'] = first_item['sys_id'] | ||
item['sys_beg'] = first_item['hit_id'] | ||
item['sys_end'] = last_item['hit_id'] | ||
item['type'] = first_item['type'] | ||
item['subtype'] = first_item['subtype'] | ||
item['protein_in_syst'] = reduce(lambda acc, s: acc + ',' + s['hit_id'], system_group, '')[1:] | ||
item['genes_count'] = len(system_group) | ||
item['name_of_profiles_in_sys'] = reduce(lambda acc, s: acc + ',' + s['gene_name'], system_group, '')[1:] | ||
out.append(item) | ||
return out | ||
out=sys.merge(sys_beg,on = 'sys_id') | ||
out=out.merge(sys_end,on = 'sys_id') | ||
out=out.merge(protein_in_syst,on = 'sys_id') | ||
out=out.merge(genes_count,on = 'sys_id') | ||
out=out.merge(name_of_profiles_in_sys,on = 'sys_id') | ||
|
||
return(out) | ||
|
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
idem os.path.join