taffners

Mixed starting strain to one ending strain

Apr 18th, 2013
115
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 9.40 KB | None | 0 0
  1. #|*******************************************************************************
  2. #| Functions that handle mixed starting strain to one ending strain
  3. #|*******************************************************************************
  4.  
  5.  
  6. def make_extra_plates():
  7.     '''
  8.    |       user input -> file
  9.    |
  10.    |       Returns a file containing multiplexed plate or plates need to run
  11.    |       that includes all the primers missing in the first file but present
  12.    |       in the second file.
  13.    |
  14.    |       Preconditions:
  15.    |           files
  16.    |               .txt files
  17.    |
  18.    |               example snippet of one input file
  19.    |               1 D15mit252F D6mit224V D15mit59N none
  20.    |               2 D1mit440F D1mit236V D16mit107N none
  21.    |
  22.    |               no blank lines in the file
  23.    |
  24.    |           python 3 needs to be used
  25.    |      
  26.    |       Example use of this function:
  27.    |       A comstomer comes to start a speed congenics project however the
  28.    |       starting strain of the project is mixed.  For example the starting
  29.    |       strain is 129 and C3H combined.  The customer wants to cross this
  30.    |       mouse onto the B6 background.  Therefore the best approach would be
  31.    |       to run a full 129XB6 screen (1st input asked for) and then run the
  32.    |       primers that are present in the B6XC3H screen and not in the 129XB6
  33.    |       screen. The second screen asked for will be the B6XC3H screen.
  34.    
  35.    '''
  36.  
  37. # get input of locations of files
  38.     full_screen_running = input('''Please enter the location of the full
  39. screen be you running: ''')
  40.     other_strain = input('''Plese enter the location of the other plate mat
  41. you are wishing to inducde: ''')
  42.  
  43. # output_list will now equall a list of all primers not in file1 but in file2
  44.     extra_primers_needed  = primers_missing_from_plate_mat(full_screen_running,other_strain)
  45.  
  46. # seperates all primers into nested fluorescence lists
  47.     fluoro_list = make_seperate_lists(extra_primers_needed)
  48.  
  49. # remove all the empty lists.
  50.     empties_removed = remove_empties(fluoro_list)
  51.    
  52. # multiplex all the primers
  53.     multiplexed = multiplex_primers(empties_removed)
  54.  
  55. # get input of where is the output_file is
  56.     to_file = input('''Please enter the location of the .txt file you would
  57. like the multiplexed extra plates writen to: ''')
  58.  
  59. # write all the primers to a file
  60.     write_extra_primer_layout(multiplexed, to_file)
  61.  
  62.     print('Complete!  The extra primers you need are located at', to_file)
  63.  
  64. def primers_missing_from_plate_mat(first_screen_mat, extra_primer_mat):
  65.     '''
  66.    |       (file, file) -> list
  67.    |
  68.    |       Return a list of primers which are not in the first file but are
  69.    |       present in the second file.
  70.    |
  71.    |       first_screen_mat example
  72.    |       1 D15mit252 F D6mit224 V D15mit59 N none
  73.    |       2 D1mit440 F D1mit236 V D16mit107 N none
  74.    |    
  75.    |       extra_primer_mat example
  76.    |       1 D15mit252 F D6mit224 V D15mit59 N none
  77.    |       2 D19mit91 V D1mit236 V D16mit107 N none
  78.    |
  79.    |       list output example
  80.    |       ['D19mit91 V']
  81.    
  82.    '''
  83.     with open(first_screen_mat, 'r') as sf:
  84.         with open(extra_primer_mat, 'r') as of:
  85.             line = sf.readline()
  86.             sf_list = []
  87.             while line != '':
  88.                
  89.                 line = line.strip().split()
  90.                
  91.                
  92.                 for s in line:
  93.                     if s != 'none' and not s.isdigit():
  94.                         sf_list.append(s)
  95.                        
  96.                 line = sf.readline()
  97.  
  98.  
  99.             line = of.readline()
  100.             output_list = []
  101.  
  102.             while line != '':
  103.                 line = line.strip().split()
  104.  
  105.                 for s in line:
  106.                     if s not in sf_list and s != 'none' and not s.isdigit():
  107.                    
  108.                         output_list.append(s)
  109.                        
  110.                        
  111.                 line = of.readline()
  112.    
  113.     print('\nThe full screen you will be running includes', len(sf_list), 'primers\n')
  114.    
  115.     print('The extra plate or plates includes', len(output_list), 'primers\n')
  116.  
  117.     print('Therefore the total number of primers used for this project is',
  118.           (len(sf_list) + len(output_list)), '\n')
  119.     return output_list
  120.  
  121. def make_seperate_lists(mixed_list):
  122.     '''
  123.    |       (list) -> list
  124.    |
  125.    |       Returns a nested 2D list of strings.  Each nested list is seperated into
  126.    |       primer fluorescence. Therefore there are 4 nested lists.  One for FAM,
  127.    |       one for Vic, one for Ned, and one for Pet in that order.  Each list will
  128.    |       be created.  However, somelists might be empty such as the Pet list
  129.    |       in the example below.
  130.    |
  131.    |       example input list:
  132.    |       ['D19mit91F', 'D13mit275V', 'D9mit90V', 'D14mit95F', 'D9mit182F',
  133.    |       'D11mit231N']
  134.    |
  135.    |       example output_list:
  136.    |       [['D19mit91F', 'D14mit95F', 'D9mit182F'], ['D13mit275V', 'D9mit90V'],
  137.    |       ['D11mit231N'], []]
  138.  
  139.  
  140.    '''
  141.     l = mixed_list
  142.     F_list = []
  143.     V_list = []
  144.     N_list = []
  145.     P_list = []
  146.     for s in l:
  147.         if s[-1] == 'F':
  148.             F_list.append(s)
  149.         elif s[-1] == 'V':
  150.             V_list.append(s)
  151.         elif s[-1] == 'N':
  152.             N_list.append(s)
  153.         elif s[-1] == 'P':
  154.             P_list.append(s)
  155.  
  156.     new_list = [F_list, V_list, N_list, P_list]  
  157.     return new_list
  158.  
  159. def remove_empties(fluro_seperated_list):
  160.     '''
  161.    |       (2D list) -> 2D list
  162.    |
  163.    |       Returns a nested 2D list that contains no empty lists.  
  164.    |
  165.    |       Example 2D_list_input:
  166.    |       [['D19mit91F', 'D14mit95F', 'D9mit182F'], ['D13mit275V', 'D9mit90V'],
  167.    |       ['D11mit231N'], []]
  168.    |
  169.    |       Example 2D_list_output:
  170.    |       [['D19mit91F', 'D14mit95F', 'D9mit182F'], ['D13mit275V', 'D9mit90V'],
  171.    |       ['D11mit231N']]
  172.    '''
  173.     list2 = [x for x in fluro_seperated_list if x]
  174.     return list2
  175.  
  176. def multiplex_primers(fluro_seperated_list_no_empties):
  177.     '''
  178.    |       (2D list) -> 2D list
  179.    |
  180.    |       Returns a list where each primer is multiplexed with one other primer of
  181.    |       a different fluorescence.
  182.    |
  183.    |       Example input_list:
  184.    |       [['D19mit91F', 'D14mit95F', 'D9mit182F'], ['D13mit275V', 'D9mit90V'],
  185.    |       ['D11mit231N']]
  186.    |
  187.    |       Example output_list:
  188.    |       [['D19mit91F', 'D13mit275V'], ['D14mit95F', 'D9mit90V'],
  189.    |       ['D9mit182F', 'D11mit231N']]
  190.  
  191.    '''
  192.  
  193.     answer = []
  194.    
  195.     new_list = fluro_seperated_list_no_empties
  196.    
  197.     while len(new_list[0]) > 0 and len(new_list[1]) > 0:
  198.         answer.append([new_list[0][0], new_list[1][0]])
  199.         del new_list[0][0], new_list[1][0]
  200.  
  201.         new_list = remove_empties(new_list) #remove empty lists every iteration
  202.  
  203.         if len(new_list) == 0: #if length is 0, our work is done
  204.             break
  205.         if len(new_list) == 1: #if length is 1, we need to append remaining numbers as lists with length 1
  206.            
  207.             for i in new_list[0]:
  208.                 answer.append([i])
  209.             break
  210.    
  211.     return answer
  212.    
  213.  
  214. def write_extra_primer_layout(list_of_multiplexed_primers, to_file):
  215.     '''
  216.    |       (2D list) -> file
  217.    |
  218.    |       Returns a file  that contains primers in the lists in plate format.  A
  219.    |       number in the file counts up to 12 and restarts for the next plate.
  220.    |
  221.    |       Example input_list
  222.    |       [['D19mit91F', 'D13mit275V'], ['D14mit95F', 'D9mit90V'],
  223.    |       ['D9mit182F', 'D11mit231N'], ['D19mit91F', 'D13mit275V'],
  224.    |       ['D14mit95F', 'D9mit90V'], ['D9mit182F', 'D11mit231N'],
  225.    |       ['D19mit91F', 'D13mit275V'], ['D14mit95F', 'D9mit90V'],
  226.    |       ['D9mit182F', 'D11mit231N'],['D19mit91F', 'D13mit275V'],
  227.    |       ['D14mit95F', 'D9mit90V'], ['D9mit182F', 'D11mit231N'],
  228.    |       ['D19mit91F', 'D13mit275V'], ['D14mit95F', 'D9mit90V'],
  229.    |       ['D9mit182F', 'D11mit231N'], ['D19mit91F', 'D13mit275V'],
  230.    |       ['D14mit95F', 'D9mit90V'], ['D9mit182F', 'D11mit231N'],
  231.    |       ['D19mit91F', 'D13mit275V'], ['D14mit95F', 'D9mit90V'],
  232.    |       ['D9mit182F', 'D11mit231N']]
  233.    |
  234.    |       Example out_put file:
  235.    |       1 D19mit91F D13mit275V
  236.    |       2 D14mit95F D9mit90V
  237.    |       3 D9mit182F D11mit231N
  238.    |       4 D19mit91F D13mit275V
  239.    |       5 D14mit95F D9mit90V
  240.    |       6 D9mit182F D11mit231N
  241.    |       7 D19mit91F D13mit275V
  242.    |       8 D14mit95F D9mit90V
  243.    |       9 D9mit182F D11mit231N
  244.    |       10 D19mit91F D13mit275V
  245.    |       11 D14mit95F D9mit90V
  246.    |       12 D9mit182F D11mit231N
  247.    |
  248.    |       1 D19mit91F D13mit275V
  249.    |       2 D14mit95F D9mit90V
  250.    |       3 D9mit182F D11mit231N
  251.    |       4 D19mit91F D13mit275V
  252.    |       5 D14mit95F D9mit90V
  253.    |       6 D9mit182F D11mit231N
  254.    |       7 D19mit91F D13mit275V
  255.    |       8 D14mit95F D9mit90V
  256.    |       9 D9mit182F D11mit231N
  257.    '''
  258.    
  259.     in_list = list_of_multiplexed_primers
  260.     with open(to_file, 'w') as tf:
  261.      
  262.       num = 1
  263.       for i in in_list:
  264.         if num == 12:
  265.             tf.write(str(num)+ ' ' +(' '.join(i)) + ' ' +'\n\n')
  266.             num = 1
  267.  
  268.         else:
  269.             tf.write(str(num)+ ' ' +(' '.join(i)) + ' ' +'\n')
  270.             num = num + 1
Advertisement
Add Comment
Please, Sign In to add comment