text: user1224: I have a list of protein sequence ids (names). I want to sort the lines of a table, checking if the id is equal to the first column of the table. I split the table on lines and search for the id via regex:
  code:
  def map_line_to_ids(id):
      id= id.strip('\n')
      for s in lines:
          if re.search(id,s):
              return s #<------------ why always return else not if(s) when takes empty 'def'
          else:
              return id + '\n'
  
  names = ['XXXXXXX45.1', 'YP_343434.1', 'WP_XXXXXXXX2.1', 'WP_XXXXXXX90.1',
           'WP_0XXXXXX62.1', 'XP_222222222.2', 'WP_192XXXXXX.1',
           'WP_XXXXXXX29.1', 'WP_0XXXXXXXX.1']
  
  sorted_lines = list(map(map_line_to_ids, names))
  
  names (order):
  XXXXXXX45.1
  YP_343434.1
  WP_XXXXXXXX2.1
  WP_XXXXXXX90.1
  WP_0XXXXXX62.1
  XP_222222222.2
  WP_192XXXXXX.1
  WP_XXXXXXX29.1
  WP_0XXXXXXXX.1
  
  Input table:
  WP_XXXXXXXX2.1  PF06325 Ribosomal protein L11 methyltransferase (PrmA)  5
  WP_192XXXXXX.1  PF13489 Methyltransferase domain    4
  XXXXXXX45.1 PF02310 B12 binding domain  12; PF04055 Radical SAM superfamily 5 
  WP_XXXXXXX29.1  PF13489 Methyltransferase domain    4
  WP_0XXXXXXXX.1  PF13489 Methyltransferase domain    4
  WP_XXXXXXX90.1  PF06325 Ribosomal protein L11 methyltransferase (PrmA)  5; PF06325  Ribosomal protein L11 methyltransferase (PrmA)  7  
  WP_0XXXXXX62.1  PF13489 Methyltransferase domain    4
  
  Expected output:
  XXXXXXX45.1 PF02310 B12 binding domain  12; PF04055 Radical SAM superfamily 5
  YP_343434.1
  WP_XXXXXXXX2.1  PF06325 Ribosomal protein L11 methyltransferase (PrmA)  5
  WP_XXXXXXX90.1  PF06325 Ribosomal protein L11 methyltransferase (PrmA)  5; PF06325  Ribosomal protein L11 methyltransferase (PrmA)  7
  XP_222222222.2  
  WP_0XXXXXX62.1  PF13489 Methyltransferase domain    4
  WP_192XXXXXX.1  PF13489 Methyltransferase domain    4
  WP_XXXXXXX29.1  PF13489 Methyltransferase domain    4
  WP_0XXXXXXXX.1  PF13489 Methyltransferase domain    4
  
  
  user4413: Following on your last post, your initial file test.txt looks like that:
  WP_XXXXXXXX2.1  PF06325 Ribosomal protein L11 methyltransferase (PrmA)  5
  WP_192XXXXXX.1  PF13489 Methyltransferase domain    4
  XXXXXXX45.1 PF02310 B12 binding domain  12
  XXXXXXX45.1 PF04055 Radical SAM superfamily 5
  WP_XXXXXXX29.1  PF13489 Methyltransferase domain    4
  WP_0XXXXXXXX.1  PF13489 Methyltransferase domain    4
  WP_XXXXXXX90.1  PF06325 Ribosomal protein L11 methyltransferase (PrmA)  5
  WP_XXXXXXX90.1  PF06325 Ribosomal protein L11 methyltransferase (PrmA)  7
  WP_0XXXXXX62.1  PF13489 Methyltransferase domain    4
  
  And you have another file order.txt that contains your ordered IDs:
  XXXXXXX45.1
  YP_343434.1
  WP_XXXXXXXX2.1
  WP_XXXXXXX90.1
  WP_0XXXXXX62.1
  XP_222222222.2
  WP_192XXXXXX.1
  WP_XXXXXXX29.1
  WP_0XXXXXXXX.1
  
  Since you want to merge by the first field in test.txt and sort them according to order.txt, you have to read first both of the files:
  # read 'test.txt'
  with open("test.txt",'r') as rf:
      l = rf.readlines()
  
  # read sorted IDs 'order.txt'
  with open("order.txt",'r') as rf:
      l_order = rf.readlines()
  
  # remove newlines from each element in list
  l_order = [i.strip() for i in l_order]
  
  My previous answers describe how we obtain the merged fields in the file, so I will not go over it again. Since we need to sort the dictionary according to the list, we simply have to do:
  # for each element in ordered list
  for i in l_order:
      if i in dict_id.keys():                        # if element is in my dictionary's keys
          print(i + "\t" + "; ".join(dict_id[i]))    # print the key and the value
      else:
          print(i)                                   # else prints the element
  
  This should print:
  XXXXXXX45.1     PF02310 B12 binding domain      12; PF04055     Radical SAM superfamily 5
  YP_343434.1
  WP_XXXXXXXX2.1  PF06325 Ribosomal protein L11 methyltransferase (PrmA)  5
  WP_XXXXXXX90.1  PF06325 Ribosomal protein L11 methyltransferase (PrmA)  5; PF06325      Ribosomal protein L11 methyltransferase (PrmA)  7
  WP_0XXXXXX62.1  PF13489 Methyltransferase domain        4
  XP_222222222.2
  WP_192XXXXXX.1  PF13489 Methyltransferase domain        4
  WP_XXXXXXX29.1  PF13489 Methyltransferase domain        4
  WP_0XXXXXXXX.1  PF13489 Methyltransferase domain        4
  
  Full code:
  with open("test.txt",'r') as rf:
      l = rf.readlines()
  
  with open("order.txt",'r') as rf:
      l_order = rf.readlines()
  
  l_order = [i.strip() for i in l_order]
  
  dict_id = {}
  for i in l:
      key_id = i.split("\t")[0]
      if key_id not in dict_id.keys():
          value = "\t".join(i.split("\t")[1:]).rstrip()
          dict_id[key_id] = [value] 
      else:
          dict_id[key_id].append("\t".join(i.split("\t")[1:]).rstrip())
  
  for i in l_order:
      if i in dict_id.keys():
          print(i + "\t" + "; ".join(dict_id[i]))
      else:
          print(i)
  
  PS: please for further pure programming questions, do post your questions in StackOverflow instead of Bioinformatics.
  
original_text: 1224: <p>I have a list of protein sequence ids (names). I want to sort the lines of a table, checking if the id is equal to the first column of the table. I split the table on lines and search for the id via regex:</p>
  <p>code:</p>
  <pre><code>def map_line_to_ids(id):
      id= id.strip('\n')
      for s in lines:
          if re.search(id,s):
              return s #&lt;------------ why always return else not if(s) when takes empty 'def'
          else:
              return id + '\n'
  
  names = ['XXXXXXX45.1', 'YP_343434.1', 'WP_XXXXXXXX2.1', 'WP_XXXXXXX90.1',
           'WP_0XXXXXX62.1', 'XP_222222222.2', 'WP_192XXXXXX.1',
           'WP_XXXXXXX29.1', 'WP_0XXXXXXXX.1']
  
  sorted_lines = list(map(map_line_to_ids, names))
  </code></pre>
  <p>names (order):</p>
  <pre><code>XXXXXXX45.1
  YP_343434.1
  WP_XXXXXXXX2.1
  WP_XXXXXXX90.1
  WP_0XXXXXX62.1
  XP_222222222.2
  WP_192XXXXXX.1
  WP_XXXXXXX29.1
  WP_0XXXXXXXX.1
  </code></pre>
  <p>Input table:</p>
  <pre><code>WP_XXXXXXXX2.1  PF06325 Ribosomal protein L11 methyltransferase (PrmA)  5
  WP_192XXXXXX.1  PF13489 Methyltransferase domain    4
  XXXXXXX45.1 PF02310 B12 binding domain  12; PF04055 Radical SAM superfamily 5 
  WP_XXXXXXX29.1  PF13489 Methyltransferase domain    4
  WP_0XXXXXXXX.1  PF13489 Methyltransferase domain    4
  WP_XXXXXXX90.1  PF06325 Ribosomal protein L11 methyltransferase (PrmA)  5; PF06325  Ribosomal protein L11 methyltransferase (PrmA)  7  
  WP_0XXXXXX62.1  PF13489 Methyltransferase domain    4
  </code></pre>
  <p>Expected output:</p>
  <pre><code>XXXXXXX45.1 PF02310 B12 binding domain  12; PF04055 Radical SAM superfamily 5
  YP_343434.1
  WP_XXXXXXXX2.1  PF06325 Ribosomal protein L11 methyltransferase (PrmA)  5
  WP_XXXXXXX90.1  PF06325 Ribosomal protein L11 methyltransferase (PrmA)  5; PF06325  Ribosomal protein L11 methyltransferase (PrmA)  7
  XP_222222222.2  
  WP_0XXXXXX62.1  PF13489 Methyltransferase domain    4
  WP_192XXXXXX.1  PF13489 Methyltransferase domain    4
  WP_XXXXXXX29.1  PF13489 Methyltransferase domain    4
  WP_0XXXXXXXX.1  PF13489 Methyltransferase domain    4
  </code></pre>
   4413: <p>Following on your <a href="https://bioinformatics.stackexchange.com/questions/18412/connect-same-ids-with-values-in-id-rows-result-of-effectiveeld/18413#18413">last post</a>, your initial file <code>test.txt</code> looks like that:</p>
  <pre><code>WP_XXXXXXXX2.1  PF06325 Ribosomal protein L11 methyltransferase (PrmA)  5
  WP_192XXXXXX.1  PF13489 Methyltransferase domain    4
  XXXXXXX45.1 PF02310 B12 binding domain  12
  XXXXXXX45.1 PF04055 Radical SAM superfamily 5
  WP_XXXXXXX29.1  PF13489 Methyltransferase domain    4
  WP_0XXXXXXXX.1  PF13489 Methyltransferase domain    4
  WP_XXXXXXX90.1  PF06325 Ribosomal protein L11 methyltransferase (PrmA)  5
  WP_XXXXXXX90.1  PF06325 Ribosomal protein L11 methyltransferase (PrmA)  7
  WP_0XXXXXX62.1  PF13489 Methyltransferase domain    4
  </code></pre>
  <p>And you have another file <code>order.txt</code> that contains your ordered IDs:</p>
  <pre><code>XXXXXXX45.1
  YP_343434.1
  WP_XXXXXXXX2.1
  WP_XXXXXXX90.1
  WP_0XXXXXX62.1
  XP_222222222.2
  WP_192XXXXXX.1
  WP_XXXXXXX29.1
  WP_0XXXXXXXX.1
  </code></pre>
  <p>Since you want to merge by the first field in <code>test.txt</code> and sort them according to <code>order.txt</code>, you have to read first both of the files:</p>
  <pre><code># read 'test.txt'
  with open(&quot;test.txt&quot;,'r') as rf:
      l = rf.readlines()
  
  # read sorted IDs 'order.txt'
  with open(&quot;order.txt&quot;,'r') as rf:
      l_order = rf.readlines()
  
  # remove newlines from each element in list
  l_order = [i.strip() for i in l_order]
  </code></pre>
  <p>My previous answers describe how we obtain the merged fields in the file, so I will not go over it again. Since we need to sort the dictionary according to the list, we simply have to do:</p>
  <pre><code># for each element in ordered list
  for i in l_order:
      if i in dict_id.keys():                        # if element is in my dictionary's keys
          print(i + &quot;\t&quot; + &quot;; &quot;.join(dict_id[i]))    # print the key and the value
      else:
          print(i)                                   # else prints the element
  </code></pre>
  <p>This should print:</p>
  <pre><code>XXXXXXX45.1     PF02310 B12 binding domain      12; PF04055     Radical SAM superfamily 5
  YP_343434.1
  WP_XXXXXXXX2.1  PF06325 Ribosomal protein L11 methyltransferase (PrmA)  5
  WP_XXXXXXX90.1  PF06325 Ribosomal protein L11 methyltransferase (PrmA)  5; PF06325      Ribosomal protein L11 methyltransferase (PrmA)  7
  WP_0XXXXXX62.1  PF13489 Methyltransferase domain        4
  XP_222222222.2
  WP_192XXXXXX.1  PF13489 Methyltransferase domain        4
  WP_XXXXXXX29.1  PF13489 Methyltransferase domain        4
  WP_0XXXXXXXX.1  PF13489 Methyltransferase domain        4
  </code></pre>
  <p>Full code:</p>
  <pre><code>with open(&quot;test.txt&quot;,'r') as rf:
      l = rf.readlines()
  
  with open(&quot;order.txt&quot;,'r') as rf:
      l_order = rf.readlines()
  
  l_order = [i.strip() for i in l_order]
  
  dict_id = {}
  for i in l:
      key_id = i.split(&quot;\t&quot;)[0]
      if key_id not in dict_id.keys():
          value = &quot;\t&quot;.join(i.split(&quot;\t&quot;)[1:]).rstrip()
          dict_id[key_id] = [value] 
      else:
          dict_id[key_id].append(&quot;\t&quot;.join(i.split(&quot;\t&quot;)[1:]).rstrip())
  
  for i in l_order:
      if i in dict_id.keys():
          print(i + &quot;\t&quot; + &quot;; &quot;.join(dict_id[i]))
      else:
          print(i)
  </code></pre>
  <p>PS: please for further pure programming questions, do post your questions in StackOverflow instead of Bioinformatics.</p>
  