changeset 1:1e74cb2c8e67 draft

"planemo upload for repository https://github.com/ASaiM/galaxytools/tree/master/tools/format_metaphlan2_output/ commit 2cc71b230101205641d7fafa822d4ab3d398066a"
author bebatut
date Mon, 14 Sep 2020 09:52:15 +0000
parents 2bfa9b200600
children 370b56f8a02d
files format_metaphlan2_output.py format_metaphlan2_output.xml
diffstat 2 files changed, 51 insertions(+), 53 deletions(-) [+]
line wrap: on
line diff
--- a/format_metaphlan2_output.py	Wed Apr 20 07:52:41 2016 -0400
+++ b/format_metaphlan2_output.py	Mon Sep 14 09:52:15 2020 +0000
@@ -1,56 +1,52 @@
 #!/usr/bin/env python
 # -*- coding: utf-8 -*-
 
-import sys
-import os
 import argparse
-import re
+
 
-taxo_level_correspondance = {}
-taxo_level_correspondance['k'] = 'kingdom'
-taxo_level_correspondance['p'] = 'phylum'
-taxo_level_correspondance['c'] = 'class'
-taxo_level_correspondance['o'] = 'order'
-taxo_level_correspondance['f'] = 'family'
-taxo_level_correspondance['g'] = 'genus'
-taxo_level_correspondance['s'] = 'species'
-taxo_level_correspondance['t'] = 'strains'
+taxo_level_corresp = {
+    'k': 'kingdom',
+    'p': 'phylum',
+    'c': 'class',
+    'o': 'order',
+    'f': 'family',
+    'g': 'genus',
+    's': 'species',
+    't': 'strains'}
+    
 
 def write_taxo_abundance(output_files, level, taxo, abundance):
-    if not taxo_level_correspondance.has_key(level):
+    if level not in taxo_level_corresp:
         raise ValueError(level + ' is not a know taxonomic level')
-    output_files[taxo_level_correspondance[level]].write(taxo + '\t')
-    output_files[taxo_level_correspondance[level]].write(abundance + '\n')
+    f_n = taxo_level_corresp[level]
+    output_files[f_n].write(taxo + '\t')
+    output_files[f_n].write(abundance + '\n')
+
 
 def format_metaphlan2_output(args):
-    taxo_levels_abundance_files = {}
-    taxo_levels_abundance_files['kingdom'] = open(args.kingdom_abundance_file, 'w')
-    taxo_levels_abundance_files['phylum'] = open(args.phylum_abundance_file, 'w')
-    taxo_levels_abundance_files['class'] = open(args.class_abundance_file, 'w')
-    taxo_levels_abundance_files['order'] = open(args.order_abundance_file, 'w')
-    taxo_levels_abundance_files['family'] = open(args.family_abundance_file, 'w')
-    taxo_levels_abundance_files['genus'] = open(args.genus_abundance_file, 'w')
-    taxo_levels_abundance_files['species'] = open(args.species_abundance_file, 'w')
-    taxo_levels_abundance_files['strains'] = open(args.strains_abundance_file, 'w')
-
-    for taxo_level_file in taxo_levels_abundance_files:
-        taxo_levels_abundance_files[taxo_level_file].write(taxo_level_file + '\t')
-        taxo_levels_abundance_files[taxo_level_file].write('abundance\n')
+    taxo_levels_abund_f = {
+        'kingdom': open(args.kingdom_abundance_file, 'w'),
+        'phylum': open(args.phylum_abundance_file, 'w'),
+        'class': open(args.class_abundance_file, 'w'),
+        'order': open(args.order_abundance_file, 'w'),
+        'family': open(args.family_abundance_file, 'w'),
+        'genus': open(args.genus_abundance_file, 'w'),
+        'species': open(args.species_abundance_file, 'w'),
+        'strains': open(args.strains_abundance_file, 'w')
+    }
 
-    with open(args.metaphlan2_output, 'r') as input_file:
-        with open(args.all_taxo_level_abundance_file, 'w') as output_file:
-            output_file.write("kingdom\t")
-            output_file.write("phylum\t")
-            output_file.write("class\t")
-            output_file.write("order\t")
-            output_file.write("family\t")
-            output_file.write("genus\t")
-            output_file.write("species\t")
-            output_file.write("strains\t")
-            output_file.write("abundance\n")
+    for taxo_level_f in taxo_levels_abund_f:
+        s = taxo_level_f + '\t' + 'abundance\n'
+        taxo_levels_abund_f[taxo_level_f].write(s)
+
+    with open(args.metaphlan2_output, 'r') as input_f:
+        with open(args.all_taxo_level_abundance_file, 'w') as output_f:
+            s = "kingdom\tphylum\tclass\torder\tfamily\t"
+            s += "genus\tspecies\tstrains\tabundance\n"
+            output_f.write(s)
+
             levels_number = 8
-
-            for line in input_file.readlines():
+            for line in input_f.readlines():
                 if line.startswith("#"):
                     continue
 
@@ -61,23 +57,26 @@
                 split_taxo = all_taxo.split('|')
                 for level in split_taxo:
                     taxo = level.split('__')[1]
-                    taxo = taxo.replace("_"," ")
-                    output_file.write(taxo + '\t')
+                    taxo = taxo.replace("_", " ")
+                    output_f.write(taxo + '\t')
 
                 for i in range(len(split_taxo), levels_number):
-                    output_file.write('\t')
+                    output_f.write('\t')
 
-                output_file.write(abundance + "\n")
-
+                output_f.write(abundance + "\n")
 
                 last_taxo_level = split_taxo[-1].split('__')
-                taxo = last_taxo_level[1].replace("_"," ")
+                taxo = last_taxo_level[1].replace("_", " ")
                 level = last_taxo_level[0]
-                write_taxo_abundance(taxo_levels_abundance_files, level, taxo, 
+                write_taxo_abundance(
+                    taxo_levels_abund_f,
+                    level,
+                    taxo,
                     abundance)
 
-    for taxo_level_file in taxo_levels_abundance_files:
-        taxo_levels_abundance_files[taxo_level_file].close()
+    for taxo_level_f in taxo_levels_abund_f:
+        taxo_levels_abund_f[taxo_level_f].close()
+
 
 if __name__ == '__main__':
     parser = argparse.ArgumentParser()
@@ -91,7 +90,6 @@
     parser.add_argument('--genus_abundance_file', required=True)
     parser.add_argument('--species_abundance_file', required=True)
     parser.add_argument('--strains_abundance_file', required=True)
-    
     args = parser.parse_args()
 
-    format_metaphlan2_output(args)
\ No newline at end of file
+    format_metaphlan2_output(args)
--- a/format_metaphlan2_output.xml	Wed Apr 20 07:52:41 2016 -0400
+++ b/format_metaphlan2_output.xml	Mon Sep 14 09:52:15 2020 +0000
@@ -1,4 +1,4 @@
-<tool id="format_metaphlan2_output" name="Format MetaPhlAn2" version="0.1.0">
+<tool id="format_metaphlan2_output" name="Format MetaPhlAn2" version="0.2.0">
     <description>output to extract abundance at different taxonomic levels</description>
 
     <requirements>