Repository 'imgt_locus_split'
hg clone https://toolshed.g2.bx.psu.edu/repos/davidvanzessen/imgt_locus_split

Changeset 1:418b7dbc8947 (2017-07-17)
Previous changeset 0:b00c257f0a67 (2017-07-13) Next changeset 2:4bb8f6523130 (2019-03-05)
Commit message:
Uploaded
modified:
imgt_locus_split.py
imgt_locus_split.xml
b
diff -r b00c257f0a67 -r 418b7dbc8947 imgt_locus_split.py
--- a/imgt_locus_split.py Thu Jul 13 10:24:39 2017 -0400
+++ b/imgt_locus_split.py Mon Jul 17 08:54:02 2017 -0400
[
@@ -44,8 +44,8 @@
             shutil.rmtree(check)
 
 
-def filter_tabular_file(old_file, new_file, column, regex):
-    logging.debug("Filtering {0} with {1}".format(old_file, regex.pattern))
+def filter_imgt_file(old_file, new_file, column, fltr):
+    logging.debug("Filtering {0} with {1}".format(old_file, fltr))
     first = True
     total = 0
     remain = 0
@@ -59,7 +59,7 @@
                 nf.write(line)
                 continue
             total += 1
-            if len(splt) >= column_index and regex.search(splt[column_index]):
+            if len(splt) > column_index and splt[column_index].find(fltr) != -1:
                 remain += 1
                 nf.write(line)
     return total, remain
@@ -69,22 +69,21 @@
     return all(l[0] == x for x in l[1:])
 
 
-def filter_imgt_dir(imgt_dir, loci):
-    logging.info("Filtering {0} with {1}".format(imgt_dir, loci))
+def filter_imgt_dir(imgt_dir, locus):
+    logging.info("Working on {0}".format(locus))
     imgt_files = [f for f in os.listdir(imgt_dir) if imgt_file_regex.match(f)]
     tmp_file = os.path.join(imgt_dir, "tmp.txt")
     totals = []
     remains = []
-    loci_regex = re.compile("|".join(loci))
     for imgt_file in imgt_files:
         imgt_file = os.path.join(imgt_dir, imgt_file)
-        total, remain = filter_tabular_file(imgt_file, tmp_file, "V-GENE and allele", loci_regex)
+        total, remain = filter_imgt_file(imgt_file, tmp_file, "V-GENE and allele", locus)
         totals.append(total)
         remains.append(remain)
         logging.debug("{0} rows, {1} after filtering".format(total, remain))
         shutil.move(tmp_file, imgt_file)
     if not (all_same_in_list(totals) and all_same_in_list(remains)):
-        logging.warning("Not all files had the same number of sequences remaining for {0}: {1}".format(imgt_dir, remains))
+        logging.warning("Not all files had the same number of sequences remaining for {0}".format(imgt_dir))
     return totals[0], remains[0]
 
 
@@ -100,9 +99,16 @@
 
 def main():
     parser = argparse.ArgumentParser()
-    parser.add_argument("-i", "--input", help="The input IMGT file", required=True)
-    parser.add_argument("-l", "--loci", help="The Loci to filter on", required=True)
-    parser.add_argument("-o", "--output", help="The output file for the new IMGT zip with just the filtered sequences", required=True)
+    parser.add_argument("--input", help="The input IMGT file", required=True)
+    parser.add_argument("--output-ig", help="The output file for new IMGT ZIP with just IG sequences", default="None")
+    parser.add_argument("--output-igh", help="The output file for new IMGT ZIP with just IGH sequences", default="None")
+    parser.add_argument("--output-igk", help="The output file for new IMGT ZIP with just IGK sequences", default="None")
+    parser.add_argument("--output-igl", help="The output file for new IMGT ZIP with just IGL sequences", default="None")
+    parser.add_argument("--output-tr", help="The output file for new IMGT ZIP with just TR sequences", default="None")
+    parser.add_argument("--output-tra", help="The output file for new IMGT ZIP with just TRA sequences", default="None")
+    parser.add_argument("--output-trb", help="The output file for new IMGT ZIP with just TRB sequences", default="None")
+    parser.add_argument("--output-trd", help="The output file for new IMGT ZIP with just TRD sequences", default="None")
+    parser.add_argument("--output-trg", help="The output file for new IMGT ZIP with just TRG sequences", default="None")
 
     logging.basicConfig(filename="./log.html", level=logging.DEBUG, format="%(asctime)s:&emsp;%(message)s <br />",
                         datefmt='%Y/%m/%d %H:%M:%S')
@@ -111,27 +117,57 @@
 
     args = parser.parse_args()
     input_file = args.input
-    loci = args.loci.split(",")
-    output_file = args.output
+    output_ig = args.output_ig
+    output_igh = args.output_igh
+    output_igk = args.output_igk
+    output_igl = args.output_igl
+    output_tr = args.output_tr
+    output_tra = args.output_tra
+    output_trb = args.output_trb
+    output_trd = args.output_trd
+    output_trg = args.output_trg
+
+    loci = {
+        "IG": output_ig,
+        "IGH": output_igh,
+        "IGK": output_igk,
+        "IGL": output_igl,
+        "TR": output_tr,
+        "TRA": output_tra,
+        "TRB": output_trb,
+        "TRD": output_trd,
+        "TRG": output_trg
+    }
+
+    loci_to_filter = {}
 
     logging.debug("All Parameters:")
     logging.debug("Input: {0}".format(input_file))
-    logging.debug("Loci: {0}".format(loci))
-    logging.debug("Output: {0}".format(output_file))
+    for locus, path in loci.items():
+        logging.debug("{0}: {1}".format(locus, path))
+        if path != "None" and os.path.isdir(os.path.split(path)[0]):
+            loci_to_filter[locus] = path
 
-    if len(loci) == 0:
+    if len(loci_to_filter) == 0:
         raise Exception("No locus selected, nothing to do")
 
+    logging.info("Parameters:")
+    for locus, path in loci_to_filter.items():
+        logging.info("{0}: {1}".format(locus, path))
+
     work_dir = tempfile.mkdtemp()
     original_files_dir = os.path.join(work_dir, "original")
     os.mkdir(original_files_dir)
 
     unpack_imgt_zip(input_file, original_files_dir)
 
-    total, remain = filter_imgt_dir(original_files_dir, loci)
-    logging.info("{0}\t{1}".format(total, remain))
+    for locus, path in loci_to_filter.items():
+        locus_dir = os.path.join(work_dir, locus)
+        shutil.copytree(original_files_dir, locus_dir)
+        total, remain = filter_imgt_dir(locus_dir, locus)
+        logging.info("{0}\t{1}\t{2}\t{3}".format(locus, path, total, remain))
 
-    make_new_xz_file(original_files_dir, output_file)
+        make_new_xz_file(locus_dir, loci_to_filter[locus])
 
 
 if __name__ == "__main__":
b
diff -r b00c257f0a67 -r 418b7dbc8947 imgt_locus_split.xml
--- a/imgt_locus_split.xml Thu Jul 13 10:24:39 2017 -0400
+++ b/imgt_locus_split.xml Mon Jul 17 08:54:02 2017 -0400
[
@@ -4,9 +4,16 @@
     </requirements>
     <command detect_errors="exit_code"><![CDATA[
         python3 $__tool_directory__/imgt_locus_split.py 
-            --input $input
-            --loci $loci
-            --output $output
+            --input $input 
+            --output-ig ${output_ig}
+            --output-igh ${output_igh}
+            --output-igk ${output_igk}
+            --output-igl ${output_igl}
+            --output-tr ${output_tr}
+            --output-tra ${output_tra}
+            --output-trb ${output_trb}
+            --output-trd ${output_trd}
+            --output-trg ${output_trg}
     ]]></command>
     <inputs>
         <param name="input" type="data" format="data" label="The IMGT zip file to be split"/>
@@ -23,7 +30,33 @@
         </param>
     </inputs>
     <outputs>
-        <data format="imgt_archive" name="output" label="${input.name} $loci" />
+        <data format="imgt_archive" name="output_ig" label="Filtered IMGT IG: ${input.name}">
+            <filter>loci.__contains__('IG')</filter>
+        </data>
+        <data format="imgt_archive" name="output_igh" label="Filtered IMGT IGH: ${input.name}">
+            <filter>loci.__contains__('IGH')</filter>
+        </data>
+        <data format="imgt_archive" name="output_igk" label="Filtered IMGT IGK: ${input.name}">
+            <filter>loci.__contains__('IGK')</filter>
+        </data>
+        <data format="imgt_archive" name="output_igl" label="Filtered IMGT IGL: ${input.name}">
+            <filter>loci.__contains__('IGL')</filter>
+        </data>
+        <data format="imgt_archive" name="output_tr" label="Filtered IMGT TR: ${input.name}">
+            <filter>loci.__contains__('TR')</filter>
+        </data>
+        <data format="imgt_archive" name="output_tra" label="Filtered IMGT TRA: ${input.name}">
+            <filter>loci.__contains__('TRA')</filter>
+        </data>
+        <data format="imgt_archive" name="output_trb" label="Filtered IMGT TRB: ${input.name}">
+            <filter>loci.__contains__('TRB')</filter>
+        </data>
+        <data format="imgt_archive" name="output_trd" label="Filtered IMGT TRD: ${input.name}">
+            <filter>loci.__contains__('TRD')</filter>
+        </data>
+        <data format="imgt_archive" name="output_trg" label="Filtered IMGT TRG: ${input.name}">
+            <filter>loci.__contains__('TRG')</filter>
+        </data>
     </outputs>
     <help><![CDATA[
         Creates a new IMGT zip file for every checked locus with sequences from only that locus.