mark the example submission rule files as being raw, so the escapes dont get confused
[htsworkflow.git] / extra / fix_ob3.py
1 """
2 Fix off by 3 error in our database
3 """
4 from optparse import OptionParser
5 import os
6 import re
7 import sys
8
9 from django.core.management import setup_environ
10 from django.conf import settings
11 setup_environ(settings)
12
13 import htsworkflow.frontend.samples.models as samples
14
15 def main(cmdline=None):
16     parser = make_parser()
17     opts, args = parser.parse_args(cmdline)
18
19     dry_run = not opts.run
20     fix_ob3(dry_run=dry_run)
21
22     return 0
23
24 def make_parser():
25     parser = OptionParser("%prog: fix off by 3 error that creeped in")
26     parser.add_option("--run", default=False, action="store_true",
27                       help="change the database")
28     return parser
29
30 def fix_ob3(dry_run=True):
31     libraries = samples.Library.objects.order_by('id')
32
33     mismatch = 0
34     wrong_amp = 0
35     wrong_amp_ids = set()
36     for lib in libraries:
37         if lib.amplified_from_sample is not None:
38             amp_sample = lib.amplified_from_sample
39             alt_sample = samples.Library.objects.get(pk=int(amp_sample.id)-3)
40             if is_alt_sample_right(lib, alt_sample):
41                 wrong_amp += 1
42                 wrong_amp_ids.add(int(lib.id))
43                 print "--- wrong lib ---"
44                 display_names(lib, amp_sample, alt_sample)
45                 if not dry_run:
46                     lib.amplified_from_sample = alt_sample
47                     lib.save()
48             #elif lib_name != amp_sample.library_name:
49             #    mismatch += 1
50             #    print "--- didn't match ---"
51             #    display_names(lib_name, lib, amp_sample, other_sample)
52     print "-----"
53     print "{0} mismatches".format(mismatch)
54     print "{0} obviously wrong libs".format(wrong_amp)
55     if len(wrong_amp_ids) > 0:
56         print "    {0} - {1}".format(min(wrong_amp_ids), max(wrong_amp_ids))
57
58 def clean_lib_name(lib):
59     """Strip trailing amplified marker character"""
60     return re.sub(" *a$", "", lib.library_name)
61
62 def is_alt_sample_right(lib, alt_sample):
63     """Check to see if the alt sample is the right sample
64     """
65     lib_name = clean_lib_name(lib)
66     return lib_name == alt_sample.library_name
67     
68 def display_names(lib, amp_from_sample, alt_from_sample):
69     lib_name = clean_lib_name(lib)
70     print "NonA: "+lib.id+"|"+lib_name+"|"
71     print "   A: "+lib.id+"|"+lib.library_name+"|"
72     print "AmpF: "+ amp_from_sample.id+"|"+amp_from_sample.library_name+"|"
73     print "FixF: "+ alt_from_sample.id+"|"+alt_from_sample.library_name+"|"
74
75
76 if __name__ == "__main__":
77     sys.exit(main())