Answers.Body: <p>You really need to learn some python :)</p>
  <pre><code>with open('/path/list2.txt', 'r') as fin:
      to_keep = set([])
      for line in fin:
          to_keep.add(line.strip())
          
  with open('/path/list1.txt', 'r') as fin:
      with open('/path/list3.txt', 'w') as fout:
          for line in fin:
              if line.strip().split(' ')[0] in to_keep:
                  fout.write(line)
  </code></pre>
  
Answers.ContentLicense: CC BY-SA 4.0
Answers.CreationDate: 2021-02-04T17:59:41.553Z
Answers.Text: Thank you very much. It worked. Yes, I need to learn python.
Answers.UserDisplayName: MudithMMBc
Answers.ContentLicense: CC BY-SA 4.0
Answers.CreationDate: 2021-02-04T18:09:39.693Z
Answers.Text: And another question. Do you know a way to filter out/remove those sequences from the original fasta file using the id list created from the above code? I have a piece of code I used earlier to remove sequences using a list. But since these ids contains a space, It's not working now. I tried to change it, but it didn't work. It'll be an easy thing for you,but I couldn't figure it out. Can you please help?
Answers.UserDisplayName: MudithMMBc
Answers.ContentLicense: CC BY-SA 4.0
Answers.CreationDate: 2021-02-04T20:16:12.367Z
Answers.Text: Sure. Post a new question with an example plz
Answers.UserDisplayName: Liam McIntyre
Answers.ContentLicense: CC BY-SA 4.0
Answers.CreationDate: 2021-02-05T15:34:22.420Z
Answers.Text: Why would you hardcode two file names instead of just reading argv for the input file and printing to stdout so you can then redirect?
Answers.UserDisplayName: terdon
Answers.ContentLicense: CC BY-SA 4.0
Answers.CreationDate: 2021-02-05T19:16:35.590Z
Answers.Text: He asked for files
Answers.UserDisplayName: Liam McIntyre
Answers.ContentLicense: CC BY-SA 4.0
Answers.CreationDate: 2021-02-04T07:31:24.250Z
Answers.LastActivityDate: 2021-02-04T07:31:24.250Z
Answers.UserDisplayName: Liam McIntyre
Answers.Body: <p>This can be done in python easily. If you have different text files then you can load them into a python list and follow from step 2</p>
  <pre><code># Step 1
  list1 = [&quot;m64041_200717_231916/100664277/0_42364 id=2440&quot;,
  &quot;m64041_200717_231916/100008029/0_42112 id=244006&quot;,
  &quot;m64041_200717_231916/100665802/0_46854 id=2440067&quot;,
  &quot;m64041_200717_231916/100008044/0_60161 id=24400&quot;,
  &quot;m64041_200717_231916/100928073/0_11804 id=2440152&quot;,
  &quot;m64041_200717_231916/10094492/0_44157 id=2440199&quot;,
  &quot;m64041_200717_231916/100008373/4102_50601 id=2440234&quot;]
  
  list2 = [&quot;m64041_200717_231916/100664277/0_42364&quot;,
  &quot;m64041_200717_231916/100665802/0_46854&quot;,
  &quot;m64041_200717_231916/100928073/0_11804&quot;,
  &quot;m64041_200717_231916/10094492/0_44157&quot;]
  
  # Step 2
  split_list1 = [ele.split(&quot; &quot;)[0] for ele in list1 ]
  mapdict = {split_list1[i]:list1[i] for i in range(len(list1))}
  common = [value for value in split_list1 if value in list2] 
  required = [mapdict[x] for x in common]
  print(required)
  </code></pre>
  
Answers.ContentLicense: CC BY-SA 4.0
Answers.CreationDate: 2021-02-25T04:52:30.033Z
Answers.LastActivityDate: 2021-02-25T04:52:30.033Z
Answers.UserDisplayName: Elucidata
Body: <p>I have following two lists.
  List1</p>
  <pre><code>m64041_200717_231916/100664277/0_42364 id=2440
  m64041_200717_231916/100008029/0_42112 id=244006
  m64041_200717_231916/100665802/0_46854 id=2440067
  m64041_200717_231916/100008044/0_60161 id=24400
  m64041_200717_231916/100928073/0_11804 id=2440152
  m64041_200717_231916/10094492/0_44157 id=2440199
  m64041_200717_231916/100008373/4102_50601 id=2440234
  </code></pre>
  <p>List2</p>
  <pre><code>m64041_200717_231916/100664277/0_42364
  m64041_200717_231916/100665802/0_46854
  m64041_200717_231916/100928073/0_11804
  m64041_200717_231916/10094492/0_44157
  </code></pre>
  <p>These are sequence read ids from a genome file. List1 contains all the ids from the List2 but with some id numbers (id=2440). So the sequence ids are only similar to some extent. I want to compare these two files and I need to filter out List2 sequence ids from List one (with id numbers) as below.
  Output.</p>
  <pre><code>m64041_200717_231916/100664277/0_42364 id=2440
  m64041_200717_231916/100665802/0_46854 id=2440067
  m64041_200717_231916/100928073/0_11804 id=2440152
  m64041_200717_231916/10094492/0_44157 id=2440199
  </code></pre>
  <p>I will be grateful if someone can help me with this issue.</p>
  
Comments.ContentLicense: CC BY-SA 4.0
Comments.CreationDate: 2021-02-04T07:08:04.353Z
Comments.Text: It sounds easy but I don't really understand exactly what you want to compare. Can U expand?
Comments.UserDisplayName: Liam McIntyre
Comments.ContentLicense: CC BY-SA 4.0
Comments.CreationDate: 2021-02-04T07:15:57.913Z
Comments.Text: For another example imagine there are two lists. List1.
  UnitedStates id=12,
  UnitedKingdom id=15,
  Spain id=20,
  France id=25,
  
  List2
  
  UnitedStates,
  Spain
  
  I want to filter out UnitedStates id=12 and Spain id=20 using the List2. List1 and List2 are two files.
  Output should be,
  
  UnitedStates id=12,
  Spain id=20
Comments.UserDisplayName: MudithMMBc
Comments.ContentLicense: CC BY-SA 4.0
Comments.CreationDate: 2021-02-04T07:25:41.093Z
Comments.Text: ah, so only keep whats in list 2?
Comments.UserDisplayName: Liam McIntyre
Comments.ContentLicense: CC BY-SA 4.0
Comments.CreationDate: 2021-02-04T07:29:38.833Z
Comments.Text: Yeah. list2 will be the query.
Comments.UserDisplayName: MudithMMBc
Tags: <bash><filtering>
Title: How to filter out partially similar strings from two lists and make one list?
ThreadText: # How to filter out partially similar strings from two lists and make one list?
  
  (Asked by: username_0 on 2021-02-04)
  
  I have following two lists.
  List1
  
  ```
  m64041_200717_231916/100664277/0_42364 id=2440
  m64041_200717_231916/100008029/0_42112 id=244006
  m64041_200717_231916/100665802/0_46854 id=2440067
  m64041_200717_231916/100008044/0_60161 id=24400
  m64041_200717_231916/100928073/0_11804 id=2440152
  m64041_200717_231916/10094492/0_44157 id=2440199
  m64041_200717_231916/100008373/4102_50601 id=2440234
  
  ```
  
  List2
  
  ```
  m64041_200717_231916/100664277/0_42364
  m64041_200717_231916/100665802/0_46854
  m64041_200717_231916/100928073/0_11804
  m64041_200717_231916/10094492/0_44157
  
  ```
  
  These are sequence read ids from a genome file. List1 contains all the ids from the List2 but with some id numbers (id=2440). So the sequence ids are only similar to some extent. I want to compare these two files and I need to filter out List2 sequence ids from List one (with id numbers) as below.
  Output.
  
  ```
  m64041_200717_231916/100664277/0_42364 id=2440
  m64041_200717_231916/100665802/0_46854 id=2440067
  m64041_200717_231916/100928073/0_11804 id=2440152
  m64041_200717_231916/10094492/0_44157 id=2440199
  
  ```
  
  I will be grateful if someone can help me with this issue.
  
  **Tags:** bash, filtering
  
  > **Comments:**
  > **username_1**: It sounds easy but I don't really understand exactly what you want to compare. Can U expand?  
  > **username_0**: For another example imagine there are two lists. List1. UnitedStates id=12, UnitedKingdom id=15, Spain id=20, France id=25,  List2  UnitedStates, Spain  I want to filter out UnitedStates id=12 and Spain id=20 using the List2. List1 and List2 are two files. Output should be,  UnitedStates id=12, Spain id=20  
  > **username_1**: ah, so only keep whats in list 2?  
  > **username_0**: Yeah. list2 will be the query.  
  
  ---
  
  ## 2 answers
  
  ### Answer by username_1 on 2021-02-04. Score: 3 | Accepted answer
  
  You really need to learn some python :)
  
  ```
  with open('/path/list2.txt', 'r') as fin:
      to_keep = set([])
      for line in fin:
          to_keep.add(line.strip())
          
  with open('/path/list1.txt', 'r') as fin:
      with open('/path/list3.txt', 'w') as fout:
          for line in fin:
              if line.strip().split(' ')[0] in to_keep:
                  fout.write(line)
  
  ```
  
  > **Comments:**
  > **username_0**: Thank you very much. It worked. Yes, I need to learn python.  
  > **username_0**: And another question. Do you know a way to filter out/remove those sequences from the original fasta file using the id list created from the above code? I have a piece of code I used earlier to remove sequences using a list. But since these ids contains a space, It's not working now. I tried to change it, but it didn't work. It'll be an easy thing for you,but I couldn't figure it out. Can you please help?  
  > **username_1**: Sure. Post a new question with an example plz  
  > **username_2**: Why would you hardcode two file names instead of just reading argv for the input file and printing to stdout so you can then redirect?  
  > **username_1**: He asked for files  
  
  ---
  
  ### Answer by username_3 on 2021-02-25. Score: 0
  
  This can be done in python easily. If you have different text files then you can load them into a python list and follow from step 2
  
  ```
  # Step 1
  list1 = ["m64041_200717_231916/100664277/0_42364 id=2440",
  "m64041_200717_231916/100008029/0_42112 id=244006",
  "m64041_200717_231916/100665802/0_46854 id=2440067",
  "m64041_200717_231916/100008044/0_60161 id=24400",
  "m64041_200717_231916/100928073/0_11804 id=2440152",
  "m64041_200717_231916/10094492/0_44157 id=2440199",
  "m64041_200717_231916/100008373/4102_50601 id=2440234"]
  
  list2 = ["m64041_200717_231916/100664277/0_42364",
  "m64041_200717_231916/100665802/0_46854",
  "m64041_200717_231916/100928073/0_11804",
  "m64041_200717_231916/10094492/0_44157"]
  
  # Step 2
  split_list1 = [ele.split(" ")[0] for ele in list1 ]
  mapdict = {split_list1[i]:list1[i] for i in range(len(list1))}
  common = [value for value in split_list1 if value in list2] 
  required = [mapdict[x] for x in common]
  print(required)
  
  ```
  
  
  ---
  
  