# HG changeset patch
# User holtgrewe
# Date 1367858083 14400
# Node ID 0ac4f6f3d984f8fd7f31bbac8f26e87924b4639a
# Parent  61d9bdb6d5195738557c70ac3b050a4addf62288
Uploaded
diff -r 61d9bdb6d519 -r 0ac4f6f3d984 bam2roi.xml
--- a/bam2roi.xml	Thu Apr 18 08:03:38 2013 -0400
+++ b/bam2roi.xml	Mon May 06 12:34:43 2013 -0400
@@ -28,9 +28,9 @@
                help="When selected, a ROI continues over skipped bases." />
         
+               help="When selected, the strands of the reads alignments are considered, e.g. there can be two or more ROIs on different strands that would overlap on the same strand." />
 
-        
+        
             
             
             
diff -r 61d9bdb6d519 -r 0ac4f6f3d984 gff_sort.sh
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/gff_sort.sh	Mon May 06 12:34:43 2013 -0400
@@ -0,0 +1,64 @@
+#!/bin/bash
+
+# Sorting of GFF files.
+#
+# USAGE: sort_gff.sh [OPTIONS] -i IN.roi -o OUT.roi
+#
+# Options:
+#   -r     reverse orientation
+#   -p     sort by position (ref, start, end) -- default
+
+# The parameters that we will pass to sort.
+SORT_POS_ARGS="-k 1,1 -k 2,2n -k 3,3n"
+SORT_POS_ARGS_REV="-k 1,1r -k 2,2nr -k 3,3nr"
+
+# The arguments will go here.
+SORT_BY=beginPos
+REVERSE=
+
+# Parse option values.
+while getopts "pc:i:o:n:r" opt; do
+    case $opt in
+        i)
+            IN_FILE=$OPTARG
+            ;;
+        o)
+            OUT_FILE=$OPTARG
+            ;;
+        r)
+            REVERSE=r
+            ;;
+        \?)
+            echo "Invalid option: -$OPTARG" >&2
+            exit 1
+            ;;
+    esac
+done
+
+# Check that -i or -o are given.
+if [[ "$IN_FILE" == "" || "$OUT_FILE" == "" ]]; then
+    echo "Missing option -i or -o" >&2
+    exit 1
+fi
+
+# Setup sort args.
+case $SORT_BY in
+    beginPos)
+        if [[ "$REVERSE" == "r" ]]; then
+            SORT_ARGS=${SORT_POS_ARGS_REV}
+        else
+            SORT_ARGS=${SORT_POS_ARGS}
+        fi
+        ;;
+esac
+
+# Execute sorting.
+#echo "OUT_FILE=${OUT_FILE}" 2>&2
+#echo "SORT_ARGS=${SORT_ARGS}" 1>&2
+(
+    export LC_ALL=C
+    #echo "sort ${SORT_ARGS} <(grep -v '^#' ${IN_FILE});" 1>&2
+    sort ${SORT_ARGS} <(grep -v '^#' ${IN_FILE});
+) > ${OUT_FILE}
+
+exit $?
diff -r 61d9bdb6d519 -r 0ac4f6f3d984 ngs_roi/app.py
--- a/ngs_roi/app.py	Thu Apr 18 08:03:38 2013 -0400
+++ b/ngs_roi/app.py	Mon May 06 12:34:43 2013 -0400
@@ -57,6 +57,8 @@
                     '--spacing', self.args.spacing]
         cmd_args = ['roi_plot_thumbnails'] + map(str, cmd_args)
         #import pdb; pdb.set_trace()
+        import sys
+        print >>sys.stderr, 'Running %s' % ' '.join(cmd_args)
         p = subprocess.Popen(cmd_args, stderr=subprocess.PIPE, stdout=subprocess.PIPE)
         res = p.wait()
         if res:
diff -r 61d9bdb6d519 -r 0ac4f6f3d984 roi_details.py
--- a/roi_details.py	Thu Apr 18 08:03:38 2013 -0400
+++ b/roi_details.py	Mon May 06 12:34:43 2013 -0400
@@ -12,28 +12,51 @@
 import Cheetah.Template
 import matplotlib.pyplot as plt
 
-import rois
+import ngs_roi.app
+import ngs_roi.argparse
+import ngs_roi.io
 
 PAGE_TPL = """
 
-  
ROI Table
+  
+    ROI Table
+    
+  
   
     Detailed ROI Report
+
     #for i, roi in enumerate($records)
-    ${roi.ref}:${roi.start_pos + 1}-${roi.end_pos+1}
-    
-    
-      - chr:start-end name length strand
 
-      - ${roi.ref}:${roi.start_pos}-${roi.end_pos} ${roi.region_name} ${roi.region_length} ${roi.strand}
 
-      - metrics
 
-      - #for j, key in enumerate($data_keys)#$key: ${roi.data[$j]}, #end for#
 
-    
+    
+      
${roi.ref}:${roi.start_pos + 1}-${roi.end_pos+1}
+      

+      
+        chr:start-end ${roi.ref}:${roi.start_pos}-${roi.end_pos} ${roi.strand};
+        region name ${roi.region_name};
+        region length ${roi.region_length};
+      
+      #if $roi.data
+      
#for j, key in enumerate($data_keys)#$key: ${roi.data[$j]}; #end for#
+      #end if
+    
 
     #end for
+    
+    $args
   
 
 """
 
-class DetailedRoiGenerator(object):
+class DetailedRoiGenerator(ngs_roi.app.App):
     """Generate detailed ROI report.
 
     :ivar args:Arguments from the comment line.
@@ -48,28 +71,40 @@
         :return: integer with the result.
         """
         print >>sys.stderr, 'Loading ROI'
-        keys, records = rois.loadRoi(self.args.in_file, self.args.max_rois)
+        records = ngs_roi.io.load(self.args.in_file, self.args.max_rois)
+        keys = records[0].data_keys
 
         self.writeHtml(keys, records)
         self.writePlots(records)
         return 0
 
     def writePlots(self, records):
+        COLOR = 'blue'
+        LINE_WIDTH = .5
+        LINE_STYLE = '-'
+        TICK_FONT_SIZE = 8
+        LABEL_FONT_SIZE = 10
         for i, roi in enumerate(records):
-            file_name = '%s%d.png' % (os.path.join(self.args.out_dir, self.args.out_prefix), i)
+            file_name = 'plot_%d.png' % i
+            file_name = os.path.join(self.args.out_dir, file_name)
             print >>sys.stderr, 'Writing plot %s' % file_name
-            plt.figure(figsize=(6, 4))
-            plt.gcf().subplots_adjust(bottom=0.1, left=0.1)
-            plt.plot(roi.points)
-            plt.ylabel('coverage')
-            plt.xlabel('ROI pos')
+            plt.figure(figsize=(4, 2.5))
+            plt.gcf().subplots_adjust(bottom=0.16, left=0.15)
+            plt.plot(roi.points, color=COLOR, linewidth=LINE_WIDTH, linestyle=LINE_STYLE)
+            plt.ylim(ymin=0)
+            if self.args.max_value:
+                plt.ylim(ymax=self.args.max_value)
+            plt.tick_params(labelsize=TICK_FONT_SIZE)
+            plt.ylabel('coverage', fontsize=LABEL_FONT_SIZE, weight='semibold')
+            plt.xlabel('ROI beginPos', fontsize=LABEL_FONT_SIZE, weight='semibold')
             plt.savefig(file_name)
 
     def writeHtml(self, keys, records):
         file_name = self.args.out_file
         print >>sys.stderr, 'Writing HTML file %s' % file_name
 
-        vals = {'args': self.args, 'records': records, 'data_keys': keys}
+        vals = {'args': self.args, 'records': records, 'data_keys': keys,
+                'href': lambda x: self.buildHref(x.ref, x.start_pos, x.end_pos)}
         t = Cheetah.Template.Template(PAGE_TPL, searchList=vals)
         
         with open(file_name, 'wb') as f:
@@ -78,64 +113,12 @@
 
 def main():
     parser = argparse.ArgumentParser(description='Plot ROI file.')
-    parser.add_argument('-o', '--out-file', dest='out_file', metavar='PATH',
-                        help='Prefix of output HTML file.  The tool will create '
-                        'PNG files parallel to the HTML file.',
-                        default='roi_plots.html')
-    parser.add_argument('-d', '--out-dir', dest='out_dir', metavar='DIR',
-                        help='Directory to write images to.  By default '
-                        'this is the same as the directory for --out-file.',
-                        default=None)
-    parser.add_argument('-p', '--out-prefix', dest='out_prefix', metavar='DIR',
-                        help='Prefix of output PNG files.', default='roi_plot_grid')
-    parser.add_argument('-i', '--in-file', dest='in_file', metavar='FILE',
-                        required=True, help='Path to ROI file to read.')
-
-    group = parser.add_argument_group('Plot Configuration', 'Arguments for the plot images.')
-
-    group.add_argument('--max-rois', dest='max_rois', metavar='NUM',
-                        type=int, default=0,
-                        help='Maximal number of ROIs, 0 for all.')
+    ngs_roi.argparse.addFileArguments(parser)
+    ngs_roi.argparse.addPlotGridArguments(parser)
+    ngs_roi.argparse.addLinkArguments(parser)
+    args = parser.parse_args()
+    ngs_roi.argparse.applyFileDefaults(args)
 
-    group.add_argument('--num-rows', dest='num_rows', metavar='ROWS',
-                        type=int, default=50,
-                        help='Number of rows per grid.')
-    group.add_argument('--num-cols', dest='num_cols', metavar='COLS',
-                       type=int, default=40,
-                       help='Number of columns per grid.')
-    
-    group.add_argument('--plot-height', dest='plot_height', metavar='HEIGHT',
-                       type=int, default=30, help='Height of one plot in px.')
-    group.add_argument('--plot-width', dest='plot_width', metavar='WIDTH',
-                       type=int, default=30, help='Width of one plot in px.')
-
-    group = parser.add_argument_group('HTML Links', 'Arguments for HTML link creation.')
-
-    group.add_argument('--link-target', dest='link_target', metavar='TARGET',
-                       default='_blank', choices=['_blank', '_top'],
-                       help='Select the link target to create (_blank or _top).')
-    
-    group.add_argument('--link-type', dest='link_type', metavar='TARGET',
-                       default='local_igv', choices=['local_igv', 'ucsc'],
-                       help='Select the type of links to create.  One of '
-                       '"local_igv" and "ucsc".')
-    
-    group.add_argument('--igv-host', dest='igv_host', metavar='HOST',
-                       default='localhost', help='Host for IGV link.')
-    group.add_argument('--igv-port', dest='igv_port', metavar='PORT',
-                       type=int, default='60151', help='Port for IGV link.')
-
-    group.add_argument('--ucsc-org', dest='ucsc_org', metavar='ORG',
-                       default='human', help='Organism for UCSC browser link.')
-    group.add_argument('--ucsc-db', dest='ucsc_db', metavar='DB',
-                       default='hg18', help='Assembly version for UCSC browser link.')
-    group.add_argument('--ucsc-chr-prefix', dest='ucsc_chr_prefix', metavar='PREFIX',
-                       default='', help='Prefix for chromosome names in UCSC browser.')
-
-    args = parser.parse_args()
-    if not args.out_dir:
-        args.out_dir = os.path.dirname(args.out_file) or '.'
-    print args
     app = DetailedRoiGenerator(args)
     return app.run()
 
diff -r 61d9bdb6d519 -r 0ac4f6f3d984 roi_details.xml
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/roi_details.xml	Mon May 06 12:34:43 2013 -0400
@@ -0,0 +1,65 @@
+
+
+    ROI Details
+
+    
+    roi_details.py --in-file $input --out-file "$out_file" --out-dir "$out_file.files_path"
+    #if $max_rois    # --max-rois $max_rois #end if
+    #if $link_target # --link-target $link_target #end if
+    #if $link_type   # --link-type $link_type #end if
+    #if $igv_host    # --igv-host $igv_host #end if
+    #if $igv_port    # --igv-port $igv_port #end if
+    #if $ucsc_org    # --ucsc-org $ucsc_org #end if
+    #if $ucsc_db     # --ucsc-db $ucsc_db #end if
+    #if $ucsc_chr_prefix # --ucsc-chr-prefix "$ucsc_chr_prefix" #end if
+    
+
+    
+    
+        
+
+        
+
+        
+            
+            
+        
+
+        
+            
+            
+            
+        
+
+        
+        
+
+        
+        
+        
+    
+
+    
+    
+        
+    
+
+    
+    
+        
+        
+    
+
+    
+    No help yet.
+
diff -r 61d9bdb6d519 -r 0ac4f6f3d984 roi_metrics.Rscript
--- a/roi_metrics.Rscript	Thu Apr 18 08:03:38 2013 -0400
+++ b/roi_metrics.Rscript	Mon May 06 12:34:43 2013 -0400
@@ -49,7 +49,7 @@
 # Compute metrics.
 # ----------------------------------------------------------------------------
 
-# Read input files.x
+# Read input files.
 roi = readROI(opt$infile)
 
 # Compute some basic statistics.
diff -r 61d9bdb6d519 -r 0ac4f6f3d984 roi_plot_thumbnails.py
--- a/roi_plot_thumbnails.py	Thu Apr 18 08:03:38 2013 -0400
+++ b/roi_plot_thumbnails.py	Mon May 06 12:34:43 2013 -0400
@@ -148,7 +148,7 @@
         print >>sys.stderr, 'Writing HTML to %s' % file_name
         with open(file_name, 'wb') as f:
             f.write('\n')
-            f.write('\n')
+            f.write('ROI Thumbnail Plots
')
             for gl in self.grid_links:
                 vals = (gl.file_name, gl.file_name, self.grid.canvas_width, self.grid.canvas_height)
                 f.write('
\n' % vals)
@@ -168,7 +168,8 @@
                     f.write('  \n' % vals)
                 f.write('\n')
-            f.write('\n')
+            f.write('\n')
+            f.write('' + str(self.args) + '